Maybe you can give me a quick example of how to do that with Events. That would be helpful.
The general idea is to stop using the "Switch input - Switch" as an GPIO output device, which is what your relay task is doing. This was a convenient thing to do in ESPEasy, but is now forbidden.
I snipped some code from one of my nodes. This example includes rules based push button toggle (momentary press on/off). A boot rule is also used to restore the relay state on a soft reset.
General Info:
* Relay is on GPIO-12. 0=Off, 1=On.
* Push Switch is active low (press sinks GPIO to ground).
* ESPEasy's
Home Assistant (openHAB) MQTT controller is used to publish relay state (0/1).
Device Task Info:
*
Switch input - Switch task is named
BUTTON, value is named
press. Internal Pullup, Inverse Logic, Switch Type:
Switch, Button Type:
Normal, Debounce:
100mS, Send to Controller: Disabled, Interval:0.
*
Generic - Dummy Device at task position 6 is named
STATEVAR, value #1 is named
rlyval.
* No other device tasks are needed.
Code: Select all
// Restore the relay's state during reboot.
on System#Boot do
if[STATEVAR#rlyval]=1 // Warm boot, relay was previously on.
event,RelayOn
else
event,RelayOff
endif
endon
// Push Button toggles the Relay
on BUTTON#Press=1 do
if [STATEVAR#rlyval]=1
event,RelayOff
else
event,RelayOn
endif
endon
// Event: Turn on Relay.
on RelayOn do
TaskValueSet 6,1,1
gpio,12,1
Publish %sysname%/STATEVAR/rlyval,[STATEVAR#rlyval]
endon
// Event: Turn off relay.
on RelayOff do
TaskValueSet 6,1,-1
gpio,12,0
Publish %sysname%/STATEVAR/rlyval,[STATEVAR#rlyval]
endon
Tools->Command, MQTT, and http can control the relay using the RelayOn and RelayOff events. Command examples:
My rules advice reflects how I like to implement it. But there's more than one way to skin this cat. Maybe someone else will chime in and share an example of their solution.
- Thomas