MQTT Sonoff Tasmota reconnect issue

i want to take a sonoff basic to switch my 3D Printer on and off via home assistant. The problem is the wifi signal is very weak and this causes reconnection. So the sonoff restarts and switches the relay. I figured out setting the wificonfig to 5 eliminates the restarts but i have a problem with the MQTT settings. If the sonoff loses the wifi connection, it reconnects to the wifi and to the MQTT broker. The console shows this

23:10:20 MQT: Verbindungsversuch…
23:10:25 MQT: Verbindung fehlgeschlagen aufgrund von 192.168.0.100:1883, rc -2. Wiederversuch in 10 sek
23:10:36 MQT: Verbindungsversuch…
23:10:36 MQT: verbunden
23:10:36 MQT: tele/sonoff5/LWT = online (beibehalten)
23:10:36 MQT: cmnd/sonoff5/POWER =
23:10:36 MQT: stat/sonoff5/RESULT = {“POWER”:“ON”}
23:10:36 MQT: stat/sonoff5/POWER = ON
23:10:36 MQT: stat/sonoff5/RESULT = {“POWER”:“OFF”}
23:10:36 MQT: stat/sonoff5/POWER = OFF

My configuration for the mqtt switch looks like this:

  • platform: mqtt
    name: “Schreibtischlampe”
    command_topic: “cmnd/sonoff5/power”
    state_topic: “stat/sonoff5/POWER”
    qos: 1
    payload_on: “ON”
    payload_off: “OFF”
    retain: true

The question is, how to set up home assistant or the tasmota firmware to make sure that even if the sonoff reconnects it doesn’t switch it’s state. In this scenario my 3d-printer loses his power and in the middle of work, it stops printing.

Hope i could describe the problem as good as possible as a german potato xD

I’m also researching about this similar issue and found this:
https://github.com/arendst/Sonoff-Tasmota/issues/744

I’m going to try the last thread’s suggestion of increasing blinkcount next.
Hope this helps.

Yes. I‘ve also found this topic. Increasing the blinkcount didn‘t help in my case. Is it Important to set a retain flag in the tasmota firmware?

Hope someone could help and have a solution or a crazy workaround :slight_smile:

You can use the serial monitor when connected through ftdi/USB or through the console through the web interface to send commands.
You probably need to send “PowerRetain 1” to the esp. Also make sure you set retain to true in your home assistant config.

Here is a list of all commands: https://github.com/arendst/Sonoff-Tasmota/wiki/Commands

If i set it up like this the only thing changed are the retain flags after the ON and OFF mqtt commands. But still shut off when reconnect to home assistant. I could‘t find out if the sonoff sends these massages or the mqtt broker.

With the greatest respect would the most logical answer to be to move your router to give better wifi coverage, reuse one of the many old routers we all have lying about as an access point or just buy a cheapo plugin extender?

As I’ve said on previous threads, I had one at the extremes of my wifi coverage that kept dropping on and off the network due to interference from neighbours wifi so you couldn’t rely on it. The changes I made to my network got the strength up enough to stop that and it’s been 100% reliable from then on.

If the Sonoff reboots its going to cycle the relay no matter what configuration it is in. It’s not a locking relay.

I’ll have to agree with Bobby_Nobble, fix the WiFi issue, it’s a good excuse to pick up a couple Ubiquiti access points and enjoy some stable wifi.

I was having a similar issue with a Sonoff SV. The issue in my case was that the MQTT broker was retaining a value of ON for the topic POWER (exactly as shown in the log file that atriOo provided in the original post above). So, every time it reconnected it would revert to POWER:ON.

I came to the conclusion that while experimenting with the HA configuration, I must have inadvertently set a value to retain the power ON within the mosquitto.db. Since atriOo’s config has a “retain: true”, this is most likely the culprit. The problem is that simply setting “retain” to “false” will not fix it. The retain value needs to be cleared from the mosquitto database. Once I cleared this, no more powering on upon re-connect.

I accomplished this by sending an empty payload while using mqtt.publish service in Developers tools. I then published an OFF command to the topic along with a Retain value of true. Doing this sets it to always be in a POWER:OFF state at re-connect. That fixed my issue for good. In your case, you probably just want to remove the retain value from your config and then just clear it by sending the empty payload and then publish a retain value of false, with no ON/OFF command. More info can be found here: MQTT Fan turns every time HASS restarts

1 Like

@e1miran thanks for your brilliant solution. After i‘ve cleared out the saved mqtt command with an empty payload, the sonoff stays on if the previous state was ON and vice versa. As long as i‘m using MQTT or home assistant to toggle the sonoff it‘s working perfectly. But if i‘m pressing the button the value seems not to be saved and after a reconnection it toggles to the wrong state.
My workaround:
Set the buttontopic to a specific value (in my case ‚button5‘) and with a simple automation you could let home assistant toggle the sonoff.

Now it seems to work perfect. Thanks for your helpful answers! Love that community! <3