Template sensor unique_id is ignored

Hello,
I am trying to set a unique_id for my template sensor, but it is ignored by HA. I expect the entity ID to be ble_battery_fa748e71106b, but it is sensor.ble_battery_monstera_deliciosa instead.

template:
  - sensor:
      ...
  - sensor:
      ...
  - sensor:
      - unique_id: ble_battery_fa748e71106b
        name: "ble battery Monstera Deliciosa"
        unit_of_measurement: "%"
        state_class: measurement
        device_class: battery
        state: >
          {% if is_number(states('sensor.ble_voltage_fa748e71106b')) %}
            {{ max([min([((states('sensor.ble_voltage_fa748e71106b') | float - 2.0) / 1.2) * 100.0, 100.0]), 0.0]) | round(1) }}
          {% else %}
            'unavailable'
          {% endif %}

I tried removing the name: "ble battery Monstera Deliciosa" part, but even then the ID is sensor.template_ble_battery_fa748e71106b :exploding_head:
Can anyone tell me what am I doing wrong? Thanks!

Unique ID is not entity_id. (yes I know. There’s many threads on the site about why we need another unique ID in top of what was once supposed to be a unique ID - just go with it)

The entity ID for template sensors currently appear to be based on the name. (slugified if you have spaces. “My super Device” becomes [device domain]. my_super_device) so set the name: field to be what you want and the entity ID will auto-generate based on that.

Adding unique_id will then allow you to further customize the template sensor in the UI (which I believe includes further editing the entity ID of you wish)

I don’t remember being able to specify the entity ID in the template but would be happy to be wrong… That probably would require a feature request

2 Likes

That makes sense, thank you very much for the detailed explanation. I just edited the entity ID in the UI to match the unique_id I wanted - not ideal, but it works.