# How to sync ui-switch with global.variable (follow-up)

**URL:** <https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051>\
**Category:** Dashboard\
**Created:** [1 November 2021 09:35 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051 "2021-11-01T09:35:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![magnusso](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/magnusso/32/37680_2.png) [@magnusso](https://discourse.nodered.org/u/magnusso)\
**Post date:** [1 November 2021 09:35 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051/1 "2021-11-01T09:35:05Z")

</div>

As a follow-up to the [How to sync ui-switch with global.variable](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-infinit-loop-problem/44823), the sync only only works in one direction, from switch to global variable, but note vice versa. Any suggestion of how to make it work in both directions?

![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/168fdd0cf2e784a81e8380218451ea99245d34aa.jpeg)

```auto
[{"id":"85f73f8b.75843","type":"ui_switch","z":"5ce1ba35.e70844","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":1640,"y":1000,"wires":[["8dfdf81f.377ce8"]]},{"id":"8dfdf81f.377ce8","type":"change","z":"5ce1ba35.e70844","name":"","rules":[{"t":"set","p":"varTest1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1840,"y":1000,"wires":[["85f73f8b.75843"]]},{"id":"f8cad404.4500f8","type":"change","z":"5ce1ba35.e70844","name":"","rules":[{"t":"set","p":"varTest1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1850,"y":1080,"wires":[[]]},{"id":"f9e97d99.dabb1","type":"inject","z":"5ce1ba35.e70844","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":1630,"y":1080,"wires":[["f8cad404.4500f8"]]},{"id":"d8ddfdb4.23cb","type":"inject","z":"5ce1ba35.e70844","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":1630,"y":1140,"wires":[["f8cad404.4500f8"]]},{"id":"ec871ba7.2cc968","type":"inject","z":"5ce1ba35.e70844","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"date","x":1640,"y":1200,"wires":[["b796a4cd.65bcd8"]]},{"id":"135b7996.cf8706","type":"debug","z":"5ce1ba35.e70844","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2010,"y":1200,"wires":[]},{"id":"b796a4cd.65bcd8","type":"change","z":"5ce1ba35.e70844","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"varTest1","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":1820,"y":1200,"wires":[["135b7996.cf8706"]]},{"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:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 November 2021 09:51 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051/2 "2021-11-01T09:51:32Z")

</div>

Personally I would use MQTT Retained variables for this, not global vars, then it is very easy.

---

<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:** [1 November 2021 09:53 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051/3 "2021-11-01T09:53:23Z")

</div>

OR set up wires differently, us of link nodes aswell if in different flows

```auto
[{"id":"85f73f8b.75843","type":"ui_switch","z":"b779de97.b1b46","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":200,"y":4780,"wires":[["8dfdf81f.377ce8"]]},{"id":"8dfdf81f.377ce8","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"varTest1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":4780,"wires":[["85f73f8b.75843","135b7996.cf8706"]]},{"id":"135b7996.cf8706","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":570,"y":4720,"wires":[]},{"id":"1aaf6769.8baa01","type":"link in","z":"b779de97.b1b46","name":"","links":["a727966f.c0d3e"],"x":355,"y":4760,"wires":[["8dfdf81f.377ce8"]]},{"id":"f9e97d99.dabb1","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":190,"y":4860,"wires":[["a727966f.c0d3e"]]},{"id":"a727966f.c0d3e","type":"link out","z":"b779de97.b1b46","name":"","links":["1aaf6769.8baa01"],"x":305,"y":4900,"wires":[]},{"id":"d8ddfdb4.23cb","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":190,"y":4920,"wires":[["a727966f.c0d3e"]]},{"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:** ![magnusso](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/magnusso/32/37680_2.png) [@magnusso](https://discourse.nodered.org/u/magnusso)\
**Post date:** [1 November 2021 11:53 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051/4 "2021-11-01T11:53:56Z")

</div>

> [@Colin](#):
>
> Personally I would use MQTT Retained variables for this, not global vars, then it is very easy.

@Colin , could you please provide som further information on this, e.g. a link or an example?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 November 2021 15:44 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051/5 "2021-11-01T15:44:06Z")

</div>

> [@magnusso](#):
>
> could you please provide som further information on this

Which aspect? MQTT or how to use it in this case?  
For MQTT see [MQTT Essentials - All Core Concepts Explained](https://www.hivemq.com/mqtt-essentials/)

---

<div class="post-metadata">

**Author:** ![magnusso](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/magnusso/32/37680_2.png) [@magnusso](https://discourse.nodered.org/u/magnusso)\
**Post date:** [1 November 2021 18:04 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051/6 "2021-11-01T18:04:02Z")

</div>

I'm familiar with the fundamentals of MQTT, but I fail to see why and how to use MQTT as a replacement for Node Red global variables.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 November 2021 20:25 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051/7 "2021-11-01T20:25:21Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/ae88af781afcea3bd26d4c037eb38b8488d6fa35.png)

```auto
[{"id":"ce881f34665956f1","type":"mqtt in","z":"bdd7be38.d3b55","name":"","topic":"test/switch","qos":"1","datatype":"auto","broker":"adf2dac3.b20fe8","nl":false,"rap":true,"rh":0,"inputs":0,"x":190,"y":2600,"wires":[["f6c485268cf2ade1"]]},{"id":"f6c485268cf2ade1","type":"ui_switch","z":"bdd7be38.d3b55","name":"","label":"switch","tooltip":"","group":"a2fd251646d3069d","order":1,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"test/switch","topicType":"str","style":"","onvalue":"On","onvalueType":"str","onicon":"","oncolor":"","offvalue":"Off","offvalueType":"str","officon":"","offcolor":"","animate":false,"className":"","x":350,"y":2600,"wires":[["58b46f856165adf2"]]},{"id":"58b46f856165adf2","type":"mqtt out","z":"bdd7be38.d3b55","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"adf2dac3.b20fe8","x":590,"y":2600,"wires":[]},{"id":"7686b09fec1f5052","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/switch","payload":"On","payloadType":"str","x":470,"y":2740,"wires":[["491f3ce956386e7c"]]},{"id":"7cc43183c86b2754","type":"comment","z":"bdd7be38.d3b55","name":"Simulate the external deivice being manually switched","info":"","x":340,"y":2700,"wires":[]},{"id":"2c31a386bc8ef0bf","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/switch","payload":"Off","payloadType":"str","x":470,"y":2780,"wires":[["491f3ce956386e7c"]]},{"id":"491f3ce956386e7c","type":"mqtt out","z":"bdd7be38.d3b55","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"adf2dac3.b20fe8","x":650,"y":2760,"wires":[]},{"id":"977876c5134d39d7","type":"mqtt in","z":"bdd7be38.d3b55","name":"","topic":"test/switch","qos":"1","datatype":"auto","broker":"adf2dac3.b20fe8","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":2900,"wires":[[]]},{"id":"45ee5975b492ba49","type":"comment","z":"bdd7be38.d3b55","name":"Feed this into anywhere else that needs to react to switch state, on any computer in the system","info":"","x":520,"y":2900,"wires":[]},{"id":"adf2dac3.b20fe8","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"a2fd251646d3069d","type":"ui_group","name":"Test","tab":"3febc928.86e386","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"3febc928.86e386","type":"ui_tab","name":"Tab 1","icon":"dashboard","order":1}]

```

When using a ui\_switch in this way make sure that message pass through is _not_ selected, and that the switch is set to Show State of Input.  
If you make the topic Retained then it can automatically set the ui and the external device back to the state that it was in prior to node-red or system restart.

---

<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:** [1 December 2021 20:25 UTC](https://discourse.nodered.org/t/how-to-sync-ui-switch-with-global-variable-follow-up/53051/8 "2021-12-01T20:25:56Z")

</div>

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