Rename entity fails with Entity with this ID is already registered

I have a device that had to be replaced (Powerwall inverter).

I want the new device to have the same entity ids as the old ones, so all my automations, scripts, and custom entities keep working (some written in YAML).

e.g. I have binary_sensor.grid_status which changes when the grid goes on/off.

I renamed the old entry binary_sensor.grid_status_old

Now I am trying to rename the new entry binary_sensor.grid_status but I get the error: Entity with this ID is already registered. This is extremely frustrating, since I just renamed the “existing” entity to something else. The name is now available, and I can’t use it.

I also can’t change the old entity back to it’s original name. I get the same error.

How do I get out of this trap?

And did you restart HA after renaming the sensor to _old? And then rename the new one?

Change the old entity back to its original name.

Delete it.

Restart.

Look in Settings → Devices & Services → Entities list to make sure the old entity is gone. If not, delete it again from there.

Rename the new entity.

You shouldn’t lose any history but make a backup before doing this just to be sure.

When I try to change the name back I get an error: Entity with this ID is already registered.

Any way to clear out the phantom entity?

How attached are you to the history of this entity?

Well the whole point of keeping the name is keeping the history…

What I don’t understand is why a new phantom entity is created (with the original name) when I rename an entity, making it impossible to change back? What is the phantom entity?

I can’t do anything with it:

I should point out, I’m perfectly happy to go and run a SQL script (or edit a YAML or JSON file by hand) to fix this if it can’t be done in the UI. Just not familiar enough with how the schema interacts with the entity registry to figure it out myself.

@tom_l - any further clues?

So I removed this entity from a dashboard it was in, and deleted it from the homeassistant.exposed_entities JSON file, and restarted but it was still showing in the entities list as unmanageable.

Later I checked again on the Android app and it was gone, so I could rename the old entity back.

It’s not clear which thing helped here, but I take it that renaming an entity who’s name you want to keep is not advised - better to delete the existing one it seems?