Easiest way to remove MQTT device?

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.

Just for reference,
The following perl script will purge all messages from a topic and sub topics below it. You need to install mosquitto_sub and mosquitto_pub first, then run the script as mqtt_purge.pl [mqtt_ip] [user] [password] [topic_top]

It works by pulling retained messages one by one, waiting up to 10 seconds for each one. If it times out, it stops, otherwise, it parses the message, grabs the topic from it, and sends a NULL message. Then the next message is pulled and so on.

mqtt_purge.pl


#!/bin/env perl                                                                                                    
                                                                                                                              
my $mqtt_server = $ARGV[0];                                                                                                   
my $USER = $ARGV[1];                                                                                                          
my $PASS = $ARGV[2];                                                                                                          
my $topic_top = $ARGV[3];                                                                                                     
                                                                                                                              
print STDERR "\nRemoving old messages from $topic_top/#\n";                                                                   
my $outfile = "/tmp/next.txt";                                                                                                
while (1) {                                                                                                                   
    my $com = "mosquitto_sub  -t '$topic_top/#' -h $mqtt_server -u '$USER' -P '$PASS' " .                                     
        "-v -C 1 -W 10 --retained-only >$outfile";                                                                            
    print STDERR "RUN $com\n";                                                                                                
    my $err = system($com);                                                                                                   
    last if $err || ! -e $outfile;                                                                                            
    my $buff = '';                                                                                                            
    open my $fh, "<", $outfile;                                                                                               
    last if ! defined $fh;                                                                                                    
    read $fh, $buff, 128, length($buff);                                                                                      
    close $fh;                                                                                                                
    my $topic = (split(/ /, $buff))[0];                                                                                       
    last if !$topic;                                                                                                          
    print STDERR  "clear topic $topic\n";                                                                                     
    $com = "mosquitto_pub -t '$topic' -n -r  -h $mqtt_server -u '$USER' -P '$PASS'";     
    $err = system($com);                                                                                                      
    last if $err;                                                                                                             
}                                                                                                                             
unlink $outfile;                                                                                                              
                                                                                                                              
print STDERR "\nALL CLEARED !!\n\n";

Now you can do this…

imagen

8 Likes

Because of an Issue with OpenMqttGateway i had the same problem, that i had to remove many mqtt devices in HA. My Solution is to execute the following Bash Script #!/bin/bash

MQTT Broker Informationen

BROKER_HOST=“IP”
BROKER_PORT=“1883”
USERNAME=“user”
PASSWORD=“password”

Überprüfe, ob der Suchbegriff als Argument übergeben wurde

if [[ -z “$1” ]]; then
echo “Bitte geben Sie einen Suchbegriff als Argument ein.”
exit 1
fi

Suchbegriff aus dem ersten Argument extrahieren

SEARCH_TERM="$1"

Zeitbegrenzung in Sekunden (z.B. 60 für eine Minute)

TIMEOUT=10

Temporäre Datei für Ausgabe

TMP_FILE=$(mktemp)

MQTT Subscription

mosquitto_sub -h “$BROKER_HOST” -p “$BROKER_PORT” -u “$USERNAME” -P “$PASSWORD” -t “homeassistant/#” -v > “$TMP_FILE” &

Debugging-Ausgabe

echo “Waiting for subscription to complete…”

Warte für die angegebene Zeit

sleep $TIMEOUT

Debugging-Ausgabe

echo “Subscription completed.”

Beenden des MQTT-Abonnements

kill $!

Schleife durch die Liste der Topics und sende Löschbefehle

#cat “$TMP_FILE” | awk ‘{print $1}’ | grep “$SEARCH_TERM” | sed ‘s//config$//’ | while IFS= read -r topic; do
cat “$TMP_FILE” | awk ‘{print $1}’ | grep “$SEARCH_TERM” | while IFS= read -r topic; do
mosquitto_pub -h “$BROKER_HOST” -p “$BROKER_PORT” -u “$USERNAME” -P “$PASSWORD” -t “$topic” -r -m “”
echo “Löschbefehl für Topic: $topic gesendet”
done

Temporäre Datei löschen

rm “$TMP_FILE”
with the parameter of the part of the topic name that is to be deleted. If you save this script as delete_mqtt-devices.sh then run it for example in this way: ./delete_mqtt-devices.sh skylink. In this example all devices which have a name that is including skylink will be deleted.