MQTT - retained state not sticking on reboot

Hello, I have followed several posts here and this video from The Hookup @ https://www.youtube.com/watch?v=31IyfM1gygo

The problem is that when I reboot the system, the switches go on, even if they were off previous to the reboot. I thought this shouldn’t have been the case by following the configuration.

On a service restart, say from within the Hassio Server Management page or via command line, the switches won’t go on.

So is my understanding incorrect or is something incorrectly configured?

My configuration.yaml is as follows:

switch:
  - platform: mqtt
    name: "Office TV"
    command_topic: "cmnd/sonoffofficetv/POWER"
    state_topic: "stat/sonoffofficetv/POWER"
    availability_topic: "tele/sonoffofficetv/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

Using the console on the Sonoffs, I entered the following, per the video:

switchretain off 
buttonretain on 
buttonretain off 
poweronstate 3 
powerretain on

I turn off the devices via Home Assistant - toggle to off
In the Sonoff console, I see the following:

15:48:31 MQT: stat/sonoffofficetv/RESULT = {"POWER":"OFF"}
15:48:31 MQT: stat/sonoffofficetv/POWER = OFF (retained)

So the setting seems to ‘set’ to OFF and retained, but after the reboot

15:51:42 MQT: Connected
15:51:42 MQT: tele/sonoffofficetv/LWT = Online (retained)
15:51:42 MQT: cmnd/sonoffofficetv/POWER = 
15:51:42 MQT: stat/sonoffofficetv/RESULT = {"POWER":"ON"}
15:51:42 MQT: stat/sonoffofficetv/POWER = ON (retained)

The switch goes on. Huh?!?

What am I doing wrong or what isn’t properly configured?

Solved!

I could not figure out how to clear and send an empty command to the MQTT DB for the “retained” setting.

after reviewing comments in The Hookup’s video, I noticed someone has a similar issue as I did.

For some reason, trying to run that command to clear the DB doesn’t work for me - no idea why.

For now I’m going to let it go and tackle it later. The reinstall of Mosquitto worked for me.

I read the posts in the image and it’s a face-palm moment.

It’s obvious that reinstalling the MQTT Broker will clear out all retained messages. That’s like getting rid of a funky smell in the bathroom by demolishing and rebuilding the entire house.

The procedure to purge a topic’s retained message is to publish a blank message (an empty-string) to the topic with retain=true.

If done with retain=false, it will not purge the message. That’s precisely what the first person in that thread did and conclusively proved you can’t purge the message using retain=false.

After purging the topic’s message (using retain=true), set retain to false and then be sure to never ever publish to that topic using retain=true again. Otherwise you’ll have to go through the purge operation all over again.

PS
Another sure-fire way to purge all retained messages is the delete the MQTT Broker’s database. However, that’s just short of demolishing the house to eliminate the odor. :slight_smile:

1 Like

Thanks for the reply and info

I could not figure out how to purge the retain message - I’m just not guru enough.

This was the only way I could “reset” things and get back to the beginning. Now things are working as expected.

Given my config above, what is the method to purge the retain message/flag?

Thanks again.

You could definitely use mosquitto_pub to do the job. Its documentation shows you must include -r in the command-line to specify retain=true.

For people who are uncomfortable with command-line interfaces, or simply wish to have a graphical user-interface, I recommend using an MQTT client like MQTT.fx or MQTT-Spy. There are others; these are just the two open-source clients I use to experiment/debug MQTT.

Here’s what MQTT.fx looks like. In this example, it’s subscribed to all the sub-topics of hass_master. It’s very clear which topics are retained. It also shows a list of a topic’s changes (in this example, the kitchen light undergoes several state-changes with the last one bearing a message of on). Publishing to a topic is equally easy.

Thanks for the info. Th’at a really cool tool! But I’m OK with command line, I just haven’t gotten the construct correct to clear the flag.

Either way, I’ll keep at it and I’m sure I’ll eventually get it

Thanks again!

The mosquitto_pub command in the comments should work, but I did suggest this as an alternative to someone a while ago.

3 Likes

Nice! I’ve bookmarked your post and will share it the next time “How to purge a retained message?” comes up … which is about every other week. :slight_smile:

Excellent! Thank you!

1 Like