MQTT Discovery: Allow setting the entity_id via disovery message

Hey,

I’m extensively using MQTT discovery in conjunction with ESP8266 devices. One thing that I found quite annoying and counterintuitive is that the entity id of discovered entities derived from the MQTT name without a way to change it. My friendly names are not necessarily globally unique (for example, I want to have an “Uptime” sensor for each device). Moreover, I’d like to have them in my local language but keep entity names etc. in English.

Since people using zigbee2mqtt / tasmota probably don’t want the entity id to be derived from the unique_id, my proposal is as follows: Add an optional field entity_id to the MQTT discovery message. If present, the homeassistant entity id is set to <platform>.<discovery json entity id>, requiring that this entity id is unique. Optionally, entity_id could be used as unique_id, too. If entity_id is not present, fall back to the “old” behaviour. This would mean that nothing changes for existing setups.

I think this should be quite easy to add, either in the generic entity id derivation function or in the one of MQTT mixins (I guess MqttAttributes). I’m not sure which is better though.

EDIT: Changed my proposal slightly since different use cases don’t want unique_id in their system.

That would be a nightmare, given the way tasmota and zigbee2mqtt create unique id’s. Tasmota -> unique id is derived from the Mac address, zigbee2mqtt -> unique id is derived from the IEEE address. We would end with totally incomprehensible entity id’s. Worst feature request ever !

1 Like

At least in my case, zigbee2mqtt requires manually setting a name in either case (after all, how could it possibly know the name?). For me, it doesn’t really matter whether you edit the zigbee2mqtt yaml or change the entity id in homeassistant. And yes, the ids aren’t readable, but thats not the purpose of an (unique) ID. That’s what friendly names are for IMO.

Alternatively, MQTT discovery messages could be equipped with an additional “entity_id” field which is required to be unique if present.

I’m with Francis on this. I would much prefer to see:

sensor.gadget_status
sensor.gadget_status_2

Rather than:

sensor.9873289723649876234
sensor.9832093264098623490

Fix your friendly names. device_friendly_name works for me. You can then easily change this when adding the device so that you end up with:

entity_id: sensor.device_1_status
friendly_name: Status

entity_id: sensor.device_2_status
friendly_name: Status

I really think “friendly names” shouldn’t have any impact on the behaviour except display or as a fallback. However, I do see your point (I’m not working much with zigbee or tasmota). I’m adding dozens of devices via MQTT, each with several sensors, manually changing them is quite tedious and errorprone to be honest.

Let me reformulate my request. What I am missing is a way of setting sensor ids via MQTT. So maybe instead one could add an optional entity_id or similar to the MQTT message. Then, the entity_id is derived as <platform>.<entity_name>, requiring that this id is unique. Moreover, entity_id could then be used as unique_id, too. If entity_id is not set, fall back to the previous behaviour.

I updated the original post

And I fully agree with @incaseoftrouble. There must be a way to set the entity ID separately.
The advantage of the discovery is that the entities get generated automatically. If I now have to manually remove the device name from the friendly name, I am almost faster on doing all manually without using the discovery!

And we are not the only one, see MQTT Sensor configuration payload and entity ID vs Name? - #2 by 123

It just goes off the name doesn’t…

This is the payload from my discovery device.

SMA Inverter Nom (sensor.sma_inverter_nom)
MQTT discovery data:
  Topic: homeassistant/sensor/sbfspot_Hasinverter/sbfspot_123908348InvName/config
     Payload
       name: SMA Inverter Nom
       state_topic: homeassistant/sbfspot_Hasinverter/sbfspot_123908348
       value_template: '{{ value_json.InvName | default() }}'
       unique_id: 123908348_InvName
       enabled_by_default: 'true'
       entity_category: diagnostic
       icon: mdi:card-bulleted
       device:
          identifiers:
             - HAOS-SBFspot-Sensors
          name: HAOS-SBFspot
          model: SB 5000TL-20
          manufacturer: SMA
         sw_version: 03.10.37.R
         platform: mqtt

This is the displayed name
SMA Inverter Nom

This is the entity ID
sensor.sma_inverter_nom

Yes, I know. But I would prefer not to have the Device Name within the Friendly Name as it gets too bulky!

But there seems to be an undocumented feature (or I get dump):
I added now the device name to the name and expected that each name then also has it. But it looks as if they remove the device name from the name again:
grafik

So either I broke something, but it now looks as wanted :slight_smile:

Watch that the title of the edit box still contains the device name, but not the entity itself!

Sorry I am not sure what you mean.

This request is 2 years old…

Is you system upto date? It looks very different to mine.

Looking on the documentation once more, it looks like as if it is not fully correct.
They clearly state that the object_id in the config topic is not used for the entity ID, how ever it looks as if they still use it that way!

Yes, my HA is up to date.

In your screenshot I don’t see the sensors Friendly name. Is it now “SMA Grid Frequency” or “Frequency”? I want to have the later one. And it seems to work this ways although the documentation states differnetly.

SMA Grid Frequency is the friendly name

To name it frequency I would give it the name frequency instead in the payload.

like this topic

homeassistant/sensor/sbfspot_5000TL-test/sbfspot_1223891379GridFreq/config

message

{
  "name": "Frequency",
  "state_topic": "homeassistant/sbfspot_5000TL-test/sbfspot_1223891379",
  "value_template": "{{ value_json.GridFreq | float(default) }}",
  "unique_id": "1223891379_GridFreq",
  "device_class": "frequency",
  "state_class": "measurement",
  "unit_of_measurement": "Hz",
  "enabled_by_default": "true",
  "icon": "mdi:transmission-tower",
  "device": {
    "identifiers": [
      "test-SBFspot-Sensors"
    ],
    "name": "test-SBFspot",
    "model": "SB_5000TL-20",
    "manufacturer": "SMA",
    "sw_version": "03.10.37.R"
  }
}