Duplicate switch entity ids - how to rectify?

Hi all,

I am new to Home Assistant and using it to automate our new hackerspace. While I’ve worked around most issues through research, I am missing something about a current problem where two switches out of 9 at some point decided to duplicate their entity ids, which leads to an unwanted default switch group being displayed in the HASS UI.

These switches are Tasmota flashed Sonoff S26 devices, the majority of which work correctly. An example of the States listing is shown below.

switch.power_switch_four on friendly_name: Power Switch Four icon: mdi:lightbulb
switch.power_switch_four_2 on friendly_name: Power Switch Four hidden: true

These entries refer to exactly the same device. Clicking on the icon to the left of the entity ID brings up the same data display.

The difference in the entries for Power Switch Four is that one shows that an icon is associated with it and the other does not. The duplicated one with the 2 suffix shows it is hidden because of the work-around in customization.yaml which hides the unwanted device entity id.

I am not happy about using this work-around without understanding why the duplication happens, how to prevent it and how to fix it more permanently than randomly adding in extra customization.yaml entries when these issues pop-up.

In the journey to discover the cause and solution, I have tried many variations like commenting out discovery, cleaning a duplicate entity id’s entries from the States table, turned discovery off and attempting to use the Tasmota SetOption19 variations (HA discovery on/off).

So my questions are:

  • Why do these device duplicates occur only for specific devices; and
  • how can you remove them permanently and not simply hide them?

Thanks in advance for any advice.

check you known_devices.yaml file, that’s probably where you’ll find the duplicate.
Remove that block and restart HA and it should go away

Thank you for your quick reply. I would love to use that approach but the system does not have a known_devices.yaml file in the configuration directory.

Any other thoughts?

What was your first ever HA version? If it was anything prior to 0.77.x, check your hidden files…
After that I believe it’s stored in .storage/core.entity_registry.
Make sure you backup the file first !!!

1 Like

say whaaaaaat? I have a .storage/core.entity_registry as you mentioned… as I’ve been using HA since well before 0.77.x. But… if you didn’t migrate up and just started at 0.77.x… you’ll have a totally different file name? Do you know why? That seems silly. I didn’t create core.entity_registry to begin with, HA did it for me. So… why not create the same file name no matter what? And if, at some version, core.entity_registry was the file name, and then it got changed to known_devices.yaml, why wouldn’t HA migrate the file over?

It’s the other way round. Was saved in known devices and since 0.77 is saved in .storage/core.entity_registry (not sure if known devices is still maintained)
Not sure what happened but if you have duplicate devices that’s where they’ll be saved. Remove the unwanted block(s) after backup and restart ha it should solve your problem

I used to have a file called “entity_registry.yaml”. Some upgrade (probably 0.77) moved it to “.storage/core.entity_registry”.

The file format also changed from YAML to JSON.

Thank you for your responses. We are running HA 0.75.3 in a docker container on Debian Linux 9.5. I will look inside the Docker container directories for these files. I am new to Docker and HA. Looks like I need to update the Docker instance to the latest HA release.

Any idea why the duplication may have occurred in the first place and are there any best practices for preventing it happening in future?

That’s weird. I still have a known_devices file after the update (currently 0.77.3).

Is the known_devices.yaml still a valid option in configuration.yaml for the latest release?

I don’t have the extra WiFi entries in configuration.yaml and I expect that is why I don’t see the known_devices.yaml file.

known_devices.yaml is for the device tracker platform. It’s different than the old entity_registry.yaml and is still a file in use in 0.77.x.

1 Like

I upgraded the HASS docker instance to .77.3 and like this thread (Entity_registry.yaml is gone as of version 0.77.1, as well as the GUI entity registry editor) suggests, the core.entity_registry file is now in the .storage directory under the HA configuration directory.

However, when I view this core.entity_registry file, it only contains two devices, both Wemo. Therefore, I am still no wiser as to:

  • how the duplication occurred in the first place; and
  • how to fix it.

Reading the forums it is clear that my problem is not unique and there are many frustrated HA users out there.

Any thoughts?

Ahhhh!

More than likely your switches are coming from MQTT discovery.

In the mqtt section of your config, you probably have discovery on. You likely have a retained MQTT message configuring two switches with the same name (or you have one switch defined in MQTT discovery and one switch defined directly in your config).

Using an MQTT tool of your choice, explore that parent topic. If you see any config entries that should not exist (or that are now defined directly in your HA config) you can delete them by publishing a null message to that same topic.

Alternately, you can delete ALL retained messages with the following commands (assuming you’re using mosquitto):

sudo service mosquitto stop
sudo rm /var/lib/mosquitto/mosquitto.db
sudo service mosquitto start
1 Like

Thank you @swiftlyfalling, I will take a look and let you know the results.

@swiftlyfalling, thank you again for your suggestion, which worked like a charm to remove the two rogue duplicate switch entries. While I have a software background, I am new to MQTT, HA and Tasmota. I will learn how to explore the MQTT database. While I used the ‘sledgehammer’ approach of deleting the mosquitto.db, I took a copy to review later.

Which tool do you use to explore the db?

MQTT.fx is a decent Java based client that should run just about anywhere.

On the command line, mosquitto_sub works well.

MQTTBox is a Chrome browser extension that isn’t the best, but gets the job done.

I hate to bring up an old topic but I am having the same issue and I am not sure how to remove the duplicate entries I have in my new HASSIO build.

I was able to resolve my issue by removing the MQTT broker and reinstalling it, that deleted all the auto discovered items.

I have the same problem. All my Tasmota switches are duplicated. That does not happens for other devices. I Tried uninstalling/reinstalling MQTT and deleting core.restore_state, but they are always duplicated.

And now, my friend also has same duplication problem with tasmotized switches
They don’t appear in HA MQTT autodiscovered devices under Integrations->MQTT