UI switch behaviour

I have this flow. The UI switch is set by a signal from the LAN, but I also need to control it manually from the dashboard without generating a "loop", so I thought to include a "gate" function, but it is not working. The message is always passing thru.

[{"id":"c5bf3fb2.c7589","type":"inject","z":"fddf53d3.68236","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":240,"y":180,"wires":[["409f56cb.81b608"]]},{"id":"409f56cb.81b608","type":"ui_switch","z":"fddf53d3.68236","name":"","label":"Device","tooltip":"","group":"d98cf33c.c487e","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"gate","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":450,"y":60,"wires":[["5e469e53.4219b"]]},{"id":"5e469e53.4219b","type":"function","z":"fddf53d3.68236","name":"gate","func":"if (msg.topic === \"gate\") {\n return msg; // pass the message to following node\n}\n\nreturn null; // or drop it","outputs":1,"noerr":0,"initialize":"","finalize":"","x":650,"y":60,"wires":[["a419a442.0e8858"]]},{"id":"a419a442.0e8858","type":"debug","z":"fddf53d3.68236","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":60,"wires":[]},{"id":"d98cf33c.c487e","type":"ui_group","name":"Group 1","tab":"a0d76ec8.d2f6e","order":1,"disp":false,"width":"6","collapse":false},{"id":"a0d76ec8.d2f6e","type":"ui_tab","name":"Sinottico automazioni","icon":"mi-flip_to_front","order":1,"disabled":false,"hidden":false}]

Schermata 2021-02-04 alle 16.25.20

msg. topic is not always passed by the switch, try adding a msg property to the injected messages and test if true or false.
e.g.

[{"id":"e4e79045.294b2","type":"inject","z":"57675e72.26a0d8","name":"","props":[{"p":"payload"},{"p":"noPassThrough","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":170,"y":4860,"wires":[["75a7ac1b.53fdb4"]]},{"id":"75a7ac1b.53fdb4","type":"ui_switch","z":"57675e72.26a0d8","name":"","label":"Device","tooltip":"","group":"8b5cde76.edd58","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":340,"y":4840,"wires":[["5ac2d05c.3e73e"]]},{"id":"5ac2d05c.3e73e","type":"function","z":"57675e72.26a0d8","name":"gate","func":"if (!msg.noPassThrough ) {\n return msg; // pass the message to following node\n}\n\nreturn null; // or drop it","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":4840,"wires":[["ffe1c2b.d32564"]]},{"id":"ffe1c2b.d32564","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":720,"y":4840,"wires":[]},{"id":"8b5cde76.edd58","type":"ui_group","z":"","name":"","tab":"8f03e639.85956","order":1,"disp":true,"width":"6","collapse":false},{"id":"8f03e639.85956","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

Set the switch to not pass messages through and to Show the State of the input.
Then when you click the switch it will send the new state, that will be fed back in at the front by the signal from whatever you are controlling and that will set the switch to show the state.

For the future please see this post for how to share a flow here - How to share code or flow json

1 Like

Like this. The inject nodes simulate someone manually turning it on or off. The delay node is to simulate it going out to the real world and back again. It will work fine without it.

[{"id":"32f01a47.b485be","type":"ui_switch","z":"bdd7be38.d3b55","name":"","label":"switch","tooltip":"","group":"61285987.c20328","order":3,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":230,"y":4680,"wires":[["6883bf9d.0eb888"]]},{"id":"b4256330.5ce378","type":"link out","z":"bdd7be38.d3b55","name":"","links":["421f911a.4d2318"],"x":555,"y":4800,"wires":[]},{"id":"421f911a.4d2318","type":"link in","z":"bdd7be38.d3b55","name":"","links":["b4256330.5ce378"],"x":65,"y":4680,"wires":[["32f01a47.b485be"]]},{"id":"6883bf9d.0eb888","type":"delay","z":"bdd7be38.d3b55","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":410,"y":4720,"wires":[["b4256330.5ce378"]]},{"id":"35204c81.d0f9e4","type":"inject","z":"bdd7be38.d3b55","name":"Light on manually","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":320,"y":4820,"wires":[["b4256330.5ce378"]]},{"id":"2731fa90.8e4dd6","type":"inject","z":"bdd7be38.d3b55","name":"Light off manually","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":320,"y":4860,"wires":[["b4256330.5ce378"]]},{"id":"61285987.c20328","type":"ui_group","name":"Main","tab":"e7c46d5e.a1283","order":1,"disp":true,"width":"6","collapse":false},{"id":"e7c46d5e.a1283","type":"ui_tab","name":"Dashboard","icon":"dashboard"}]
1 Like

Thank you all. I got the point!

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