Absolutely loving this combo of ESP Easy and Node-RED. Have made a whole bunch of little modules, for activating relays and recently just grabbed a bunch of Sonoff's for controlling lighting circuits.
So the set up is a Sonoff Basic switch, loaded with ESP Easy, and then the exisiting light switch wired in to GPIO 14 so people can still use the switch.
There is an internal "rule" in the sonoff that says, when the state of GPIO 14 changes, toggle the relay output. Looks like this:
on ext_sw#Switch do
on switchon do
on switchoff do
But I've come across one really annoying thing. When the Sonoff reboots, it triggers this rule, and toggles the relay meaning that lighting circuit will turn on for a few seconds and turn off again.
This makes sense as the switch state has "changed" so the rule should trigger, but still really annoying. But I am seriously struggling to do this sort of basic logic. I've made sure the boot state of GPIO 12 is low, but it still gets toggled because the switch event runs at boot. There is an event called 'System#Boot' you can run rules on, so I've tried simple delay and timer rules, but they don't do anything either. So my thought process is rather than do this on the Sonoff, do it in Node-red.
There are two MQTT topics:
/sonoff20/relay/state <--- reports is the relay on (1) or off (0)
/sonoff20/ext_sw_Switch <--- reports everytime the switch state changes to either 1 or 0
So in Node-RED I need a flow that says "on 'switch event', if "relay state" is 0, change it to 1, or if relay state is 1, change it to 0"
My thought process says I need to store "relay state" in some sort of a persistent variable, and then just toggle that between 1 & 0 on each switch event. It doesn't matter if the switch output is 1 or 0, we just want the rule to run each time it changes (input in to the function node can be via a 'report by exception' node to achieve this).