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

**URL:** https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-infinit-loop-problem/44823
**Category:** Dashboard
**Created:** [27 April 2021 19:45 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-infinit-loop-problem/44823 "2021-04-27T19:45:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Marcow](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Marcow](https://discourse.nodered.org/u/Marcow)
#### Post date: [27 April 2021 19:45 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-infinit-loop-problem/44823/1 "2021-04-27T19:45:45Z")

</div>

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

```auto
[{"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}]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [27 April 2021 20:14 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-infinit-loop-problem/44823/2 "2021-04-27T20:14:00Z")

</div>

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

```auto
[{"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}]

```

---

<div class="post-metadata">

### Author: ![Marcow](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Marcow](https://discourse.nodered.org/u/Marcow)
#### Post date: [28 April 2021 05:50 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-infinit-loop-problem/44823/3 "2021-04-28T05:50:34Z")

</div>

> [@E1cid](#):
>
> `05354`

- solved -  
Thank you E1cid! That was an easy one ;o)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [12 May 2021 05:51 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-infinit-loop-problem/44823/4 "2021-05-12T05:51:15Z")

</div>

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