Some entity ids changing to <entity_id>_2 occasionally

I have a bit of a weird problem. Every now and then, some entities are renamed to <original_entity_id>_2. I usually catch it only a while later. For example I monitor the CO2 concentration in my bedroom and regulate the home ventilation based on this value. However, at some point, I found out that the regulation is not working anymore. After searching, I found that the original sensor.co2_sensor_bedroom had become unavailable and a new sensor called sensor.co2_sensor_bedroom_2 has been added.

The containment is to remove the previous entity, sometimes through changing files in .storage and to renaming the entity_2 back to the original name. However, this happens semi-regularly to various entities and I’m at a loss on how to find what’s happening.

In this specific case, the CO2 sensor is connected through ESPHome. However, more entities suffer from this issue, I don’t remember the integrations.
I don’t know if this happens randomly or only after a restart of HA or the entire machine.
I’m running HA Supervised on an Intel NUC that runs Debian.
It happens randomly, I think around every few months.

Has anyone suffered from this issue before and/or has a direction I can start looking in?

Thanks!

It happend to me while creating rest sensors without having an unique_id.

So every time I restarted HA, they created another entity with a “2” at the end and the original became unavailable. Same as what you describe.

Check the docs to see if you can add an unique_id to your sensor, so that it would not keep duplicating while a reboot/restart.

Let me know if it works for you.

https://community.home-assistant.io/t/creating-sensors-and-filtering-specific-json-value-from-curb-energy-api/568353/7?u=luisgarcia87

Thanks for the response. I’m not using rest sensors. I did search for ESPHome and unique IDs and found a few issues that mention that ESPHome does not generate unique IDs. So I wonder if this is fixable within ESPHome.