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

Yes you are absolutely correct. I was worried that the setting would not be retained across power cycles - but have confirmed that it is!

Yes, and while you are in the console, if you send a HASS command to your sonoff modules, you will see : MQTT: stat/sonoff1/POWER = ON (retained) ! ain’t that cute !

2 Likes

Thanks for this
I’m surprised this wasn’t enabled by default or was it?

1 Like

@jeanrocco
Thank you very much for this tip. This has been nagging me since I installed the Sonoff and have tried other useless approaches.

1 Like

I have enabled PowerRetain via the Console in the web interface of a sonoff switch, however it still doesn’t retain when HA is restarted. Is this because I’m using the internal MQTT broker?

Using an mqtt client on my phone, I can see the Sonoff stat messages say “not retained”.

Yes, the built-in broker does not seem to honor the retain flag.

Just to add, it works perfectly with the hassio add on though if you’re inclined to change

1 Like

I installed mosquitto last night and connected my sonoffs and HA to it. Haven’t had a chance to properly test it but in my android client the messages still show up as not retained.

EDIT: Having messed about for a bit, it now works as expected. If a device is on when HA restarts then it will be know it’s on. So migrating to mosquitto solved the problem for me.

1 Like

That’s always been ‘the cure’ but a guy came up with a fix using the built in broker the other day. Can’t comment on it myself as I use hassio though.

PowerRetain 1 seems to work perfect, on every HA restart it will keep last button states.
The problem now to solve is that if you do a hassio host reboot, the son off is not able to conect to mqtt host, you need to restart sonoff for all go back to normal
Anyway to solve this?

Hi I have the same problem but I notice that the problem come out when I reboot mqtt or the sonoff loose connection…

Super! Works for me!

Hi,

I have the following problem with MQTT and Sonofff SV. After reboot stays ON:
Background of the problem:

  • Sonoff SV was responsible for opening my main door
  • the solution was working perfectly for close to 10 days
  • after some of the reboots I did while modifying hassio setup I noticed that the door is buzzing constantly (as somebody has pressed the button to open a door). I switched off the power to my Sonoff as a temp solution

Here is what I tried and the different outcomes:

  • changed the settings of the Sonoff not to be linked to the build in MQTT broker - upon restart it was in OFF state as expected. Conclusion Broker is triggering the state to be ON
  • changed the state of the Sonoff from the console to be Retain status OFF after reboot
  • after connecting to the Broker the problem came back. Conclusion as it was also described in different posts Broker is overwriting the state in the Sonoff. The problem seems to be in the broker.
  • I did uninstall of the Mosquito build in Broker and after that installed again. The setup to open the door worked fine for a short time. When I did occasional reboot of the Raspberry PI the Sonoff was again in ON state.

I am not sure how I can overcome the problem.
My next step will be to try using Broker that will be running on Ubuntu Server and connecting to it from HA.

Any help on the matter will be appreciated.

The last time I checked, the embedded broker did not respect the retain flag. I suggest you just install the mosquitto broker and try with that.

Very, very strange.

I did an install of MQTT broker under Ubuntu.

The same problem: after reboot of the Sonoff SV it enters in state ON.
I have removed all links in the HA yaml files to isolate the influence of HA.
Again the same upon restart of SV it enters in ON state.
Looking in the SV console PowerOnState is set to default 3. (previously was 0).

Here is the log:
19:52:14 MQT: homeassistant/switch/sonoffsv01/config = (retained)
19:52:15 MQT: stat/sonoffsv01/RESULT = {“POWER”:“ON”}
19:52:15 MQT: stat/sonoffsv01/POWER = ON

Next thing I will try tomorrow is to Flash again the Sonoff and check if there will be some difference.

Share with More-Log options from Tasmota pls :slight_smile:

Also, what do you get when you run the following command

$ mosquitto_sub -t "#" -v  

You may need to add your username and password to that command. It will print out what the broker has retained in its database.

Hi,

I have re written my MQTT commands.

I found out that I have mixed “stat” and “cmd” also some other syntax.

1 Like

then I got to Tasmota web GUI/console and entered

PowerRetain 1

is that everything I need to do?

1 Like

yup - that worked for me. Make sure you give it a quick test to make sure it’s all working as you expect!

1 Like