I had just successfully finished setting up my first Sonoff all bar clearing the Retained messages in Mosquitto which was turning on my Sonoff every time I power cycled it.
I read that all I need to do to clear the retain in Mosquitto was to uninstall and install it again which I did and pasted my settings back in and my Sonoff didn’t switch on after power cycle which is good.
However my Sonoff switch disappeared of the Overview page in Hassio and I am seeing errors in the Mosquitto logs.
I found this thread on the same thing but the guy ended up doing a fresh install the last post did not fix my issue. 'Unable to connect to the broker'
I have restored from a backup I did the day before and the issue goes away but naturally mosquitto still has the retain issue with sonoff turning on after power cycle. After I did a restore from a backup I was able to replicate this problem again by simply uninstalling and re-installing mosquitto without changing any settings in the config or anywhere else.
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…
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?
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.
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?
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.
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.
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 #.
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.
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.