Replace entities and keep history (BREAKING CHANGE starting HA Core 2023.4)

I really think this needs more attention. So I deciced to make a dedicated topic out of the release notes blog post at 2023.4: Custom template macros, and many more new entity dialogs! - #621 by e-raser.


According to 2023.4: Custom template macros, and many more new entity dialogs! - Home Assistant

  • Home Assistant now keeps history when renaming entities :metal:

Before:

  1. sensor.entity with statistics
  2. Renamed sensor.entity to sensor.entity_old
  3. Created a new sensor.entity using the same name
  4. The new sensor.entity will have the full history (states and statistics) of the old sensor.entity

Now: doing the same results in a complete new history (states and statistics).

How to rename entities to actually preserve the old states + statistics?

It seems to be not possible anymore for an entity to inherit the history of another entity just based on the entity_id. I really need to solve this, otherwise this would be an actual step back… the entity_id swap workaround worked just perfectly before 2023.4!

When reading this first I thought that’s exactly what this new feature does - instead after trying with a few entities, I lost all the history data… :cry:


Edit: I think the core issue is when the old entity has it’s own unique_id set. One simply can not transfer unique_id’s to another entity, right?


Currently instead of a rocking

  • Home Assistant now keeps history when renaming entities :metal:

I tend to say the opposite is right: now we are not able to swap entities and keep their history. So that “fancy new feature” comes with a HUGE downside :warning:
Not possible without very very VERY ugly and dangerous database hacks I guess - really not an option.

Please my fellow HA enthusiasts, prove me wrong. How to achieve this now > HA Core 2023.4?

1 Like

Edit: found this unsolved topic targeting the same topic/issue:

Looking forward to the solution as well. Yesterday one of my powerplugs died and I want to replace it with a new one, but keep the history of the old powerplug since it is integrated in my energy dashboard.

The new powerplug is ready to be adopted in Home Assistant, but without the steps to keep the history, I am a little hesitant to proceed.

Current status: After HA Core 2023.4 it is still possible to swap entities AND keep the history of the old (replaced) entity.

According to How to replace entity in Energy Dashboard by new one without losing history - #2 by petro the process now with HA Core >= 2023.4 is:

1. Delete old entity
2. Rename new entity to old entity_id
3. New entity will inherit old entities history

Do not under any circumstances rename the old entity before you delete it, this will cause the history to follow the renamed name.

There’ll also be some errors logged to the HA log which seems to be “normal” for this kind of database and entity registry operation.


Another guide which seems to work fine according to Replacing device but keeping entity_id/long term statistics in HA - #12 by mekaneck can be found (via Replacing device but keeping entity_id/long term statistics in HA - #10 by mekaneck) at HA Energy Dashboard FAQ · GitHub

It seems way more complicated as it involves restarting HA, which shouldn’t be necessary (anymore with HA Core >= 2023.4).


For all further information I strongly recommend to look at this topic where few open questions should be answered shortly:

3 Likes

I tried to reproduce the sequence the suggested sequence and and failed at first, but then succeeded. Here is a report what happened. I hope it can be useful for someone. I didn’t find exact detailed description in the forum.

I have an ESPHome device with a sensor. I need to replace this device and retain history and statistics of the sensor.

Steps

  • Entity to retain sensor.esphome_original contains history and statistics for the past
  • Turn off the old device
  • Connect the sensor to the new device
  • Flash the new device
  • New sensor gets name sensor.esphome_new
  • (here were steps from wrong way, see below)
  • Make a backup for Home assistant
  • Make a copy of /config/.storage/core.entity_registry
  • Open /config/.storage/core.entity_registry in Studio Code Server
  • Find "entity_id":"sensor.esphome_original"
  • Replace it to "entity_id":"sensor.esphome_old_dummy"this is considered a dangerous operation, be careful to not accidentally corrupt something else. This is also a critical step in the whole sequence.
  • Restart HA
  • Now history and statistics is not available for esphome_old_dummy
  • Rename esphome_new to esphome_original
  • History and statistics is available for esphome_original before turning off the old device
  • Any history collected for esphome_new for the time before renaming is lost
  • Outcome - esphome_original is on the new device up and running with history and statistics retained
  • Side comment - esphome_old_dummy is still available on the old device, but without any history and statistics and probably will start collecting them if start using the old device again

Steps succeeded thanks to the comment: How to replace entity in Energy Dashboard by new one without losing history - #15 by Nerdix

Wrong way

  • Settings → Device & services → Entities
    • Status of old entry is “Unavailable” - exclamation mark in red circle
    • Enter selection mode → select
    • Three dots menu → Delete selected
    • Dialog → Confirm
      • Delete selected entities?
      • You can only delete 0 of the 1 entities. The others require the integration to stop providing them, and sometimes a Home Assistant restart is needed. Are you sure you want to delete the deletable entities? Remove them from your dashboard and automations if they include these entities.
    • Item stays
  • Rename - error “Entity with this ID is already registered”

Additional information

Home assistant versions:

  • Core 2025.2.4
  • Supervisor 2025.02.1
  • Operating System 14.2
  • Frontend 20250214.0

Good luck!