MQTT : How to Retain Sonoff Switch Setting across Restarts of HA

Hello All,

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:

- platform: mqtt
  name: "Strip Cord 1"
  command_topic: "cmnd/sonoff/power"
  state_topic: "stat/sonoff/POWER"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  retain: true

I thought that with the “retain: true”, my switch state would be recovered on restart of HA, but it is not. What am I missing?

Thanks in advance for your help!

Tom

1 Like

The retain flag is set when a topic is published, so the retain flag here refers to the command_topic.

To get the broker to retain the state_topic, the code in the sonoff must be changed to set the retain flag when the state_topic is published.

Ok - that makes sense. Does anyone have any experince modifying the Tasmota Sonoff code to accomplish this?

Thank you for your help!

Tom

Try -> "local.ip.of.sonoff/cm?cmnd=PowerRetain 1 "

10 Likes

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.

I am also using mosquitto.

I get the following by using mosquitto_sub -t “#” -v

cmnd/sonoff_2/POWER OFF
cmnd/Sonoff_2/relais ON
/Sonoff_2/relais/Switch ON
/Sonoff_2/relais/switch 1
/Sonoff_2/relais/SWITCH 0
/Sonoff_2/relais/STATE 1
/Sonoff_2/relais/12 0
/Sonoff_2/gpio/12 1
/Sonoff_2/button/state 0
/Sonoff_2/sonoff_2/Humidity 46
/Sonoff_2/sonoff_2/Temperature 23.6
home/sonoff/sonoff_2/1 OFF
home/sonoff/sonoff_2/1/cmnd ON

I am not quite sure what to look for.
My subscribe template in ESP easy says:
/%sysname%/#
so my text in the yaml is:
state_topic: “/Sonoff_2/#”

@monkey_of_hill, Thanks very much for this.
The state off the switch is now correct after HA restarts.

1 Like

Could some of it be old unused topics? If yes is it possible to “clean” it up?

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.

I use this in my yaml for state:
state_topic: “/Sonoff_2/#”
but this it now shown in the mosquitto_sub list.

1 Like

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

So instead of “/Sonoff_2/#” in the setup of ESP easy I have to write "/Sonoff_2/relais/STATE and then the same in my yaml?

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.

I get the following when I connect:

$mosquitto_sub -u tom -P xxxxxxxx  -t "#" -v
   cmnd/sonoff/power ON
   tele/sonoff/LWT Online

Where do I enter this command?

Thanks all for your help!

Tom

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.

8 Likes

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.

Thanks again!

Tom

Found it…

user_config.h (MQTT_POWER_RETAIN) :

#define MQTT_BUTTON_RETAIN     0                 // [ButtonRetain] Button may send retain flag (0 = off, 1 = on)
#define MQTT_POWER_RETAIN      1                 // [PowerRetain] Power status message may send retain flag (0 = off, 1 = on)
#define MQTT_SWITCH_RETAIN     0                 // [SwitchRetain] Switch may send retain flag (0 = off, 1 = on)

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 " …

2 Likes