Mutual exclusion with switch node

Dears,

I want to use switch node do mutual exclusion on lighting(light A is on-- light B is off, light B is on--light A is off). However I don't know how to implement it with switch node.

light-a

BR,
Chris

Think through the logic first:

When lightA changes:

if (lightA === true) lightB = false;

When lightB changes:

if (lightB === true) lightA = false;

[{"id":"90b02a78.a31118","type":"ui_switch","z":"75be3680.18cac8","name":"","label":"LIGHT A","group":"ac2c8e97.2a413","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","x":310,"y":540,"wires":[["a9df4ef9.64f4d","e96d6ba1.6ed668"]]},{"id":"2ee42378.c35cbc","type":"ui_switch","z":"75be3680.18cac8","name":"","label":"LIGHT B","group":"ac2c8e97.2a413","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"str","onicon":"","oncolor":"","offvalue":"false","offvalueType":"str","officon":"","offcolor":"","x":300,"y":600,"wires":[["f9454df0.6fddb","e96d6ba1.6ed668"]]},{"id":"e96d6ba1.6ed668","type":"debug","z":"75be3680.18cac8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":580,"wires":[]},{"id":"a9df4ef9.64f4d","type":"change","z":"75be3680.18cac8","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"str","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":540,"wires":[["2ee42378.c35cbc"]]},{"id":"f9454df0.6fddb","type":"change","z":"75be3680.18cac8","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"str","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":600,"wires":[["90b02a78.a31118"]]},{"id":"ac2c8e97.2a413","type":"ui_group","z":"","name":"test","tab":"d92ab4b4.55f5c8","order":3,"disp":true,"width":"7","collapse":true},{"id":"d92ab4b4.55f5c8","type":"ui_tab","z":"","name":"test","icon":"fa-television","order":2}]

Thanks. I see. Below is my flow with function node + switch node to implement.

[{"id":"b888eb90.f58e38","type":"ui_switch","z":"2c2fe5ba.c6ea6a","name":"","label":"light A","group":"db50e537.4096e8","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":240,"y":200,"wires":[["51a8b614.842668","2cf2dca9.827404"]]},{"id":"52b5570b.29f958","type":"ui_switch","z":"2c2fe5ba.c6ea6a","name":"","label":"light B","group":"db50e537.4096e8","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":240,"y":260,"wires":[["61e58da7.1ba524","f7d3d7dc.2f6218"]]},{"id":"51a8b614.842668","type":"function","z":"2c2fe5ba.c6ea6a","name":"change payload","func":"if (msg.payload===true)\n msg.payload=false;\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":200,"wires":[["52b5570b.29f958"]]},{"id":"61e58da7.1ba524","type":"function","z":"2c2fe5ba.c6ea6a","name":"change payload","func":"if (msg.payload===true)\n msg.payload=false;\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":260,"wires":[["b888eb90.f58e38"]]},{"id":"2cf2dca9.827404","type":"debug","z":"2c2fe5ba.c6ea6a","name":"light A","active":true,"console":"false","complete":"payload","x":590,"y":140,"wires":[]},{"id":"f7d3d7dc.2f6218","type":"debug","z":"2c2fe5ba.c6ea6a","name":"light B","active":true,"console":"false","complete":"payload","x":590,"y":300,"wires":[]},{"id":"db50e537.4096e8","type":"ui_group","z":"","name":"Light","tab":"5167dfe6.e7d11","order":1,"disp":true,"width":"6"},{"id":"5167dfe6.e7d11","type":"ui_tab","z":"","name":"Dashboard","icon":"dashboard"}]

Thanks david. It's good way.

I took your code and changed it slightly. It eliminates the use of the function node, which makes it easier to read. And it is really mutually exclusive now. You can click on any of the two buttons in the dashboard, and the other button changes its state too.
Does this fit your need?

[{"id":"637be1ab.fff2","type":"ui_switch","z":"8a8860c2.8bae6","name":"","label":"light A","group":"c5de6ddc.866bf8","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":470,"y":180,"wires":[["6a3c6e42.e32188","1c14f263.cfaf16"]]},{"id":"c12c290e.94e26","type":"ui_switch","z":"8a8860c2.8bae6","name":"","label":"light B","group":"c5de6ddc.866bf8","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":470,"y":340,"wires":[["b8443192.103cc8","730ad674.c7504"]]},{"id":"6a3c6e42.e32188","type":"debug","z":"8a8860c2.8bae6","name":"light A","active":true,"console":"false","complete":"payload","x":620,"y":180,"wires":[]},{"id":"b8443192.103cc8","type":"debug","z":"8a8860c2.8bae6","name":"light B","active":true,"console":"false","complete":"payload","x":630,"y":340,"wires":[]},{"id":"1c14f263.cfaf16","type":"switch","z":"8a8860c2.8bae6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":190,"y":340,"wires":[["916170a3.459168"],["6d1526ac.53c11"]]},{"id":"916170a3.459168","type":"change","z":"8a8860c2.8bae6","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":320,"wires":[["c12c290e.94e26"]]},{"id":"6d1526ac.53c11","type":"change","z":"8a8860c2.8bae6","name":"true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":360,"wires":[["c12c290e.94e26"]]},{"id":"730ad674.c7504","type":"switch","z":"8a8860c2.8bae6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":190,"y":180,"wires":[["b58cf08a.63039"],["25e1b090.d9858"]]},{"id":"b58cf08a.63039","type":"change","z":"8a8860c2.8bae6","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":160,"wires":[["637be1ab.fff2"]]},{"id":"25e1b090.d9858","type":"change","z":"8a8860c2.8bae6","name":"true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":200,"wires":[["637be1ab.fff2"]]},{"id":"c5de6ddc.866bf8","type":"ui_group","z":"","name":"Light","tab":"b987fe51.3b5d48","order":1,"disp":true,"width":"6"},{"id":"b987fe51.3b5d48","type":"ui_tab","z":"","name":"Dashboard","icon":"dashboard"}]

Thank you. It looks cool. But I just want the mutual exclusion on light on(light A is on--light B is off, light B is on--light A is off) not light off. Another question is how to solve external input condition on switch node for mutual exclusion not only click function.

Below is my flow I believe still have issue.
[{"id":"b888eb90.f58e38","type":"ui_switch","z":"2c2fe5ba.c6ea6a","name":"","label":"light A","group":"db50e537.4096e8","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":370,"y":200,"wires":[["51a8b614.842668","2cf2dca9.827404"]]},{"id":"52b5570b.29f958","type":"ui_switch","z":"2c2fe5ba.c6ea6a","name":"","label":"light B","group":"db50e537.4096e8","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":370,"y":260,"wires":[["61e58da7.1ba524","f7d3d7dc.2f6218"]]},{"id":"51a8b614.842668","type":"function","z":"2c2fe5ba.c6ea6a","name":"change payload","func":"if (msg.payload===true)\n msg.payload=false;\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":200,"wires":[["52b5570b.29f958"]]},{"id":"61e58da7.1ba524","type":"function","z":"2c2fe5ba.c6ea6a","name":"change payload","func":"if (msg.payload===true)\n msg.payload=false;\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":260,"wires":[["b888eb90.f58e38"]]},{"id":"2cf2dca9.827404","type":"debug","z":"2c2fe5ba.c6ea6a","name":"light A","active":true,"console":"false","complete":"payload","x":720,"y":140,"wires":},{"id":"f7d3d7dc.2f6218","type":"debug","z":"2c2fe5ba.c6ea6a","name":"light B","active":true,"console":"false","complete":"payload","x":720,"y":300,"wires":},{"id":"91afe59e.d9c5e8","type":"inject","z":"2c2fe5ba.c6ea6a","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"x":150,"y":180,"wires":[["b888eb90.f58e38"]]},{"id":"8ef7a87c.fed578","type":"inject","z":"2c2fe5ba.c6ea6a","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"x":150,"y":260,"wires":[["52b5570b.29f958"]]},{"id":"db50e537.4096e8","type":"ui_group","z":"","name":"Light","tab":"5167dfe6.e7d11","order":1,"disp":true,"width":"6"},{"id":"5167dfe6.e7d11","type":"ui_tab","z":"","name":"Dashboard","icon":"dashboard"}]

OK, I understand.

In your dashboard nodes 'light A' and 'light B' there is a configuration option which reads:
if msg arrives on input, pass through to output

I suggest you enable this option and see in awe what happens :wink:

Kind regards,

Urs.

Hey,

I have been trying to get the mutually exclusive toggle buttons to work for a while, but there seems to be something else going on! A few times the looping nature of this solutions froze up the computer :smiley:

Your code works in the editor as it should, but it doesn't show the change in dashboard (buttons should show output, so when they are toggled from left they should still change appearance I guess?)

Edit: ok, so the toggle switch has input state and output state separately, it really can't be turned off (visually and output wise) without direct user input. And there is no radio button support in ui elements. This is really quite weird :confused:

Anyway, NR 1.2.9, node 15.8.0

Maybe see this thread for an alternative Feature Request - Off - Auto - On Toggle - #8 by hotNipi

1 Like

Hey, thank you for the pointer. It's exactly that - a radio button set - that I was looking for.
The idea is that the user can select only one value to be used as a global variable later.
It could be done with a drop-down list, but it adds one interaction.
I have modified Urs-Eppenberger's flow to drop the true values and I think I have a working solution:


Screen Shot 2021-02-15 at 15.35.09

[{"id":"cc0949dd.83878","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"aa71b71d.4eadc","type":"ui_switch","z":"cc0949dd.83878","name":"","label":"250 g","tooltip":"","group":"e663eac3.db4718","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"250","onvalueType":"num","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":470,"y":600,"wires":[["e7c86ed9.285b78","ba213f16.841a9","1bf5ba97.a7f4bd"]]},{"id":"5f61c353.9031ac","type":"ui_switch","z":"cc0949dd.83878","name":"","label":"500 g","tooltip":"","group":"e663eac3.db4718","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"500","onvalueType":"num","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":470,"y":720,"wires":[["d416eca7.f00448","a59e4f6c.b8b7c8","1bf5ba97.a7f4bd"]]},{"id":"3d720dd5.f4e112","type":"debug","z":"cc0949dd.83878","name":"result_1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":600,"wires":[]},{"id":"8294eff4.aad8","type":"debug","z":"cc0949dd.83878","name":"result_2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":720,"wires":[]},{"id":"e7c86ed9.285b78","type":"switch","z":"cc0949dd.83878","name":"lose false","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":220,"y":720,"wires":[["70d2e7b1.76818"],[]]},{"id":"70d2e7b1.76818","type":"change","z":"cc0949dd.83878","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":720,"wires":[["5f61c353.9031ac"]]},{"id":"d416eca7.f00448","type":"switch","z":"cc0949dd.83878","name":"lose false","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":220,"y":600,"wires":[["d17918ec.82cd98"],[]]},{"id":"d17918ec.82cd98","type":"change","z":"cc0949dd.83878","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":600,"wires":[["aa71b71d.4eadc"]]},{"id":"ba213f16.841a9","type":"switch","z":"cc0949dd.83878","name":"lose false","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":640,"y":600,"wires":[["3d720dd5.f4e112"],[]]},{"id":"a59e4f6c.b8b7c8","type":"switch","z":"cc0949dd.83878","name":"lose false","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":640,"y":720,"wires":[["8294eff4.aad8"],[]]},{"id":"3dba110e.385ace","type":"ui_switch","z":"cc0949dd.83878","name":"","label":"60 g","tooltip":"","group":"e663eac3.db4718","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"60","onvalueType":"num","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":470,"y":840,"wires":[["ad2d9ffd.1dce1","e7c86ed9.285b78","d416eca7.f00448"]]},{"id":"e22f9f9e.583198","type":"debug","z":"cc0949dd.83878","name":"result_3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":840,"wires":[]},{"id":"1bf5ba97.a7f4bd","type":"switch","z":"cc0949dd.83878","name":"lose false","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":220,"y":840,"wires":[["142f795f.1f840f"],[]]},{"id":"142f795f.1f840f","type":"change","z":"cc0949dd.83878","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":840,"wires":[["3dba110e.385ace"]]},{"id":"ad2d9ffd.1dce1","type":"switch","z":"cc0949dd.83878","name":"lose false","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":640,"y":840,"wires":[["e22f9f9e.583198"],[]]},{"id":"e663eac3.db4718","type":"ui_group","name":"Select weight","tab":"8a77f740.9520c8","order":1,"disp":true,"width":"6","collapse":false},{"id":"8a77f740.9520c8","type":"ui_tab","name":"Dashboard","icon":"dashboard"}]
1 Like