Naming hell: please clear this out: name, friendly_name, unique_id, display_name

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”)

TEST 2 (“unique_id” and “friendly_name”)

TEST 3 (“name” and “friendly_name”)

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

I have struggled with these concepts for a while. What I learned after a lot of reading and YouTube is the following:

  • You can not configure the “entity_id” from YAML.
  • when you create a new template sensor the entity_id is derived from “name”. This happens only once.
  • add a “unique_id” to each template sensor and never change it again. I use a website like uuidgenerator.
  • This “unique_id” is not visible anywhere in the user interface.
  • you can go in the user interface and change the entity_id from the default if you need to.

With this approach it works repeatable. I still do not quite understand why this is designed in this way. Maybe one of the experts can explain the rationale of this matter with unique_id vs entity_id.

2 Likes