# How to update ui\_switch state out off a different flow?

**URL:** https://discourse.nodered.org/t/how-to-update-ui-switch-state-out-off-a-different-flow/44833
**Category:** General
**Created:** [28 April 2021 06:31 UTC](https://discourse.nodered.org/t/how-to-update-ui-switch-state-out-off-a-different-flow/44833 "2021-04-28T06:31:13Z")
**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: [28 April 2021 06:31 UTC](https://discourse.nodered.org/t/how-to-update-ui-switch-state-out-off-a-different-flow/44833/1 "2021-04-28T06:31:13Z")

</div>

Hello Community,

may someone can share her/his thoughts about this and help with a hint.

I tried to turn an ui\_switch ON/OFF out of a different flow by setting a global.variable. The problem now is how to sync the state of the ui\_switch (e.g. in flow#1) with the global.variable (e.g. in flow#2)? I know this classic link-options shown in my flow but I wonder if there are other solutions to synchronize the global.variable set in a different flow (e.g. flow#2) with the state of the ui\_switch (e.g. in flow#1). Like a kind of "on-change-event" or so? I found the STATUS-Node but didn't got it working.

To use a timer to read out global.variable value and push it to the ui\_switch doesn't seem to be an elegant solution 🙃

```auto
[{"id":"104ea173.eaf9ff","type":"ui_switch","z":"bab42f9.6e663d","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":140,"y":120,"wires":[["f48189de.d44db8"]]},{"id":"f48189de.d44db8","type":"change","z":"bab42f9.6e663d","name":"","rules":[{"t":"set","p":"varTest1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":120,"wires":[["cf3c0c2b.3fbe5","104ea173.eaf9ff"]]},{"id":"2e3a9596.bd642a","type":"inject","z":"bab42f9.6e663d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":150,"y":180,"wires":[["f48189de.d44db8"]]},{"id":"cf3c0c2b.3fbe5","type":"debug","z":"bab42f9.6e663d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":120,"wires":[]},{"id":"45e2194a.83fe68","type":"inject","z":"bab42f9.6e663d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":150,"y":220,"wires":[["f48189de.d44db8"]]},{"id":"8d3c3728.1348b8","type":"inject","z":"bab42f9.6e663d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":150,"y":420,"wires":[["9ebcd414.26ef48"]]},{"id":"1a627e8d.49d3d1","type":"inject","z":"bab42f9.6e663d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":150,"y":380,"wires":[["9ebcd414.26ef48"]]},{"id":"9ebcd414.26ef48","type":"change","z":"bab42f9.6e663d","name":"","rules":[{"t":"set","p":"varTest1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":380,"wires":[["d6a5b885.76e518","89ae2399.040eb","104ea173.eaf9ff"]]},{"id":"d6a5b885.76e518","type":"debug","z":"bab42f9.6e663d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":380,"wires":[]},{"id":"b450a5a3.2c0bb8","type":"comment","z":"bab42f9.6e663d","name":"Can I avoid using this kind of connection?","info":"","x":440,"y":280,"wires":[]},{"id":"2b7f6532.0431aa","type":"link in","z":"bab42f9.6e663d","name":"link in","links":[],"x":555,"y":240,"wires":[["104ea173.eaf9ff"]]},{"id":"89ae2399.040eb","type":"link out","z":"bab42f9.6e663d","name":"link out","links":[],"x":555,"y":320,"wires":[]},{"id":"cb4e8138.4d894","type":"comment","z":"bab42f9.6e663d","name":"flow #2","info":"","x":70,"y":340,"wires":[]},{"id":"36c01f95.79d4b","type":"comment","z":"bab42f9.6e663d","name":"flow #1","info":"","x":70,"y":80,"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: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [28 April 2021 06:50 UTC](https://discourse.nodered.org/t/how-to-update-ui-switch-state-out-off-a-different-flow/44833/2 "2021-04-28T06:50:04Z")

</div>

As there is no way to know when a flow or global var changes except the ugly work around of polling the state with a timer that you already found I always recommend mqtt in those cases.  
You can even create states that persist with mqtt by using the retained flag.  
By pushing each state you want to work with to its own mqtt topic you can than just subscribe to this topic from anywhere where you want to act on a change.  
This even works across different machines / instances of Node-RED.

Johannes

---

<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 08:14 UTC](https://discourse.nodered.org/t/how-to-update-ui-switch-state-out-off-a-different-flow/44833/3 "2021-04-28T08:14:14Z")

</div>

-\>\> May someone has additional ideas - so feel free to share your thoughts.

Thanks for your reply **Johannes**!

From what I understood from the MQTT\_OUT node help-function is that the retained flag indicates that the mqtt broker has to "remember" the latest payload of a topic. Otherwise the mqtt message will be dropped and it's gone.

Your suggestion is easy to implement for me while I'm using mqtt anyway.

Best regards, Marco

Solution using mqtt:

```auto
[{"id":"104ea173.eaf9ff","type":"ui_switch","z":"bab42f9.6e663d","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":280,"y":120,"wires":[["f48189de.d44db8"]]},{"id":"f48189de.d44db8","type":"change","z":"bab42f9.6e663d","name":"","rules":[{"t":"set","p":"varTest1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":180,"wires":[["cf3c0c2b.3fbe5","104ea173.eaf9ff","c0904c4f.2491c"]]},{"id":"2e3a9596.bd642a","type":"inject","z":"bab42f9.6e663d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":90,"y":240,"wires":[["f48189de.d44db8"]]},{"id":"cf3c0c2b.3fbe5","type":"debug","z":"bab42f9.6e663d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":140,"wires":[]},{"id":"45e2194a.83fe68","type":"inject","z":"bab42f9.6e663d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":90,"y":280,"wires":[["f48189de.d44db8"]]},{"id":"8d3c3728.1348b8","type":"inject","z":"bab42f9.6e663d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":90,"y":460,"wires":[["9ebcd414.26ef48"]]},{"id":"1a627e8d.49d3d1","type":"inject","z":"bab42f9.6e663d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":90,"y":420,"wires":[["9ebcd414.26ef48"]]},{"id":"9ebcd414.26ef48","type":"change","z":"bab42f9.6e663d","name":"","rules":[{"t":"set","p":"varTest1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":420,"wires":[["d6a5b885.76e518","e0cfcb78.12e128"]]},{"id":"d6a5b885.76e518","type":"debug","z":"bab42f9.6e663d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":380,"wires":[]},{"id":"cb4e8138.4d894","type":"comment","z":"bab42f9.6e663d","name":"flow #2","info":"","x":70,"y":360,"wires":[]},{"id":"36c01f95.79d4b","type":"comment","z":"bab42f9.6e663d","name":"flow #1","info":"","x":70,"y":80,"wires":[]},{"id":"e0cfcb78.12e128","type":"mqtt out","z":"bab42f9.6e663d","name":"","topic":"switch1_state","qos":"","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"","x":520,"y":420,"wires":[]},{"id":"d28d444b.162be8","type":"mqtt in","z":"bab42f9.6e663d","name":"","topic":"switch1_state","qos":"2","datatype":"json","broker":"","nl":false,"rap":true,"rh":0,"x":90,"y":120,"wires":[["104ea173.eaf9ff","dbcf49cf.081c48"]]},{"id":"dbcf49cf.081c48","type":"debug","z":"bab42f9.6e663d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":290,"y":80,"wires":[]},{"id":"c0904c4f.2491c","type":"mqtt out","z":"bab42f9.6e663d","name":"","topic":"switch1_state","qos":"","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"","x":520,"y":180,"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}]

```

#search key words: mqtt global variable store

---

<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 08:15 UTC](https://discourse.nodered.org/t/how-to-update-ui-switch-state-out-off-a-different-flow/44833/4 "2021-05-12T08:15:01Z")

</div>

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