Turning Sonoff on/off with physical button

I have several sonoff switches working using @bruhautomation setup guide. They work great. I am having one issue though. When I attempt to toggle the switch through the physical button on the device the switch will power off or on but after 1 second it reverts back to its previous state. Any ideas?

1 Like

Sounds like the physical switch is turned off/on, but no MQTT status is sent out. There is probably some persistence on your MQTT server that the Sonoff then picks up and changes it back to that state.

See if you can find out how to program the physical button to change state AND send that state out over MQTT.

I am not really sure how to do that, but maybe looking through the code might show something.

I wonder if it has to do with this section of code?

#define MQTT_BUTTON_RETAIN     0                 // [ButtonRetain] Button may send retain flag (0 = off, 1 = on)
#define MQTT_POWER_RETAIN      0                 // [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)

#define MQTT_STATUS_OFF        "OFF"             // [StateText1] Command or Status result when turned off (needs to be a string like "0" or "Off")
#define MQTT_STATUS_ON         "ON"              // [StateText2] Command or Status result when turned on (needs to be a string like "1" or "On")
#define MQTT_CMND_TOGGLE       "TOGGLE"          // [StateText3] Command to send when toggling (needs to be a string like "2" or "Toggle")
#define MQTT_CMND_HOLD         "HOLD"            // [StateText4] Command to send when button is kept down for over KEY_HOLD_TIME * 0.1 seconds (needs to be a string like "HOLD")