I want to create some entities in a template in yaml.
In order to have a grip on these entities, I want to keep a number of things under control:
- “Entity ID” (the internal name how I can address this template)
- displayname (the way the entity is showed to the user in the UI)
If you define an entity in a template, it will get an:
- “Entity ID”
based on some parameters.
However, now it’s confusing how this system works…
In the template definition, you can define the entity with:
- “name”
- “unique_id”
- “friendly_name”
and besides that, in the UI, you can overrule the “name” by another “name”. I will call this last one
- “display_name”
With all this options, I can NOT accomplish the simple task of defining an entity with a nice “display_name” and with my own “Entity ID”
See i.e.:
template:
- sensor:
# ------------------------------------------------------------------------
# name only
# ------------------------------------------------------------------------
- name: "nameconvention name (11)"
icon: mdi:flash
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
state: >
{{ states('sensor.p1_meter_total_power_import_t1') | float }}
availability: >
{{ states('sensor.p1_meter_total_power_import_t1') not in ['unknown', 'unavailable', 'none'] }}
# ------------------------------------------------------------------------
# unique_id only
# ------------------------------------------------------------------------
- unique_id: "nameconvention_id_12"
icon: mdi:flash
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
state: >
{{ states('sensor.p1_meter_total_power_import_t1') | float }}
availability: >
{{ states('sensor.p1_meter_total_power_import_t1') not in ['unknown', 'unavailable', 'none'] }}
# ------------------------------------------------------------------------
# unique_id and name
# ------------------------------------------------------------------------
- name: "nameconvention name (13)"
unique_id: "nameconvention_id_13"
icon: mdi:flash
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
attributes:
friendly_name: "nameconvention friendly_name 13"
state: >
{{ states('sensor.p1_meter_total_power_import_t1') | float }}
availability: >
{{ states('sensor.p1_meter_total_power_import_t1') not in ['unknown', 'unavailable', 'none'] }}
# ------------------------------------------------------------------------
# unique_id and friendly_name
# ------------------------------------------------------------------------
- unique_id: "nameconvention_id_14"
icon: mdi:flash
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
attributes:
friendly_name: "nameconvention friendly_name 14"
state: >
{{ states('sensor.p1_meter_total_power_import_t1') | float }}
availability: >
{{ states('sensor.p1_meter_total_power_import_t1') not in ['unknown', 'unavailable', 'none'] }}
# ------------------------------------------------------------------------
# name and friendly_name
# ------------------------------------------------------------------------
- name: "nameconvention name (15)"
icon: mdi:flash
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
attributes:
friendly_name: "nameconvention friendly_name 15"
state: >
{{ states('sensor.p1_meter_total_power_import_t1') | float }}
availability: >
{{ states('sensor.p1_meter_total_power_import_t1') not in ['unknown', 'unavailable', 'none'] }}
# ------------------------------------------------------------------------
# name, unique_id and friendly_name
# ------------------------------------------------------------------------
- name: "nameconvention name (16)"
unique_id: "nameconvention_id_16"
icon: mdi:flash
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
attributes:
friendly_name: "nameconvention friendly_name 16"
state: >
{{ states('sensor.p1_meter_total_power_import_t1') | float }}
availability: >
{{ states('sensor.p1_meter_total_power_import_t1') not in ['unknown', 'unavailable', 'none'] }}
# ------------------------------------------------------------------------
### TEST 1 ("name", "unique_id" and "friendly_name")
# ------------------------------------------------------------------------
- name: "nameconvention (TEST) with some §§§§§trange characters"
unique_id: "nameconvention_id_857896561"
icon: mdi:flash
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
attributes:
friendly_name: "nameconvention with a very weird name, but readable friendly_name"
state: >
{{ states('sensor.p1_meter_total_power_import_t1') | float }}
availability: >
{{ states('sensor.p1_meter_total_power_import_t1') not in ['unknown', 'unavailable', 'none'] }}
# ------------------------------------------------------------------------
### TEST 2 ("unique_id" and "friendly_name")
# ------------------------------------------------------------------------
- unique_id: "nameconvention_id_857896562"
icon: mdi:flash
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
attributes:
friendly_name: "nameconvention with a very weird name, but readable friendly_name"
state: >
{{ states('sensor.p1_meter_total_power_import_t1') | float }}
availability: >
{{ states('sensor.p1_meter_total_power_import_t1') not in ['unknown', 'unavailable', 'none'] }}
# ------------------------------------------------------------------------
### TEST 3 ("name" and "friendly_name")
# ------------------------------------------------------------------------
- name: "nameconvention # ç @ $ §"
icon: mdi:flash
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
attributes:
friendly_name: "nameconvention with a very weird name, but readable friendly_name"
state: >
{{ states('sensor.p1_meter_total_power_import_t1') | float }}
This will result in:
Take a closer look at the last 3 tests:
TEST 1 (“name”, “unique_id” and “friendly_name”)
- the “Entity ID” is derived from the “name” and NOT from “unique_id”, thus unpredictable
- “friendly_name” does not work
TEST 2 (“unique_id” and “friendly_name”)
- the “Entity ID” is derived from the from “unique_id”, but doesn’t match exactly: the word “template_” as added
TEST 3 (“name” and “friendly_name”)
- the “Entity ID” is derived from the “name” and unpredictable
- “friendly_name” does not work
So, can you please make this more consistant.
My proposal:
Let “Entity ID” be exactly the same as [domain].“unique_id”
Thus if you create a sensor with
"unique_id" = "xxx"
,
the “Entity ID” will become
sensor.xxx
If there is no unique_id
defined, keep the current logic (“Entity ID” derived from “name”)
kind regards,
B
PS.
For the “friendly_name” not working in combination with “name”, I made an issue-report at
naming hell: friendly_name does not always work · Issue #84299 · home-assistant/core · GitHub