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?

I ended up here on a wild goose chase. So I’ll also help other people with a possible crumb…

I had a script, that had been renamed, deleted, recreated, and confused.

I tried to rename a new script to the name of an old script “script.wol_server” but would get an error message telling me this script already existed.

" Rename entity fails with Entity with this ID is already registered"

So was stuck with “script.wol_server_2” as the script name.

I chased through the scripts.yaml, deleted all traces of it from the various addons, destroyed, refreshed caches, privacy sessions, wiped out any mention of it… could not find the script anywhere under scripts or any sane place. And still I was stuck with “script.wol_server_2” as the name.

And then finally found where it was lurking.

Settings => Areas, Labels and Zones

I spotted my original Script Name was still listed under an Area!! Clicking on it lead to lots of confused errors and offers to migrate it. (Sorry, no screen shot). But at least I could finally delete the rogue script and free up the name to use again.

So if you have a rogue script (or other entity) that seems hidden, check your Areas to see it is listed and maybe you can delete it from there.