Rename of sensor: loss of history

After a rename of a (temperature) sensor to a more friendly name, all history associated with that sensor is lost. Is the history data really deleted or is the name of that sensor just not updated in that database. If the latter, shouldn’t this be considered a bug? Is it possible to rename the sensor in the history database through some script?

4 Likes

I have seen this behaviour too. But I don’t know the answer to your question.

Hey - any news here?
I have the same “Problem”.

I can change the friendly Name - all okay. But if i change the entity_id, the data history was losing.

Is there way to change the entity_id without lost the history?

Regards
Herbert

Changing the entity ID wipes out history, that’s not a bug. You can submit a feature request to preserve it on entity ID change though.

At least for entities where the entity ID can be changed through the UI. It should be possible to update history with a script so its not lost when the entity ID changes in the UI.

For entities where the entity ID cannot be changed in the UI (edit pop-up says This entity ('<entity ID>') does not have a unique ID, therefore its settings cannot be managed from the UI.) nothing can be done about those. The entity id is the only identifier in that case, when it changes there’s no way to know what it used to be. History will be lost for those if the entity ID changes.

1 Like

I will file a bug about this. I found orphan historical records in the states table for an entity that did not exist anymore. If the behavior is truly to wipe out the history when renaming an entity, then the database should get cleaned up.

But I’m voting for simply updating the database with the new entity name.

Here’s the sqlite command (using SQLite web) I ran to recover the history:

update states set entity_id = 'sensor.f2_master_bathroom_humidity' where entity_id = 'sensor.f2_master_bedroom_sensor6_humidity';
2 Likes

Under the current design, it’s not really possible for the db to clean up. History is tied to the entity ID. Changing the entity ID in the UI does not modify the existing data, it simply becomes orphaned.

Orphaned history is indistinguishable from history for an entity that hasn’t popped into existence yet. For the many, many integrations that do not yet register their entities in the entity registry, HA has no idea what entities are still coming. Integrations aren’t required to create all their entities at startup, they can create entities on the fly when new data comes in that warrants it. There’s also a POST /api/states REST API that allows someone to post state data with any entity id at any time so new entities could pop into existence whenever. When they do you would see any history in the db tied to that entity id in the UI.

Basically if HA used the current list of entities you see in states and cleaned up any history for entities that weren’t in there it could clear out totally valid history for entities that would re-appear eventually. So instead it leaves it until it is cleaned out for being too old based on purge_keep_days.

Now you could say “HA should migrate history when I change the entity id in the UI”. As that’s the point before it becomes orphaned when it’s possible to do something about it. I would agree with you. However that’s a Feature Requests not a bug as it requires an enhancement (migrate history when entity id is changed). Remember history existed before it was possible to change entity IDs in the UI, that came later.

Pretty sure this wth is requesting exactly this (among other things):

Wow. Thanks for all this context. I’ve learned quite a bit in the last 24h. @kulmegil mentioned on Discord that Long Term Statistics are actually updated when entity_id is changed (since v2022.10). I didn’t even realize there was such a thing as LTS. I confirmed that when I use a stats graph vs history graph, I see a lot more data.

So based on all that info, I feel like I should simply switch to using Stats graphs instead of History graphs (when the sensor data supports LTS) for a more robust experience.

Just to be clear, I was arguing for the narrow case of “when a user uses the UI to rename an entity ID, the action should not leave a trail of orphaned states record”. Is it a feature request or a bug? If I was the programmer implementing the UI feature, I would have made sure the states table got updated. Can we settle on "incomplete feature: ? :grinning:

And thanks for pointing out to the WTH topic on refactoring. That would totally solve the problem and voted for it.

Hello,
How do you find orphan records ?
I have the issue that many off my records disapeared on the 24th of october.
although as they are increasing sensor (I guess it is the reason) the total is still ok.

  • Instant value are cumulated in utility meter (daily and monthly)
  • monthly is ok

but the problem appear on both daily and monthly history.

And I have no idea why, I did quite a lot of changes lately, so I would like to check if i can recover.

I did a clean up on the statistic tab on HA that told me “no record …blabla, do you want to delete” and I said yes for some, did that deleted the records from the DB ?

my problem seem strange to me (but I not very advanced on how HA work)

but statistics show data prior to that

the utility meters were not reseted in the meantime.

Any idea what could have happened ?
Thx a lot

Coming soon in Retain history when renaming an entity by bdraco · Pull Request #89963 · home-assistant/core · GitHub

1 Like

Is there a way that this works with a template entity where the end goal is to change the template name, not just have it renamed in the UI.

I’ve got a couple devices that I want to re-pair into zigbee, but I don’t want to lose their history, is it possible to retain the history of the device after deleting the old instance and re-pairing it? The same would be really useful if for example some sensor gets broken, we replace it, but I want to keep the history temperature of that room

1 Like

Hello all,

Sorry to bump this subject but i’ve searched for answers and found nothing of interest appart from this discussion.

I’ve stored 2 years of datas for many sensors, batterypower, batteryamp, solarpower, inverterpower … you got the idea.
Since i’ve changed a device in my system, many identities have gone and many have shown up.

I’m force to edit queries in grafana to include both data
SELECT mean(“value”) FROM “autogen”.“W” WHERE (“entity_id” = ‘smart_meter_5kva_power’ OR “entity_id” = ‘battery_power’ ) AND $timeFilter GROUP BY time($TimeInterval) fill(0)

My question is : can i update the database with something like
Update “autogen”.“W” set “entity_id” = ‘smart_meter_5kva_power’ where “entity_id” = ‘battery_power’