EspHome I will be able to use name_add_mac_suffix on mqtt topic_prefix?

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.

I think this

  • discovery_unique_id_generator (Optional, string): The unique_id generator to use. Can be one of legacy or mac. Defaults to legacy, which generates unique_id in format ESP<component_type><default_object_id>. mac generator uses format <mac_address>-<component_type>-<fnv1_hash(friendly_name)>.

from MQTT Client Component — ESPHome

mqtt:
  broker: !secret mqtt_host
  username: !secret mqtt_username
  password: !secret mqtt_password
  discovery_unique_id_generator: mac
[12:59:41][C][mqtt:074]:   Topic Prefix: 'mono'
[12:59:41][C][mqtt:076]:   Log Topic: 'mono/debug'
[12:59:41][C][mqtt:079]:   Availability: 'mono/status'
[12:59:41][C][mqtt.switch:041]: MQTT Switch 'Mono: Wifi Status': 
[12:59:41][C][mqtt.switch:042]:   State Topic: 'mono/switch/mono_wifi_status/state'
[12:59:41][C][mqtt.switch:042]:   Command Topic: 'mono/switch/mono_wifi_status/command'
[12:59:41][C][mqtt.switch:041]: MQTT Switch 'Mono: Relay1': 
[12:59:41][C][mqtt.switch:042]:   State Topic: 'mono/switch/mono_relay1/state'
[12:59:41][C][mqtt.switch:042]:   Command Topic: 'mono/switch/mono_relay1/command'
[12:59:41][C][mqtt.switch:041]: MQTT Switch 'Mono: Relay2': 
[12:59:41][C][mqtt.switch:042]:   State Topic: 'mono/switch/mono_relay2/state'
[12:59:41][C][mqtt.switch:042]:   Command Topic: 'mono/switch/mono_relay2/command'
[12:59:41][C][mqtt.binary_sensor:022]: MQTT Binary Sensor 'Mono: Status':
[12:59:41][C][mqtt.binary_sensor:023]:   State Topic: 'mono/status'

Still getting the same results. :sleepy:

I don’t think you can do it.

Maybe put in a feature request to make MQTT topic_prefix templatable?

In the meantime, you could retrieve the MAC address using the WiFi Info sensor then manually publish your sensors using the mqtt.publish action, which is templatable…

Not the best of workarounds.

Might have been forgotten when they added this.

I think like that.

I think I will do that. There’s more work.

The weird thing is

  • topic_prefix (Optional, string): The prefix used for all MQTT messages. Should not contain trailing slash. Defaults to <APP_NAME>.

what is <APP_NAME> - it does not seem to be defined in the docs.

I found a solution !
I installed esphome on windows then I can edit the code as I want on vscode.
But the downside is that I have to stay in this version of esphome forever.
But I can add components myself. So there’s not much problem.

That entire post makes no sense.

1 Like

hi! have done almost the same here!

1 - created a folder /custom_components/mqtt in project folder

2 - copied the content of the folder “esphome/components/mqtt” from esphome github project to it

3 - added this to yaml file of the project

external_components:
  # use all components from a local folder
  - source:
      type: local
      path: custom_components/mqtt

4 - changed the mqtt_client.cpp file (line 575) to

void MQTTClientComponent::set_topic_prefix(const std::string &topic_prefix) { this->topic_prefix_ = topic_prefix + "/" + get_mac_address(); }

5 - clear and rebuild.

when you do that, it overides the core component

Hi There, I know its after a long time but problem is exactly like yours. What is the solution?
I want the topic name to be (devicename+MAC address) - I dont want that to be hardcoded, how do I get that in my TOPIC ?

I don’t have a problem, but why screw a working system by changing the defaults?

Any way to make the prefix templatable? Oh, and yes, use MAC address in the template?

Thanks for your idea. It works! Almost))
The thing is that the topic that displays the connection status by default still has topic_prefix = node name, without MAC address…
I feel that somewhere else I need to add MAC address reception, but I don’t understand where exactly…

In this repo, I created a .H file to write my own HA discovery MQTT topics, a button.yaml to post sensors just the way i wanted and a mqtt.yaml to subscribe and receive commands.

s_unique_name receives the unique name of the sensor using de mac on boot:

id(s_unique_name).publish_state("esp32_" + mac);

https://bitbucket.org/gustavocmorais/esphome-ita-iot-sensor-cisterna/src/master/