Clear Mosquitto Broker add-on Retain Messages

I’ve read many discussions suggesting clearing retain messages of the Home Assistant MQTT Broker using the mosquitto_sub command. Can the retain messages be cleared when the Mosquitto Broker Add-On is being used? I haven’t been able to find documentation. I’m not sure if clearing the retain message will solve the issues with the Sonoff Dual R2 and Sonoff 4CH Pro R2 not automatically turning on the relays when power is loss and, then, recovered but I’d like to try it. I also like to know how to clear the MQTT Broker retain messages for future reference. The Sonoffs will not hold the PowerOnState 1 value when power is loss.

you’ll need to install mosquitto first. Once done, a simple mosquitto_pub (sub is to subscribe, pub is to publish) can clear the retained message.
The message needs the -r flag for retain and the message must be empty.
mosquitto_pub -h [MQTT_Host] -p [PORT] -u [MQTT_User] -P [MQTT_Password] -t [TOPIC] -r -m ""
Just replace the text in square brackets with your settings

The ssh add-ons already have mosquitto_sub installed. You can ssh to your hassio box and run the command.

Another option is to uninstall then reinstall the mosquitto add-on. That will delete the entire database which includes the retained topics.

1 Like

The Add-on Mosquitto broker is already installed but I have not found where the mosquitto_pub command should be issued.

it’s a shell command, so either you create a shell command in your config and manually call it or you ssh into your to run it manually

1 Like

Can the mosquitto_pub command be issued from core-ssh:~# or does it have to be issued from a specific directory? This is all new to me so while I’m asking, can you also answer the following;

  1. Which topic (state_topic, command_topic or availability_topic) contains the retain messages or which topic is issue with the mosquitto_sub to clear the the retain messages?

  2. How can it be verified that the specific topic contains retain messages?

  3. How can it be verified that the retain messages have been deleted from the topic?

  4. Is documentation and examples available? If so, can you provide links?

Everything I’ve found on this has been very general. I’d hoped to find detailed information on the Home Assistant website but haven’t been able to locate any.

try mqtt.fx, you’ll see all that’s happening on your broker.
You can also use it to send empty retained messages if you struggle with the shell command.

After clearing the retain messages of the six topics that I was working on in both MQTT.fx and through ssh, the problem still existed. In MQTT.fx, I noticed that a lot of the old (test) topic names associated with the Sonoffs were still in the MQTT Broker. Uninstalling and reinstalling the Mosquitto broker seems to have solved the problem.

Thank you both, lolouk44 and NotoriousBDG!