How to sync ui-switch with global.variable (infinit loop problem)

Hello Commnunity,
may someone can push me into the right direction.

I simply want to have a ui_switch on the dashboard (ON/OFF) synchronized with a global.variable (also e.g. ON/OFF). Goal is that I'm able to switch a device ON and OFF using the ui_switch on the dashboard and also switch the same device via the global.variable from any Node-RED tab. Both, the value of the global.variable and the state of the ui_switch, should indicate the same state any time.

The device should be turned ON/OFF by checking the global.variable state and send it to the device. I also have to use the global.variable value in other flows (e.g. if device is ON then...).

I tried several approaches but came always to the point where I got no sync between the two components or generated an infinite loop. I'm not familiar with coding so may an obvious and easy solution is something I don't see.

Thanks for your support!
Regards
Marco

[{"id":"d16d511d.05354","type":"ui_switch","z":"3aab1549.14779a","name":"","label":"switch1","tooltip":"","group":"7d6bc9d7.e86ff8","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":440,"y":140,"wires":[["1f9cf97e.8e6d67"]]},{"id":"3681d3c5.9dd3ac","type":"inject","z":"3aab1549.14779a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":450,"y":200,"wires":[["1f9cf97e.8e6d67"]]},{"id":"8d284cfa.0fd74","type":"debug","z":"3aab1549.14779a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":140,"wires":[]},{"id":"1f9cf97e.8e6d67","type":"change","z":"3aab1549.14779a","name":"","rules":[{"t":"set","p":"varTest1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":140,"wires":[["8d284cfa.0fd74","d16d511d.05354"]]},{"id":"3ba1ca0c.de4516","type":"inject","z":"3aab1549.14779a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":450,"y":240,"wires":[["1f9cf97e.8e6d67"]]},{"id":"410040de.6ac3f","type":"comment","z":"3aab1549.14779a","name":"Example does not sync ui_switch1 state and global.varTest1","info":"","x":580,"y":80,"wires":[]},{"id":"a2b1e75.d706d18","type":"comment","z":"3aab1549.14779a","name":"If \"Pass through msg if payload matches new state: \" is checked at ui_switch1 it will lead into a infinite loop :o((","info":"","x":730,"y":280,"wires":[]},{"id":"7d6bc9d7.e86ff8","type":"ui_group","name":"Group 1","tab":"8375fe35.96f7","order":1,"disp":true,"width":6},{"id":"8375fe35.96f7","type":"ui_tab","name":"Problem","icon":"dashboard","order":14,"disabled":false,"hidden":false}]

Uncheck pass through message and set indicator to show icon on state of input.
eg.

[{"id":"d16d511d.05354","type":"ui_switch","z":"9b3f9f31.c45298","name":"","label":"switch1","tooltip":"","group":"7d6bc9d7.e86ff8","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,"x":220,"y":300,"wires":[["1f9cf97e.8e6d67"]]},{"id":"1f9cf97e.8e6d67","type":"change","z":"9b3f9f31.c45298","name":"","rules":[{"t":"set","p":"varTest1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":300,"wires":[["8d284cfa.0fd74","d16d511d.05354"]]},{"id":"3681d3c5.9dd3ac","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":230,"y":360,"wires":[["1f9cf97e.8e6d67"]]},{"id":"8d284cfa.0fd74","type":"debug","z":"9b3f9f31.c45298","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":300,"wires":[]},{"id":"3ba1ca0c.de4516","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":230,"y":400,"wires":[["1f9cf97e.8e6d67"]]},{"id":"7d6bc9d7.e86ff8","type":"ui_group","name":"Group 1","tab":"8375fe35.96f7","order":1,"disp":true,"width":6},{"id":"8375fe35.96f7","type":"ui_tab","name":"Problem","icon":"dashboard","order":14,"disabled":false,"hidden":false}]
  • solved -
    Thank you E1cid! That was an easy one ;o)

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