Why do my entity names end with "_2", "_3" etc. and how do I fix it?

I have some switches that are named e.g. “Dining Switch”, but the entity names have a numeric suffix, such as light.dining_switch_light_2:

image

And strangely, some of my devices even have a mix of entities with and without the numeric suffix:

I am guessing this “_2” suffix may have appeared when I removed, then re-added the device to the zigbee network. But how do I make it go away?

I tried renaming the device, for example “Dining Switch” → “Foo”. It helpfully offers to rename the device’s entities as well, and then I end up with an entity named light.foo_light_2 :frowning:

Click the ⓘ icon next to the name. Click the Settings cog icon at the top of the pop-up. Change the entity id by removing the underscore and number than click UPDATE.

Do I have to do this for every entity individually? There are dozens of them — it will be a very painstaking process. And will editing this disable (or be later overridden by) the automatic entity renaming that will happen if I choose to rename the device in the future?

Yes you do.

I’ve been ranting about this for a couple of years. When I ask WHY?, I get “just in case someone accidentally uses the same device name twice”. Or some nonsense answer along those lines.

It’s a real pain to have to fix all of the broken automations and scripts that _2 can cause.

I don’t even understand what situation this would be referring to (maybe that’s why you called it nonsense!). When an entity name is used once, does that take it permanently out of the running for future automatically-created entities? If I am allowed to manually rename an entity to delete the _2, then that would seem to mean the un-suffixed name is not considered reserved/taken, so why wouldn’t the automatic naming be able to use it as well? Why does this only happen for some devices and not others? And why does renaming the device rename the entities as well but also keep including the _2 (even if the new automatic entity names have never been used before)? So many mysteries here…

That’s true. However the reason the entity’s entity_id ends with _2 is because, at least in theory, at the moment the underlying integration (Zigbee) created that entity, there existed a record of an active entity with the same entity_id. FWIW, there are files in a hidden folder containing registries of integrations, devices, entities, etc.

If you can now successfully remove the _2 then it means the other entity no longer exists in the registry. Or at it doesn’t exist as an active entity because the registry does keep track of deleted entities (I think; I’ll have to check EDIT yes it does).

Anyway, that’s my understanding of how the automatic production of an entity_id works. Its actual operation might differ.

Thanks for the details. Where can I find more about how this registry works (where is the folder you mentioned)? I wonder if I should be cleaning up old entries in the registry manually as well.

There might be something in the Developer documentation but there’s nothing in the User documentation because those files are not meant to be modified manually. An editing error is highly likely to prevent Home Assistant from starting.

The files are in a hidden directory named .storage located in the config directory. Feel free to examine them (contents are in JSON format) to familiarize yourself with the type and breadth of data that is recorded. However I don’t recommend you alter anything unless you have a good understanding of the data’s structure and at least one recent backup of the file. Be advised that the files contain cross-references. In other words, an entity’s information in the entity registry file will contain an identifier that refers to a device whose data is recorded in the device registry file.

Interestingly, I don’t see any record of the un-suffixed entity name in .storage at all:

I’ve just realized that one of my devices actually ended up with both an un-suffixed and a _2 entity for the same parameter:

How would I go about fixing this? The “Delete” button for both of them is grayed out. I already tried restarting the server.

The other possibility is that the ZHA integration had (or still has) an insidious bug and you’re seeing its handiwork.

I can’t say for sure if ZHA itself is the source of the issue, but yes it certainly seems like there is a bug somewhere (at least a race condition). I wish it were easier to give zigbee2mqtt a try without setting up the devices from scratch, since I’ve heard some folks prefer it, but maybe I will have to bite the bullet and see if it is more reliable.

The best way to test is to get a second coordinator and run both ZHA and z2m side by side. Then you can move individual devices between the two to test without having to do everything all at once.

FWIW, I’m a novice user of Zigbee2MQTT. I invite expert users to weigh in on my comments below.

Each paired device is added to Zigbee2MQTT, and Home Assistant, using the device’s unique internal address (a long hex string). In other words, a paired switch may be identified as 0xf1ab5ce7ff3d. I then use Zigbee2MQTT’s dashboard to change 0xf1ab5ce7ff3d to “Kitchen Cabinets” and instruct it to pass the new name to Home Assistant where it becomes switch.kitchen_cabinets.

So far, I have not accidentally, or deliberately, chosen an existing entity name so I don’t know the outcome. In other words, if switch.kitchen_cabinets already exists then perhaps the duplicate I create in Zigbee2MQTT will be automatically renamed (by Home Assistant) to switch.kitchen_cabinets_2.