I'm trying to configure Rules to achieve what I'll describe below, and while doing that I found a strange behavior that I want to confirm. I'm using a sonoff basic r2, with mega-20231225 and my goals are:
- I want to be able to toggle a relay when I press the button.
- I want to be able to toggle that same relay over the web api calls (which I'm doing and works fine, using cmd=gpio,12,x).
- I want the button to keep toggling the relay even if the state was previously changed via web.
- The button is a Push Button Active Low and it's state changes between 0 and 1 at each press, as expected.
Code: Select all
on Button#state do
GPIOToggle,12
endon
If I go to the configuration interface, go to the Devices tab, and EDIT my button, and then click Submit (it does not matter if I change anything), as soon as I submit it, the relay changes state. I didn't expect that.
Looking the logs, it seems like every time I save configs, the Button instance (Task) is completely re-initialized, its state goes immediately back to 0 (no matter what it was before) and a new EVENT of Button#state=0 is emitted, causing that code to trigger and toggle the relay. You can see it below:
Code: Select all
1339335: EVENT: TaskExit#Button=1,0
1339392: EVENT: TaskInit#Button=1,1
1339400: EVENT: Button#State=0
1339408: ACT : GPIOToggle,12
1339411: GPIO toggle: port#12: set to 1
1340157: EVENT: Clock#Time=Tue,19:05
I tried all sorts of black magic using variables to save old state and try to check if it was a real state change but in the end nothing I did could cover all the cases. I appreciate any hints.
Thanks
Andre