feature-requests icon indicating copy to clipboard operation
feature-requests copied to clipboard

I would like to use name_add_mac_suffix in mqtt topic_prefix

Open stbungaof opened this issue 2 years ago • 6 comments

`esphome: name: mono name_add_mac_suffix: true

mqtt: broker: !secret mqtt_host username: !secret mqtt_username password: !secret mqtt_password`

It show device name and wifi hostname like this “mono-8d5128” but mqtt only show “mono” Topic Prefix: ‘mono’ State Topic: ‘mono/switch/mono_relay1/state’ State Topic: ‘mono/switch/mono_relay2/state’

Is there a way for me to change it to Topic Prefix: ‘mono-8d5128’ State Topic: ‘mono-8d5128/switch/mono_relay1/state’ State Topic: ‘mono-8d5128/switch/mono_relay2/state’

Sorry, I’m not good at English. I hope you can read and understand.

stbungaof avatar Jun 10 '22 07:06 stbungaof

or allow to use templatable on topic_prefix and on_message: topic like mqtt.publish: topic please.

stbungaof avatar Jun 12 '22 16:06 stbungaof

really need this too

cavamora avatar Sep 19 '23 20:09 cavamora

I ran into this issue while trying to figure out how to use the exact same firmware image for multiple devices without having them overwrite each other's MQTT messages. I ended up with the following workaround for now:

esphome:
  name_add_mac_suffix: true
  on_boot:
    priority: 250
    then:
      - lambda: |-
          auto name = App.get_name();
          id(mqtt_client)->set_topic_prefix(name);
          id(mqtt_client)->set_birth_message(mqtt::MQTTMessage{
              .topic = name + "/status",
              .payload = "online",
              .qos = 0,
              .retain = true,
          });
          id(mqtt_client)->set_last_will(mqtt::MQTTMessage{
              .topic = name + "/status",
              .payload = "offline",
              .qos = 0,
              .retain = true,
          });
          id(mqtt_client)->set_shutdown_message(mqtt::MQTTMessage{
              .topic = name + "/status",
              .payload = "offline",
              .qos = 0,
              .retain = true,
          });
          id(mqtt_client)->set_log_message_template(mqtt::MQTTMessage{
              .topic = name + "/debug",
              .payload = "",
              .qos = 0,
              .retain = true,
          });

gunnarbeutner avatar Nov 04 '23 14:11 gunnarbeutner

Thanks @gunnarbeutner for this fix, worked like a charm. This functionality should definity come as standard if you have multiple devices with the same config to manage.

(if you get the error that the ID is undefined, try replacing id(mqtt_client) by mqtt_mqttclientcomponent )

Mrgove10 avatar Nov 09 '23 22:11 Mrgove10

+1 Please add support for this in ESPHome, being able to have the topic_prefix using unique_id would align with the Home Assistant fix Migrate ESPHome unique ids to new format #99451 . If the topic prefix can be unique, then it will open the to use similar firmware for multiple devices without any workarounds.

Another big thanks to @gunnarbeutner for the workaround tho!

bdemeny avatar Nov 15 '23 05:11 bdemeny

+1

gfplab-scaffardi avatar May 08 '24 14:05 gfplab-scaffardi

+1

artin961 avatar May 17 '24 16:05 artin961