HA keeps changing my template sensor ids, adding suffixes

I’m not sure what causes this, but HA keeps renaming my template sensor entity names.
E.g. I have these sensors. I have removed the name from them as I want to control their entity names more specifically.

template:
  - sensor:
    - unique_id: stove_total_consumption
      unit_of_measurement: "kWh"
      state_class: total_increasing
      device_class: energy
      state: >
        {{ states('sensor.shelly_shem_3_c45bbe788d27_1_total_consumption')|float(0) +
          states('sensor.shelly_shem_3_c45bbe788d27_2_total_consumption')|float(0) }}
      icon: "mdi:stove"
    - unique_id: stove_power
      unit_of_measurement: "W"
      state_class: measurement
      device_class: power
      state: >
        {{ states('sensor.shelly_shem_3_c45bbe788d27_1_current_consumption')|int(0) +
          states('sensor.shelly_shem_3_c45bbe788d27_2_current_consumption')|int(0) }}
      icon: "mdi:stove"

Now, HA has decided to rename one of them and suffix it with “_2”:

Earlier, I’ve seen stale sensors with status “unavailable” that bears the expected entity id from unique_id, together with another sensor that has a numerical suffix. I have been able to delete the unavailable sensors and restart HA, thus getting the sensor to use it’s expected entity id again.

But later, perhaps after a restart, or something, some sensors have yet gotten suffixes added to them, but I no longer see the original sensors as unavailable anymore.

This behavior breaks a lot of other configurations so I’m pretty frustrated as of now. Not sure if it’s a defect or an expected behavior, so hoping for the community to help me shed some light on it.

I run HA 2023.8.0 in a docker container.

If you change from name to unique_id and you reload (not restart), it’s going to add a second sensor with an _2. This is because the first entity is in the system and when you reload, you’re creating a new one because the first did not have a unique_id.

Restart when you add a unique_id, keep the name. You won’t get an _2 and then you can rename it to whatever you want.

Thanks. That’s a scenario I can understand. That was probably my first mistake just to reload the template sensors, not restart HA entirely.

But I don’t have name anymore on the sensors. Should I add one just because? I set their friendly name in UI instead.

I have restarted HA and it still insists on creating the _2 on some sensors, even if there are no sensors without the prefix that I can find…
As in the example above. There is a sensor defined with a unique_id: stove_power, that shows up in HA as template_stove_power_2, but there is no template_stove_power in HA without the suffix. That’s what baffles me.

Not sure if I’ll mess things up even further, but I’ll try to suffix all the sensors with, e.g. an _X. Then restart and see if I can identify and remove any stale sensors without the _X, then rename them back to what I want them to have and restart again…

it’s pointless to add a name at this point after adding unique_id because the device is now in the entity registry. Yaml changes won’t update the name. Just edit the names & entity_id’s in the UI and note this in the future. When creating new entities, just supply both a unique_id and a name. Your entity_id will be a slugified version of the name and you can edit it in the UI.

Ok. I really don’t care about the name thb. I really need the id to be correct and consistent though, as all references depend on it.

Yes, then in the UI, change the entity_id to what you want it to be. Changing yaml at this point will not adjust the current entity_id on your sensor because it’s in the entity registry. This is the downside of adding a unique_id, you are now forced to use the UI do make changes.

1 Like

Ah, thanks. It was just too simple, just changing the entity id in UI, once I got rid of the stale sensors. I’ve marked that reply as solution.

1 Like