Auto personal name for esphome device MQTT

I recently started learning ESPHome. The problem is the following: I have several light bulbs and I want to control them through mqtt. In the future I want to buy more, if I can with these. But I don’t want to create an almost individual topic for each lamp. Is it possible to automate this? I wanted to use the MAC address, but I can’t figure out how to bind it to the name

See ESPHome Core Configuration — ESPHome - use name_add_mac_suffix

Unfortunately, I did not achieve the desired result. The Device name is changed, but when I want to use this name as a topic name in mutt, the name is written without the suffiximage

i want to get that lamp name with suffix as topic

Something like:
lamp27-(mac_suffix)/status

I suspect I didn’t quite understand how to use this and would appreciate it if you could provide an example.

How many lamp27’s do you have?

Your yaml and log may help.

Now I have 3 of them, but I want to replace all the lamps.

substitutions:
  device_name: lamp27
esphome:
  name: $device_name
  name_add_mac_suffix: True
esp8266:
  board: esp01_1m
# WIFI Setting's
wifi:
  networks:
   - ssid: !secret wifi_ssid
     password: !secret wifi_password
     priority: 5.0
   - ssid: Lamp.update
     password: IDoNotKnow
     priority: 10.0
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ...
    password: ...
# Enable logging
logger:
# Enable Home Assistant API
api:
  encryption:
    key: !secret api_encryption
# Configuration entry
ota:
  password: !secret ota_password
captive_portal:
# MQTT Setting's
mqtt:
  broker: test.mosquitto.org
  username: ...
  password: ....
  discovery: true
  discovery_prefix: Lamp27-150/homeassistant
  log_topic: ${device_name}/logs
  on_connect:
    then:
     - switch.toggle: red_light
     - delay: 5s
     - switch.toggle: red_light
  on_disconnect:
    then:
     - switch.toggle: blue_light
     - delay: 25s
     - switch.toggle: blue_light
switch:
  - platform: gpio
    name: "Red_Light"
    id: red_light
    pin: GPIO4
  - platform: gpio
    name: "Green_Light"
    id: green_light
    pin: GPIO12
  - platform: gpio
    name: "Blue_Light"
    id: blue_light
    pin: GPIO14
  - platform: gpio
    name: "White_Light"
    id: white_light
    pin: GPIO13
# Output's pin on lamp27    
output:
  - platform: esp8266_pwm
    id: output_red
    pin: GPIO4
  - platform: esp8266_pwm
    id: output_green
    pin: GPIO12
  - platform: esp8266_pwm
    id: output_blue
    pin: GPIO14
  - platform: esp8266_pwm
    id: output_white
    pin: GPIO13
light:
  - platform: rgbw
    name: ${device_name}
    red: output_red
    green: output_green
    blue: output_blue
    white: output_white
    color_interlock: true
    default_transition_length: 0.2s
    command_topic: esphome/ota_mode/${device_name}
    effects:
      - pulse:
          name: fpulse
          transition_length: 0.2s
          update_interval: 0.2s
      - pulse:
          name: spulse
          transition_length: 1s
          update_interval: 1s

Please forgive me if the code is badly written.

It is. See How to help us help you - or How to ask a good question

1 Like

It seems to be formatted, as in the instructions.

no ideas?

variant 1:

substitutions:
  name: esp02
  topic: esp/esp02
esphome:
  name: ${name}

switch:
  - platform: gpio
    id: Relay1
    name: "Relay 1"
    pin: GPIO12
    state_topic: "${topic}/gpio12"
    command_topic: "${topic}/set/gpio12"

variant2:

substitutions:
  name: esp02
esphome:
  name: ${name}

switch:
  - platform: gpio
    id: Relay1
    name: "Relay 1"
    pin: GPIO12
    state_topic: "${name}/gpio12"
    command_topic: "${name}/set/gpio12"

Thanks for the answer, but I still have to create a different configuration file for each device.

I’d like to use the standard function.
For example, as in the name if you activate the mac-suffix function.

esphome:
  name: $device_name
  name_add_mac_suffix: True

In this case, in the name of the device on the network, I see this.
lamp27-6761b6
Here I would like to use the same name, but in the mqtt topic, but I can’t understand why esp ignores the name change.

Hi.
@Hromodeus
I had the same issue. Now I’m working on the PR for the esphome.

This will add mac address to mqtt name.
You are welcome to test it.

It also introduces <topic_prefix> placeholder which can be used in customized topic names and subscribe/publish actions. It is essential if your devices with mac in the name need to receive messages.

by default all device topics are <topic_prefix>/xxxxx, but you may want to customize some of the topics to be <topic_prefix>/abcdef/my/custom or even home/lights/<topic_prefix>/status.
Where <topic_prefix> will be expanded to device-name-AABBCC if name_add_mac_suffix: True

Here is an example of config

mqtt:
  broker: !secret mqtt_broker
  discovery: False
  birth_message:
    topic: <topic_prefix>/my_availability
    payload: online
  will_message:
    topic: <topic_prefix>/my_availability
    payload: offline
  on_message:
     - topic: <topic_prefix>/on_message_topic
       then:
        - logger.log: Received on_message_topic
        - mqtt.publish:
            topic: <topic_prefix>/on_message_topic_confirmation
            payload: "Received on_message_topic"
        - mqtt.publish:
            topic: <topic_prefix>/on_json_message_topic
            payload: "{ \"Received\": \"on_message_topic\" }"

And here are the results


[20:35:04][C][mqtt:168]: MQTT:
[20:35:04][C][mqtt:169]:   Server Address: xxx
[20:35:04][C][mqtt:171]:   Username: ''
[20:35:04][C][mqtt:172]:   Client ID: 'esp-sensor-551495-48e729551495'
[20:35:04][C][mqtt:177]:   Topic Prefix: 'esp-sensor-551495'
[20:35:04][C][mqtt:179]:   Log Topic: 'esp-sensor-551495/debug'
[20:35:04][C][mqtt:182]:   Availability: 'esp-sensor-551495/my_availability'
[20:35:04][C][api:139]: API Server:
[20:35:04][C][api:140]:   Address: esp-sensor.local:6053
[20:35:04][C][api:144]:   Using noise encryption: NO
[20:35:04][C][mqtt:709]: MQTT Message Trigger:
[20:35:04][C][mqtt:710]:   Topic: 'esp-sensor-551495/on_message_topic'
[20:35:04][C][mqtt:711]:   QoS: 0
[20:35:04][C][mqtt:727]: MQTT JSON Message Trigger:
[20:35:04][C][mqtt:728]:   Topic: 'esp-sensor-551495/on_json_message_topic'
[20:35:04][C][mqtt:729]:   QoS: 0