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

As a follow-up to the How to sync ui-switch with global.variable, 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

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

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

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

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

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

Which aspect? MQTT or how to use it in this case?
For MQTT see MQTT Essentials - All Core Concepts Explained

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.

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

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