Followed BRUH’s videos on setting up MQTT local server and then configuring a Sonoff device to use MQTT. Everything working perfectly except for the state of my switch is not retained after a restart of HA. Here is the relevent section of my config:
Hi
I have the same problem except I am using ESP easy firmware version 147. I have set both “retain: true” in my yaml and “MQTT Retain Msg:” in the ESP easy setup. Still the status of the switch is not retained after restart of HA.
Which MQTT broker are you using? I have heard the HA internal broker does not honour the retain flag, in which case you need to switch to an external broker.
In any case, you can see if the broker has retained the message by subscribing using a tool that dispays subscribed messages. I use mosquitto_sub, but others will work
mosquitto_sub -t "#" -v
This should display all retained topics as soon as it connects.
You need to see the state_topic that your sensor or switch receives the state on. Then when HA connects to the broker, it will receive that message and adjust the state of the sensor appropriately.
The “#” sign in MQTT topics indicates ‘match any set of characters’, so your HA switch will changes state whenever it gets a message beginning with these characters ( and matches the payload_on: parameter), which is probably not what you want.
You need to set the state_topic: to the exact topic that your client switch is sending - such as /Sonoff_2/relais/STATE
Yes, just make sure you include something that specifically identifies that Sonoff (maybe that it is the 2) so that you can have different topics for different switches.
I’m not sure what happens if you publish something with a ‘#’ in the topic, but its definitely something to avoid, just to stop confusion.
Hi @eBoon, just type it in your browser, or browse to ip of the sonoff, go to ‘Console’ and type the Command PowerRetain 1
After monkey_of_hill’s tip, i found this about the commands.
THANK YOU! That did it. Is that something I will need to do everytime I power up the Sonoff? Guess I should probably modify the Sonoff code to set that by default.
Hello, easier to use your web browser to connect to your sonoff module URL > " http://sonoff.ip.addrs ", then click on “Console” button, and enter the command " PowerRetain 1 " …