Easiest way to remove MQTT device?

I’m pulling my hair out! I’ve spent 10+ hours trying to figure out how to remove a device from Mosquitto broker and Hassio. I’ve gone through all related topic on this forum, reddit and github.

Firstly, I did zigbee2mqtt/bridge/config/remove, but removing the device from Zigbee2mqtt alone doesn’t remove it from hassio’s entity list. So I removed it from core.entity_registry, /share/zigbee2mqtt/database.db and /share/zigbee2mqtt/devices.yaml. After that, I removed all related topic via MQTT Explorer which both didn’t remove it from hassio’s entitly entry after restarting add-ons and hassio. The trick that worked was uninstall and install Mosquitto broker, but that resulted in having to rename all the devices again. So I suspected the data was stored some where in the Mosquitto container, but the only thing I found was auth-plug.so.

Is there any easier way to simply remove a device? Renaming over 60 devices is not fun.

4 Likes

Yes.

Home Assistant uses MQTT Discovery to automatically find and register a device.

Once you understand how MQTT Discovery works, you’ll also understand why you had so much trouble getting rid of the device. The principle is very simple. If I publish this ‘discovery’ payload:

'{"name": "garden", "device_class": "motion", "state_topic": "homeassistant/binary_sensor/garden/state"}'

to this topic:

homeassistant/binary_sensor/garden/config

then Home Assistant will automatically create a new entity (in the entity_registry) called binary_sensor.garden and consider it to be a motion detector. It’s that simple.

Here’s the part that can make it very hard to get rid of the new entity. You follow all the normal procedures to delete an entity yet it always gets ‘re-discovered’. Why? Because when its ‘discovery’ payload was published, it was published as a retained message. That’s the part where you guessed correctly that, somehow, mosquitto was storing it. That’s the very definition of a ‘retained message’, namely it’s a message (payload) that is retained (stored) by the MQTT broker (mosquitto).

The big ‘nuclear option’ is to re-install mosquitto and the small ‘nuclear option’ is to delete mosquitto’s database (where messages are stored). Both options are excessive. All that’s needed is to simply delete the retained ‘discovery’ message.

Rather than advocate the use of the traditional method of purging a retained message (publish an empty retained message to the topic) simply use MQTT Explorer to do it. It provides the advantage of showing you all the topics and messages being handled by your mosquitto broker, including which ones have retained messages (and allow you to delete the retained message).

Going back to my example, if I delete the retained message I had originally published to this topic:

homeassistant/binary_sensor/garden/config

then Home Assistant will no longer “re-discover” binary_sensor.garden. In fact, if I remember correctly, it will also automatically remove the existing binary_sensor.garden.


EDIT
If you don’t want to install a third-party MQTT client like MQTT Explorer (but I do recommend it), you can purge a retained message from within Home Assistant using the mqtt.publish service (thank you to @francisp for the suggestion).

Screenshot from 2020-08-07 09-29-53

15 Likes

I can’t thank you enough, following your suggestion, I was able to remove the device from both mqtt and hassio’s entity list. I must have done something wrong when I first tried to remove the entity from MQTT explorer. The only place it still have a presence is in the mqtt intergration page, but I can live with that. I’ll make a simple guide for novice users like myself.

I’d like to suggest using MQTT Explorer, to me it’s a lot less fiddly. Click the entity, click the trash icon, done.

5 Likes

Yes, that’s what I used.

I have created a guide for novice users like me here: [Guide] How to remove MQTT entity from Zigbee2mqtt, Mosquitto broker and Home Assisntant

1 Like

Sadly doesn´t work for me, I´ve deleted all related topics with MQTT Explorer (including the retained ones, of course), but although they disappear from explorer, I still see them in integrations.
Any other advice?
Thanks

I reported it as an Issue for 0.106.4

The Issue was closed by PR 32693 that (allegedly) fixed the problem. I have not yet confirmed it using the latest version (0.107.X).

Which version of Home Assistant are you using?

I´ve tried in 0.107.7

Yup, I just confirmed it still fails to work properly with 0.107.6.

Just opened a new Issue: https://github.com/home-assistant/core/issues/33286

very good works for me delete the topic homeassistant/…/config

The problem was fixed in 0.108.0.

Thank you! I didn’t know about MQTT Explorer before and your solution really helps.

1 Like

Hi All, this is the easiest way to remove MQTT devices.

  1. no need to delete any entities
  2. no need to use MQTT explorer.
  3. no need to install addon
  4. no need to restart and reboot any addon.
  5. they will be gone forever.

I learn many things from here, just give back a little…hope it helps someone…
Lastly, please install Zigbee2MQTT Assistant from the repository, it provides many tools.

2 Likes

Your example assumes one is using zigbee2mqtt; it relies on a service (force_remove) provided by zigbee2mqtt.

The basic principle is:

Delete the retained discovery message residing on the broker.

The traditional method to delete any retained message is:

Publish an empty retained message to the topic.

That’s it, that’s all.

Any MQTT client is capable of doing that, provided it supports publishing retained messages. I suggested using MQTT Explorer but it can also be done from the command-line using mosquitto_pub. For example:

mosquitto_pub -h 192.168.0.XXX -t your/topic -r -n

or if the broker requires authentication:

mosquitto_pub -h 192.168.0.XXX -t your/topic -r -n -u your_username -P your_password

In fact, if Home Assistant’s “Publish a packet” (which is an MQTT client) offered the ability to publish retained messages, it could be used as well (except it currently doesn’t offer it).

Screenshot from 2020-08-07 08-33-20


EDIT

Added a Feature Request:

Hi, @123 Taras,
Thank you for your detail explanation. You are right that I am refering to Zigbee2MQTT. Sorry that I am quite new in HA and I am just at beginner level. I have tried many things that the community suggested, but most of them does not work for me.

I have also tried MQTT Explorer, but after deleting all the retain message, and also deleted everything related to the concern device, but once I restart the Zigbee2MQTT Addon, the device re-appear again in the MQTT discoverer…(I did restart the HA after deletion, the device is gone for a moment, until I restart the Z2M)

I also tried other method like publish a code to remove device, but error occurs, it said that the concern device needed to be available. But I have already remove the device and connected it to Tuya Hub. So this method fail too…

So from the trial and error and with so many helps from this community, I thought I could share my encounter and hopefully some newbie like me can find it useful.

Thanks for introducing the MQTT Explorer, I am using it now, to understand what is going on behind the scene.

The discovery topics will re-appear if something, like Zigbee2MQTT, re-publishes payloads to them. An MQTT topic, once deleted, will stay deleted unless something re-publishes a payload to it (notably as a retained message).

It’s not complicated:

If you delete a topic and it returns then you either failed to delete it correctly or something has published a new payload to that topic.

It’s as simple as that.

As long as the device is known to zigbee2mqtt, when zigbee2mqtt restarts it will send a new discovery message. So using mqtt explorer to delete the message alone doesn’t help.

Has anyone experienced issues connecting from MQTT explorer to the Mosquitto Hassio addon?

I am trying to connect to mqtt://192.160.0.x:1883 with username and password of a Hassio user (which Mosquitto uses, I understand). I have validate certificate and encryption off. I am using a Windows machine on the same network as my Mosquitto broker.

I need to remove dozens of devices and entities from IOT Link so I would rather not have to unpublish the topics one by one.

Sorry for reviving the thread, but this feels like the best place to put my issue.

Just removed my IOT devices (they were unreliable after all).
If you have a recent HA version : Simply go to configuration -> integrations. Under MQTT, click devices. Click on your device, select delete.

Thanks Francis, I’ll try this.

I also figured out why I couldn’t connect using MQTT explorer. Mosquitto was configured with a broker hostname instead of an IP, so it couldn’t be resolved by the MQTT explorer on my Windows machine.