Enabling 'magic switch mode' for sonoff basic R4 on tasmota

Hi, the sonoff basic r4 got an esp32 upgrade as well as a new ‘magic switch mode’
That one intrigued me because it allows me to install it to a light fixture and to an existing 2 way wiring without adding new cables.

image

However, this mode doesn’t seem to work in tasmota/esphome. The relay is not toggled when pressing the switch which leads me to believe there’s additional configuration required.

After some discussion in the discord, it could be using zero crossing detection based on pulse detected at GPIO5.

//Logs from tasmota console with  `TelePeriod 10` , and `SetOption79 1` to reset the counter each cycle with GPIO5 assigned as counter.
17:57:33.952 MQT: tele/tasmota_E34494/STATE = {"Time":"2023-11-20T17:57:33","Uptime":"0T00:07:11","UptimeSec":431,"Heap":185,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"Berry":{"HeapUsed":4,"Objects":45},"POWER":"ON","Wifi":{"AP":2,"SSId":"Suliana","BSSId":"9C:A2:F4:37:D5:E1","Channel":9,"Mode":"11n","RSSI":100,"Signal":-41,"LinkCount":1,"Downtime":"0T00:00:03"}}
17:57:33.991 MQT: tele/tasmota_E34494/SENSOR = {"Time":"2023-11-20T17:57:33","Switch1":"OFF","COUNTER":{"C1":1030}}
17:57:43.933 MQT: tele/tasmota_E34494/STATE = {"Time":"2023-11-20T17:57:43","Uptime":"0T00:07:21","UptimeSec":441,"Heap":185,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"Berry":{"HeapUsed":4,"Objects":45},"POWER":"ON","Wifi":{"AP":2,"SSId":"Suliana","BSSId":"9C:A2:F4:37:D5:E1","Channel":9,"Mode":"11n","RSSI":100,"Signal":-41,"LinkCount":1,"Downtime":"0T00:00:03"}}
17:57:43.973 MQT: tele/tasmota_E34494/SENSOR = {"Time":"2023-11-20T17:57:43","Switch1":"OFF","COUNTER":{"C1":1029}}
17:57:53.917 MQT: tele/tasmota_E34494/STATE = {"Time":"2023-11-20T17:57:53","Uptime":"0T00:07:31","UptimeSec":451,"Heap":185,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":20,"MqttCount":1,"Berry":{"HeapUsed":4,"Objects":45},"POWER":"ON","Wifi":{"AP":2,"SSId":"Suliana","BSSId":"9C:A2:F4:37:D5:E1","Channel":9,"Mode":"11n","RSSI":100,"Signal":-36,"LinkCount":1,"Downtime":"0T00:00:03"}}
17:57:53.955 MQT: tele/tasmota_E34494/SENSOR = {"Time":"2023-11-20T17:57:53","Switch1":"OFF","COUNTER":{"C1":1032}}
17:58:03.954 MQT: tele/tasmota_E34494/STATE = {"Time":"2023-11-20T17:58:03","Uptime":"0T00:07:41","UptimeSec":461,"Heap":185,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"Berry":{"HeapUsed":4,"Objects":45},"POWER":"ON","Wifi":{"AP":2,"SSId":"Suliana","BSSId":"9C:A2:F4:37:D5:E1","Channel":9,"Mode":"11n","RSSI":100,"Signal":-43,"LinkCount":1,"Downtime":"0T00:00:03"}}
17:58:03.993 MQT: tele/tasmota_E34494/SENSOR = {"Time":"2023-11-20T17:58:03","Switch1":"OFF","COUNTER":{"C1":1023}}
17:58:13.941 MQT: tele/tasmota_E34494/STATE = {"Time":"2023-11-20T17:58:13","Uptime":"0T00:07:51","UptimeSec":471,"Heap":185,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"Berry":{"HeapUsed":4,"Objects":45},"POWER":"ON","Wifi":{"AP":2,"SSId":"Suliana","BSSId":"9C:A2:F4:37:D5:E1","Channel":9,"Mode":"11n","RSSI":100,"Signal":-42,"LinkCount":1,"Downtime":"0T00:00:03"}}
17:58:13.981 MQT: tele/tasmota_E34494/SENSOR = {"Time":"2023-11-20T17:58:13","Switch1":"OFF","COUNTER":{"C1":1038}}
17:58:23.922 MQT: tele/tasmota_E34494/STATE = {"Time":"2023-11-20T17:58:23","Uptime":"0T00:08:01","UptimeSec":481,"Heap":185,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":20,"MqttCount":1,"Berry":{"HeapUsed":4,"Objects":45},"POWER":"ON","Wifi":{"AP":2,"SSId":"Suliana","BSSId":"9C:A2:F4:37:D5:E1","Channel":9,"Mode":"11n","RSSI":100,"Signal":-41,"LinkCount":1,"Downtime":"0T00:00:03"}}
17:58:23.960 MQT: tele/tasmota_E34494/SENSOR = {"Time":"2023-11-20T17:58:23","Switch1":"OFF","COUNTER":{"C1":1026}}
17:58:33.961 MQT: tele/tasmota_E34494/STATE = {"Time":"2023-11-20T17:58:33","Uptime":"0T00:08:11","UptimeSec":491,"Heap":185,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"Berry":{"HeapUsed":4,"Objects":45},"POWER":"ON","Wifi":{"AP":2,"SSId":"Suliana","BSSId":"9C:A2:F4:37:D5:E1","Channel":9,"Mode":"11n","RSSI":100,"Signal":-42,"LinkCount":1,"Downtime":"0T00:00:03"}}
17:58:34.001 MQT: tele/tasmota_E34494/SENSOR = {"Time":"2023-11-20T17:58:33","Switch1":"OFF","COUNTER":{"C1":1043}}

I tried assigning zero crossing pulse on pin 5 but that doesn’t work
Tried switchdebounce 99 option but that just toggles the relay every second on its own.

17:29:21.632 MQT: stat/tasmota_E34494/RESULT = {"POWER":"OFF"}
17:29:21.637 MQT: stat/tasmota_E34494/POWER = OFF
17:29:22.260 MQT: stat/tasmota_E34494/RESULT = {"POWER":"ON"}
17:29:22.265 MQT: stat/tasmota_E34494/POWER = ON
17:29:22.990 MQT: stat/tasmota_E34494/RESULT = {"POWER":"OFF"}
17:29:22.996 MQT: stat/tasmota_E34494/POWER = OFF
17:29:24.669 MQT: stat/tasmota_E34494/RESULT = {"POWER":"ON"}
17:29:24.674 MQT: stat/tasmota_E34494/POWER = ON
17:29:25.890 MQT: stat/tasmota_E34494/RESULT = {"POWER":"OFF"}
17:29:25.893 MQT: stat/tasmota_E34494/POWER = OFF
17:29:26.497 MQT: stat/tasmota_E34494/RESULT = {"POWER":"ON"}
17:29:26.503 MQT: stat/tasmota_E34494/POWER = ON
17:29:27.747 MQT: stat/tasmota_E34494/RESULT = {"POWER":"OFF"}
17:29:27.752 MQT: stat/tasmota_E34494/POWER = OFF
17:29:28.930 MQT: stat/tasmota_E34494/RESULT = {"POWER":"ON"}
17:29:28.933 MQT: stat/tasmota_E34494/POWER = ON

Does anyone have any idea how this mode works and if it can be enabled on tasmota?

I’m also interested in the functionality but don’t have a Basic R4 yet.
Have you tryed to set the PowerOn stat to toggle value :

PowerOnState 3

Maybe the magic switch mode is just a toggle switch when during a short period of power off

It works on esphome now with custom component by ssieb

external_components:
  - source: github://ssieb/custom_components@sswitch
    components: [ magic_switch ]
    refresh: 1min

magic_switch:
  pin: 5
  on_switch:
    - light.toggle: light_1

More discussion on tasmota here

1 Like

See on https://templates.blakadder.com/sonoff_BASICR4.html

To support the MagicSwitch feature as promoted by Sonoff, set GPIO5 as MagicSwitch with index 1 or use the following template:

{"NAME":"Sonoff Basic R4","GPIO":[0,0,0,0,224,10560,544,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0],"FLAG":0,"BASE":1}

With minimal release 13.3.0.1 of the tasmota32c3 build

Tasmota firmware does not work as expected with magic switch enabled.
Sometimes power on or off multiple times… it is not good.
ESPhome firmware and Sonoff firmware both works ok with magic switch.
I am big Tasmota fan and I hope that this bug can be fixed.

Did you post an issue on the Tasmota github ?

I did not because I do not have Tasmota installed anymore on Sonoff Basic R4 so I cannot read log and another requested details for open new issue

I have found that I can get this to work with the switch on Tasmota by setting a long Window of 20
I understand this is 20x50us (1 second)

My issue now is when switching the relay on by MQTT, a bounce pulse is detected and it turns off again.

It seems the window may not be used for switching by MQTT commands but bounce pulses are created by device itself as well as the physical switch.

Issue raised on GitHub

1 Like

Hi, on the stock firmware.
Was the magic switch function working normally?

1 Like

I tested the magicswitch function in a plug socket but did not install in my lighting circuit until I had flashed with Tasmota.

I did not test with the ewelink app

I do have another stock one so may test or try esphome if a Tasmota or Home Assistant workaround can’t be found.

I had Himmel switches installed recently. Turned out they didn’t work well with the magic switch mode (even on stock firmware). I had to firmly and slowly press the switches to toggle the relay.

So I figured out there was nothing I could tweak on tasmota/esphome.

Ended up using octocoupler method since I didn’t wanna change the existing switch aesthetics.

1 Like

I have my switch working with a long window time. Had to compile my own binary to adjust this.

Thanks for posting the video though, it may be I need to put a filter between the sonoff and the load though as they have done on that video.

The issue where the relay activation was causing the magicswitch to activate had now been resolved by barbudor on GitHub and is waiting to be merged.

1 Like