Sonoff Fail After Re-Install Mosquitto

If the command is retained and triggers your light on every time, you can publish to mqtt an empty message to the same retained topic to delete it… Also if you use the home assistant mqtt switch, there is an option to not use retained messages so it will behave as you wish…

This sounds like what I need but I’m unsure exactly what you mean.

Do you have a link to an explanation of the mqtt switch and the retained messages option?

How is the switch defined in your config file ? like here ?

1 Like

Thank you!

So, if I set ‘retain’ to true, following a restart the device will be as it was prior to the restart until it receives another message. Is that a correct understanding?

Time for some basic mqtt…

Mqtt has two types of messages: retained and non-retained.

  • non-retained images are seen by any connected device when the message is sent - so if you turn on the device via hass, but the device is offline, it will never see it - it’s like getting a flyer on the street - if you don’t pass there you don’t get the paper
  • retained messages are seen by connected devices like the non-retained ones, but are also stored by the broker (mosquitto in this case) - this means that if you turn your device via hass, the on message is stored on the command topic then, later on, when the device asks to listen to commands on that topic, you get the last command - imagine getting your mail from the post-office, only that, every time you ask you get a copy of last message you have been sent…

Going back to hass / mqtt / devices…

  • the device should write state as RETAINED so that if home assistant retarts it can read the current state of the device without the device needing to resend it
  • the command should be NON-RETANED so the device, if online, should see it ONCE, do stuff, then never see the same message again ever

Now, if you also have a topic on which your device tells mqtt it’s online or offline then you can use that as

  • availability_topic - this is the topic where your device published “i’m online” or “i’m offline”
  • payload_available: “i’m online”
  • payload_not_available: “i’m offline”

If you can use the above (replace the text with the correct version for you), then home assistant can show a switch as unavailable when the device is powered off.

Hope this makes things a bit more clear…

2 Likes

Thank you for taking the time to do that. I appreciate it.

You’re welcome.
You can try to delete a retained message by doing

mosquitto_publish -h <broker_ip> -t "some/topic/here" -m "" -r

This will publish an empty message to that retained topic and will delete it. Do that after you set retained to false in home assistant config. That should solve your problem without needing to reinstall mosquitto

The thing I’m trying to fix is to have the device, after power restore, come back to what it was before power failure.

If I use SwitchRetain=true on the device (sonoff flashed with tasmota) will that achieve what I want, effectively ignoring Home Assistant until it receives an alternate message?

Either use retain true or an automation for that, if the device sends online/offline topic…

  • setup a second switch (device x last state)
  • add a mqtt sensor to listen to online / offline state
  • on light change state and light online is true, update second switch
  • on sensor change state offline → online, send last state switch value to device by publishing mqtt command

Retain means that if hass is down but mqtt up, the light will remain on/off at each restart…

Probably there are even more ways of doing this…

The tasmota topic for the online/offline is on the suffix /LWT or /lwt

1 Like

Sorry for the delay here. I was checking my emails for replys over the weekend and the board never told me there were any replies yet.

I have restored from my last backup again so just looking to clear those retained messages from when I had that enabled in my config file, but since turned retain to ‘false’.

I’ve SSH’d to my Hassio. I use a username and password in Mosquitto so your code gets access denied message: mosquitto_publish -h <broker_ip> -t “some/topic/here” -m “” -r

Instead I’ve taken the code from this page :
mosquitto_pub -h [MQTT_Host] -p [PORT] -u [MQTT_User] -P [MQTT_Password] -t [TOPIC] -r -m “”

and made it into this: mosquitto_pub -h 192.168.1.118 -p 1883 -u jackd -P password123 -t home/light/sonoff1 -r -m “”

However it doesn’t seem to clearing the retain because Sonoff still turns on. I am guessing it is my topic as I know my Sonoffs topic is sonoff1 but I was sure if I should be putting home/light/ before. Which I kinda imprevised from this mqtt publish page I am guessing ‘home’ is what I set as my homeassistant name the config yaml file and ‘light’ is what type I have my sonoff nested under also in the config yaml file.

Here my config file

I have found installing Mqtt Snooper or something similar is the best way to really see the messages that are being broadcast. Ones with a R on the left are retained messages and you can select messages from the list and delete one or clear all messages. It really helps with debugging and understanding what is happening in real time.

I don’t think sending an empty string works. You have to send a null payload with the -n parameter like

mosquitto_pub -h 192.168.1.118 -p 1883 -u jackd -P password123 -t home/light/sonoff1 -r -n

Thanks I gave it a try and it didn’t seem to fix the retain, light still powers on if I power cycle sonoff.

Still fresh with MQTT. I installed MQTT Snooper app on my phone and successfully setup up the server connection.

However I am not sure how to add my sonoff to the app. My sonoff topic name is called sonoff1 and in my config yaml it’s under lights group. In the app I added topic home/light/sonoff1 and clicked on the on button but nothing seems to happening.

MQTT defines a special character # that enables you to subscribe to all topics - just put it in the topic field when subscribing and you will see all messages flowing through the broker, which is very useful for finding out what is happening.

You can tell if the topic is still retained in the broker’s database if the topic and payload are displayed immediately after subscribing to #.

1 Like

That as easy I was trying to add the full topic path under the TOPIC menu when all I need to do was put in a single # and now I can see 6 events in my “IN” menu all with an R in the left column.

I can see in the list of 6 (screenshot below) that it has retained not only the current topic name I had for my sonoff but also the previous name I started out with. I have deleted them all now and power cycled my sonoff and happy days it’s fixed.


1 Like

Here’s how I solved it with my Sonoff.

Start by putting retain: false in the Home Assistant configuration for all Sonoff devices.

Then set your Sonoff switches as follows from “Tasmota Console” or command line in TasmoAdmin.

SwitchRetain Off
ButtonRetain On
ButtonRetain Off

If they are entered in this order, they will be changed as well as clear all previous retaining messages to Home Assistant.

Then set PowerOnState 3, it will keep the sonoff device in the last state (on or off), in case of power failure, and return to it when power recovers.

Then set PowerRetain On, to enable Home Assistant and your switch to sync the latest mode (on or off) at restart.

If your switch is of the Toutch model, you should also set SetOption13 1, which makes the response to the toutch button better.

When reinstalling Home Assistant or Mosquitto this may need to be done again!
I needed to do it after the memory card broke down!

Hope this helped you!

1 Like

Excellent. Thank you.

Why would it need to be done again, if the settings are set in the Sonoff, after a HA rebuild?

This is also covered in this video The Hook Up - Fix Random Switching I did follow this procedure on my Sonoff Tasmota console a few steps ago and this did not resolve retained messages for me.

In my case, it was because I did not fix the error with my HA directly, but it took a few weeks and during that time we used the buttons on the Sonoff modules.
When I then fixed to HA, not all Sonoff modules would sync with HA.
Everything went well as long as all modules had contact with HA but unfortunately I got into interference that caused them to lose contact.

This unfortunately happens in the middle of the night with the result that when the modules got in touch, interpret it as if someone pressed the button on them and simply lit the light!

My son who has a Touch Module in his room did not get too happy when it constantly lit the ceiling lamp in the middle of the night!

In any case, the solution for me was to go through the sequence I referred to again.

After that we can sleep well without the light lit in the middle of the night!