How to set entity_id from unique_id for MQTT sensor?

I’m integrating a few sensors that unfortunately do not adhere to HA’s MQTT standard. Nonetheless integration via configuration.yaml works, but I’m wondering how to get useful entity_id's in HA’s dashboard.

I now have

sensor:
 - platform: mqtt
   name: appliance
   unique_id: '12345.power'
   state_topic: 'plugwise2py/state/energy/000D6F0002512345'
   unit_of_measurement: 'W'
   value_template: '{{ value_json.power }}'
   device_class: power
 - platform: mqtt
   name: appliance
   unique_id: '12345.cum_energy'
   state_topic: 'plugwise2py/state/energy/000D6F0002512345'
   unit_of_measurement: 'kWh'
   value_template: '{{ value_json.cum_energy | float * 0.001 | round(2) }}'
   device_class: energy

however while I’d like to get entities like the descriptive and unique:

  1. sensor.12345.power and
  2. sensor.12345.cum_energy,

they show up as

  1. sensor.appliance and
  2. sensor.appliance_2.

This is both not very unique (what if I want to use the sensor for another device?) and non-deterministic (how do I know which is the first and which is _2?).

How can I get entity_id's based on unique_id? Should I simply use name? If so, how do I expose these with a human-readable name in HA?

Also as bonus: can I still group these sensors in a device to show up in the device registry, even if they are not discovered via MQTT?

Hope someone can help :slight_smile:

The entity_id is derived from the name, normally.
Not sure how you get “kitchen” rather than “appliance”, tbh

If you do this:

name: '12345.power'

Or this:

name: '12345 power'

The resulting sensor will be this because Home Assistant doesn’t allow the use of periods, spaces, and certain other characters in the entity’s object_id (i.e. its name).

sensor.12345_power

Thanks. @koying: sorry that was a typo (I removed the actual name).

If entity_id is based on the name field, can I set a friendly name somewhere else?

The name option serves as the entity’s friendly_name and serves as the basis for what Home Assistant will use to create the entity’s object_id.

If you use this:

name: 'My Favorite Sensor'

You will get sensor.my_favorite_sensor whose friendly_name is My Favorite Sensor. The name option serves two purposes and you can’t control them individually.

For more information, refer to MQTT Sensor.

That’s not entirely true. If the entity has an unique_id, as in this case, the friendly name and the entity_id can be overridden through the GUI:

1 Like

Thank you for spotting the error in my statement.

I should have been more precise and explained you cannot control them individually via the sensor’s configuration. There’s nothing in an MQTT Sensor’s configuration that permits one to define them separately. However, they can be customized afterwards (not just MQTT Sensor but entities in general).

In fact, this is how one must handle the new-style Template Sensors (in the template domain) when one wants precise control over object_id, friendly_name, and unique_id (because the configuration’s name option is, by default, used to generate the friendly_name).

Thanks @123 and @koying . My approach is now:

  1. Same predictable & unique name and unique_id (e.g. plugwise_<MAC>_power)
  2. Customise the name afterwards for usability (e.g. ‘oven’).

This allows me to have predictable and unique entity_id and friendly names I can understand.

There’s not much need to keep an entity’s name and unique_id the same.

The unique_id's purpose is for entity management by Home Assistant and isn’t displayed in the UI and isn’t accessible to automations, scripts, etc. For integrations that automatically create entities, each entity’s unique_id is automatically generated as well and is typically a long-winded string of randomly-selected numbers and letters. It’s used in Home Assistant’s entity registry where its appearance/legibility isn’t relevant.

Unless you must have the entity’s object_id be different from its friendly_name, using name alone is sufficient and requires no manual modifications afterwards.

Thanks @123 , I think I understand. However:

  • name: used as entity_id (for MQTT), I want it unique so I use plugwise_<MAC>_power, then rename & assign room in HA GUI
  • unique_id: required to be able to edit MQTT sensors in HA. For simplicity I use the same as name.

That’s understandable; you want the sensor’s object_id (which is the ‘name’ part in an entity_id) to be different from its friendly_name. That cannot be done via an MQTT Sensor’s configuration but through Customization which is possible via the UI or YAML.

In addition, the Lovelace UI gives you the option to display an entity, within a card, using yet another name of your choosing (different from its friendly_name).

1 Like

@deet use object_id in your configuration where you used unique_id before.