How to use switch button to control each other?

I have two switch buttons ("switch01" and "switch02") on screen and would like to toggle switch02 off when switch 01 is on, and vice versa.

I tried putting a function node after switch01, to toggle the value of msg.payload, then feed it to switch02. This can apply only one-way for switch01 to control switch02. If doing both way (also from switch02 to switch01) will create infinite loop.
The code in function is as below:

// Check the incoming message payload
if (msg.payload === true) {
     // If Switch 2 is turned on, set Switch 1 to off
     msg.payload = false;
 } else {
     // If Switch 2 is turned off, set Switch 1 to on
     msg.payload = true;
 }
 return msg;

Is there anyway to call a function to change state of the switch button directly, instead of looping output of function back to another switch button node to prevent infinite loop? Or is there any other way to do this?

Welcome to the forum @node0

Try this

image

The key is to Not pass message through, and to Show state of Input.
Here is the flow. You can paste it into your flow by copying it from here then pasting it the Import dialog.

[{"id":"61503e744073b853","type":"ui_switch","z":"bdd7be38.d3b55","name":"","label":"Switch 01","tooltip":"","group":"a095025ac31861a2","order":0,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":140,"y":2180,"wires":[["dcb5bc3b2216a85c"]]},{"id":"9cec55aca83a96bb","type":"ui_switch","z":"bdd7be38.d3b55","name":"","label":"Switch 02","tooltip":"","group":"a095025ac31861a2","order":0,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":140,"y":2260,"wires":[["78ce232ad2afa991"]]},{"id":"02649808e3c05314","type":"function","z":"bdd7be38.d3b55","name":"Invert payload","func":"msg.payload = !msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":2140,"wires":[["9cec55aca83a96bb"]]},{"id":"776ac564a4f786db","type":"function","z":"bdd7be38.d3b55","name":"Invert payload","func":"msg.payload = !msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":2300,"wires":[["61503e744073b853"]]},{"id":"78ce232ad2afa991","type":"junction","z":"bdd7be38.d3b55","x":260,"y":2300,"wires":[["776ac564a4f786db","9cec55aca83a96bb"]]},{"id":"dcb5bc3b2216a85c","type":"junction","z":"bdd7be38.d3b55","x":260,"y":2140,"wires":[["02649808e3c05314","61503e744073b853"]]},{"id":"a095025ac31861a2","type":"ui_group","name":"Test","tab":"76c928cd.92de08","order":5,"disp":true,"width":"6","collapse":false,"className":""},{"id":"76c928cd.92de08","type":"ui_tab","name":"Test","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
1 Like

Thank you @Colin , this works perfectly.

While this does change the two switches in the editor, it doesn't actually change the states of both outputs. What I mean is if the two switch nodes were connected to LEDs, setting S1 to on will turn on lamp 1 on, but if you now set S2 to on, it will turn on lamp 2 but it will not shut lamp 1 off.

To do that you need a little more.

In this example the switches send a 0 or 1 and the function node inverts the value (with the magic of math :smile:) and the LED use 1 and 0 to turn them off and on.

Ha! I just noticed the wires make it look like a pair of scissors!

Here is the flow:

[{"id":"a543f1c479f08644","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"78ce232ad2afa991","type":"junction","z":"a543f1c479f08644","x":280,"y":240,"wires":[["9cec55aca83a96bb","776ac564a4f786db"]]},{"id":"dcb5bc3b2216a85c","type":"junction","z":"a543f1c479f08644","x":280,"y":120,"wires":[["61503e744073b853","02649808e3c05314"]]},{"id":"61503e744073b853","type":"ui_switch","z":"a543f1c479f08644","name":"","label":"Switch 01","tooltip":"","group":"a095025ac31861a2","order":1,"width":"5","height":"1","passthru":false,"decouple":"true","topic":"topic","topicType":"msg","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","animate":false,"className":"","x":160,"y":120,"wires":[["dcb5bc3b2216a85c","f1653c42af2e68b1"]]},{"id":"9cec55aca83a96bb","type":"ui_switch","z":"a543f1c479f08644","name":"","label":"Switch 02","tooltip":"","group":"a095025ac31861a2","order":3,"width":"5","height":"1","passthru":false,"decouple":"true","topic":"topic","topicType":"msg","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","animate":false,"className":"","x":160,"y":240,"wires":[["78ce232ad2afa991","cba2210e71531b22"]]},{"id":"02649808e3c05314","type":"function","z":"a543f1c479f08644","name":"Invert payload 0->1 or 1->0","func":"msg.payload = 1 - msg.payload \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":60,"wires":[["9cec55aca83a96bb","cba2210e71531b22"]]},{"id":"776ac564a4f786db","type":"function","z":"a543f1c479f08644","name":"Invert payload 0->1 or 1->0","func":"msg.payload = 1 - msg.payload \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":300,"wires":[["61503e744073b853","f1653c42af2e68b1"]]},{"id":"f1653c42af2e68b1","type":"ui_led","z":"a543f1c479f08644","order":2,"group":"a095025ac31861a2","width":"1","height":"1","label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"#ff0000","value":"0","valueType":"num"},{"color":"#008000","value":"1","valueType":"num"}],"allowColorForValueInMessage":false,"shape":"circle","showGlow":true,"name":"","x":450,"y":120,"wires":[]},{"id":"cba2210e71531b22","type":"ui_led","z":"a543f1c479f08644","order":4,"group":"a095025ac31861a2","width":"1","height":"1","label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"#ff0000","value":"0","valueType":"num"},{"color":"#008000","value":"1","valueType":"num"}],"allowColorForValueInMessage":false,"shape":"circle","showGlow":true,"name":"","x":450,"y":240,"wires":[]},{"id":"6784f4c2aea8e926","type":"comment","z":"a543f1c479f08644","name":"This looks like a pair of sizzors!","info":"","x":270,"y":60,"wires":[]},{"id":"a095025ac31861a2","type":"ui_group","name":"Test","tab":"76c928cd.92de08","order":5,"disp":true,"width":"6","collapse":false,"className":""},{"id":"76c928cd.92de08","type":"ui_tab","name":"Test","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
1 Like

You are right.

The wiring can be simplified a bit. If you add a Junction in front of each Switch (with its two inputs going into the junction) then you can drive the leds off those.

Cool! Thanks :star_struck:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.