How to replace entity in Energy Dashboard by new one without losing history

You don’t, rename the entity_id and it will just work in the energy dashboard.

Did try that, but the energy dashboard was pointing to the old renamed sensor.

Then it seems like this work around no longer works.

Have you found an option to get this done?
I have to replace the entities tracking my photovoltaik system. I already have half a year of history and really don’t want to lose that.
Sadly I really have to do this, though, as my old entites no longer work as of today.

The old entities were created with templates. Is it possible to alter these templates, so that they just show the values of the new entites and this way continue the history?
If yes, then how would I do that?

Thanks in advance!

Can not find the topic, but ended up altering the short and long term statistics in the database. Now it works but you should make a backup before you begin.

Altering the templates may be an option too, just try after making a bachup first

For any other people looking into this, in the future:
Here is the topic that I solved this issue with:

Don’t miss the _cost and _consumption entities!

I ran into the same issue

Unfortunately

NO @petro, this does not seem to work anymore - at least not starting with HA Core 2023.4 … :frowning: :frowning: :frowning:

See edit on that post.

1 Like

Wow, very interesting!

Questions:

  1. I guess this is confirmed (multiple times) to work that way reliably, isn’t it?
  2. I thought with step 1 all the history is gone already… at least data from states table will be removed immediately, will it?
    Statistics of course remain in the database (mine collected a garbage of 53 entities already… :roll_eyes: ).
  3. Is it safe to rename the entity in step 4 (so once it already catched the history of the old one)?

Cross-post with a similar process (just with many more words):

No, it’ll stay.

@petro Thanks, tested this now and (of course) it seems to work, even I got this for all 4 entities swapped in the last minutes:

Cannot migrate history for entity_id `sensor.network_in_end0` to `sensor.network_in_eth0` because the new entity_id is already in use
Cannot migrate history for entity_id `sensor.network_out_end0` to `sensor.network_out_eth0` because the new entity_id is already in use
Cannot migrate history for entity_id `sensor.network_throughput_in_end0` to `sensor.network_throughput_in_eth0` because the new entity_id is already in use
Cannot migrate history for entity_id `sensor.network_throughput_out_end0` to `sensor.network_throughput_out_eth0` because the new entity_id is already in use

(I deleted the *eth0* entities and renamed the new *end0* entities to *eth0*)

I guess basically this message is saying “the history of your NEW (*end0*) entities is now overwritten by the history of the old (*eth0*) entities” - which is fine, so it’s “safe” to ignore those HA log errors. Right?


And what about this big important question?

Corner case in addition to that:
To be 100 % safe, shouldn’t step 4 be “delete statistics of entity which got overwritten in step now at /developer-tools/statistics”? Otherwise, what would happen if one would change the entity_id back to the entity_id it had prior to the renaming…? That’s very important to know actually.


Answered in parts by my own:

  1. Renaming is fine, history will follow.
  2. Corner case (using the entity_id which has been used before): history will stay, but the states shown now are wrong. One definitely :warning: looses the states table data :warning:, which is a) not an issue anyway after reaching the recorder keep_days setting or when using HA Core >= 2023.12 as it now simply shows the statistics for the time there are no state values (see 2023.12: Welcome home! - Home Assistant):

history:

states in history dashboard (HA Core < 2023.12):

One additional remark for completeness:

Entities which are provided by an integration cannot be deleted, but in this case it would still be possible:

  1. you might need to change the entity_id in the core.entity_registry, e.g. sensor.energy to sensor.energy_old
  2. restarting HA after that change will adjust the entities
  3. rename the new entity sensor.energy_new to the old entity_id sensor.energy
  4. history will be inherited then, old entity will be kept with adjusted id
  5. Old entity could be deactivated in the integration if not needed any longer

:warning: This is just an “emergency hack” and no recommendation, one should not mess with the entity_registry if not 100% confident.

3 Likes

That all sounds quite good, but does it still work if the meter readings are different?
I got a new water meter that starts at 0 again. That was already a problem without a new entity. Now I’m getting a new electricity meter (with a new entity). What happens if the last reading was 13282 kWh and the new meter starts at 0 again?

If it has a state class of total_increasing HA will handle it just fine (another reference).

I hope so, I’m a little bit nervous. Before I get my new electricity meter, I have to switch from a Tasmota „Volkszähler“ to a Tibber Pulse. Then I will get my new meter in a few weeks.
So I have to switch entities first and hope Homeassistant will do the rest when the meter is replaced.

Thanks for sharing!
This way worked me for the current version of HA:
OS Version: Home Assistant OS 11.4
Home Assistant Core: 2024.1.5

  The new process is:

    1. Delete old entity (Settings > Device&services > Entities) It was in "Restored" status. Select the checkbox at the begining of the row!
    2. Rename new entity to old entity_id -> click to the entity_id > settings > rename entity_id > update  
    3. New entity will inherit old entities history (no reboot needed, it is on the fly...)

IMPORTANT: Do not under any circumstances rename the old entity before you delete it, this will cause the history to follow the renamed name. (If you already renamed it, because you followed the earlier process, you have to rename it back, than reboot, than follow the new process above. It worked me this way too.)

@petro

I made a rather confusing discovery when trying to rename an entity of the automation domain.

Before renaming (automation.old_name):

After renaming (automation.new_name):

Because of this history loss I decided to revert the renaming (automation.new_nameautomation.old_name). History back immediately.

Where did the history go? Shouldn’t that be conserved?

In this case I don’t want to / can delete the automation itself (your step 1) before step 2.

So how to handle this with automations (likely the same with scripts)?

If it’s a yaml automation, changing the name will cause it to lose the history. If you add an id to the automation, changing the name via the UI will carry the history w/ the name change.

This is true for all yaml entities. No unique_id, no carry over.

Well, that’s the interesting part:

  • I created all my automations using the UI right from the beginning.
  • Checking automations.yaml, I can find the automation by alias, and it has an id.

That’s exactly not the way it happened for me.

So why doesn’t it work? Is an id in an automations.yaml not the same as an unique_id (as e. g. used for template entities)? Something’s different here…

Then you didn’t refresh the page after changing the entity_id. It’s a browser, when you change something like that, you have to refresh.