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.
however while I’d like to get entities like the descriptive and unique:
sensor.12345.power and
sensor.12345.cum_energy,
they show up as
sensor.appliance and
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?
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).
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.
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).
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.
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).