Toggle a state in node red


I’m trying to connect a smart switch to control a garden socket that has some outside lights attached.

I want a press of the button to toggle the state of the switch, but cant work out a simple flow. Ant tips?

Button pressed: If plug on, turn off, else turn on.


If the smart switch is integrated in HA, then this would already do that function.

Wally, thanks, but how?

I think the confusion is that the lights are also turned on by a node-red timer, so this additional switch needs to toggle it regardless of the status.

If it’s a 1-1 relationship between switch and device it’s easy, switch on - socket on, but in this case switch on could mean turn socket off if it’s already on.

So you need a switch that just show on or off independently from the socket? But why?

Its easy enough to do.
Make a button node, then follow it by a current state node that pulls the light state with an if state to on (or off). This will make two exits, one for on that should go to call service off and one for off that should go to call service on.

Ah perfect, easy when you know how. Many thanks.