How to update ui_switch state out off a different flow?

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 :upside_down_face:

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

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

->> 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:

[{"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

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