Rename entity ids and retain statistics

How can I rename entity IDs without losing historical statistics?
When i change the ID, all the statistics of this sensor are gone. I can undo the name change and the statistics are back. But i need another ID name.

1 Like

At the moment there is no way to do this (without some serious database manipulation). LTS are tied to the entity_id.

1 Like

I certainly did not know that. Thanks for the info.

I did a test on an entity I don’t particularly care about the history of, making sure to use a a never before used entity_id and it did appear to work.

Maybe that’s why this isn’t working then:

Shame it is no use for actually replacing a device/entity, as that will have existing LTS and states that need to be kept, and this is the most common use case encountered on the form.

1 Like

I don’t care that much about statistics (yet), so I do it for different reasons, but it could help here as well:
<< pointers / aliases / delegation >>.

What I mean: create a template sensor that is “purpose type” (like sensor.kitchen_thermometer). Point it at the real sensor (like sensor.sonoff_thermometer_number_13).

When you change the real thermometer (because it broke, or you moved it, or whatev) → you just change the definition of template sensor.

I’m certain you personally know you can do that, but I’m mentioning it for others.

Plus, it is kinda cumberstone to do right now → manually creating template sensors for each and every entity you wanna have a proper separation of concerns… Well, I hope I can fix that in the future :>

There are two ways that stuff can be linked via and entity id or via a guid. In the prior database schema entity id was the key; now there is a lookup table from entity_id to ID (int) and the ID is used as the key for the data. Note, this ID is not the unique_id of the entity from the registry. It a self incrementing integer identifier - such that the first entity will have id 1, second 2, etc.

The problem before was renaming the entity id would cause the loss of all recorder data (well it really wasn’t lost as it’s still in the database). With the lookup table this is no longer a problem.

As far as I can tell deleting the entity id does not cause the entity to be removed from the lookup table, so if you replace a device and use the same entity id it should not create a new guid and the old data will be linked in. I have not tested; this is based on examination of the schema and recorder code.

There’s a lot of evidence on this forum that this does not work.

1 Like

The subject seems complicated to me.
I currently have two applications:

  1. Case
    I want to rename an entity ID from entities I created myself in a yaml in ESPHome. This isn’t about the GUI without losing all the statistics. This is probably just about the way @AngellusMortis described (i guess).

EDIT: Small note: The sensors I read via Modbus and an ESP32 do not appear in the developer tools under statistics at all. So I can’t compile statistics. Or I don’t understand.

  1. Case
    I want to rename an entity ID that is provided by a helper. I can just do that through the GUI, the statistics will be preserved. BUT: If, for example, I have this statistics displayed in the dashboard via the mini-graph-card, then after the renaming, the only thing that comes out is bullshit.

Original statistic:

Original statistic in a mini-graph-card (grey bars):

Statistic in a mini-graph-card after the renaming (grey bars):

The result for me is that in both cases I can either rename the entities very complicated or not at all without fear of losing the statistics.

Ok, the reports of replacements I found not working were prior to 2024.3.1, so that was probably the issue.

Thanks for the method clarification. I’ve bookmarked it and will let others know when they ask. And they will ask. :slight_smile:

It seems this actually fails at the moment because no history or LTS are currently moved when you rename an entity, see: "New entity_id is already in use" error when renaming any entity · Issue #115747 · home-assistant/core · GitHub

1 Like