Examples to persist and share values using MQTT and context

You may persist data on reboot using MQTT or store data in context for sharing in functions.

[{"id":"6c0fab79.0e5b74","type":"mqtt in","z":"5b475aaa.0d7364","name":"","topic":"Demo/Text","qos":"2","broker":"ef3d1f53.8d09a","x":105.07645416259766,"y":147.82288455963135,"wires":[["119ff418.c1c87c"]]},{"id":"77599908.d871d8","type":"mqtt out","z":"5b475aaa.0d7364","name":"","topic":"","qos":"2","retain":"true","broker":"ef3d1f53.8d09a","x":452.0730094909668,"y":147.7499749660492,"wires":[]},{"id":"94202bbb.1395b8","type":"mqtt in","z":"5b475aaa.0d7364","name":"","topic":"Demo/Bool","qos":"0","broker":"ef3d1f53.8d09a","x":105.01735305786133,"y":101.01040172576904,"wires":[["f32bba65.222518"]]},{"id":"f32bba65.222518","type":"ui_switch","z":"5b475aaa.0d7364","name":"","label":"Run1","group":"7b11ed0f.750644","order":0,"width":0,"height":0,"passthru":false,"topic":"Demo/Bool","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":268.01736068725586,"y":101.01040172576904,"wires":[["77599908.d871d8","daa28b7.4974c78"]]},{"id":"119ff418.c1c87c","type":"ui_text_input","z":"5b475aaa.0d7364","name":"","label":"","group":"7b11ed0f.750644","order":0,"width":0,"height":0,"passthru":false,"mode":"text","delay":300,"topic":"Demo/Text","x":268.0833396911621,"y":147.43400168418884,"wires":[["77599908.d871d8","daa28b7.4974c78"]]},{"id":"daa28b7.4974c78","type":"debug","z":"5b475aaa.0d7364","name":"","active":true,"console":"false","complete":"false","x":472.0764808654785,"y":101.60764217376709,"wires":[]},{"id":"35be3df5.23c7b2","type":"function","z":"5b475aaa.0d7364","name":"global.set","func":"\nif (msg.payload == 1)\n{\n    global.set(\"enable\", 1);\n    return msg;\n}\nelse\n{\n    global.set(\"enable\", 0);     \n}\n\n","outputs":1,"noerr":0,"x":261.06944274902344,"y":284.3021478652954,"wires":[["65e1fa60.546634"]]},{"id":"c507f2f5.6c809","type":"ui_switch","z":"5b475aaa.0d7364","name":"","label":"Enable","group":"ad2ac2df.6f4bf","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"EnableBtn","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":105.06947326660156,"y":284.3993253707886,"wires":[["35be3df5.23c7b2"]]},{"id":"3c3bd546.e7eb4a","type":"ui_button","z":"5b475aaa.0d7364","name":"","group":"ad2ac2df.6f4bf","order":0,"width":0,"height":0,"passthru":false,"label":"Go!","color":"","bgcolor":"","icon":"","payload":"","payloadType":"date","topic":"GoBtn","x":106.07992553710938,"y":333.47222423553467,"wires":[["f8cb8564.85e8a8"]]},{"id":"eba55760.5c1758","type":"ui_text","z":"5b475aaa.0d7364","group":"ad2ac2df.6f4bf","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":449.076473236084,"y":332.1563243865967,"wires":[]},{"id":"f8cb8564.85e8a8","type":"function","z":"5b475aaa.0d7364","name":"global.read","func":"var enable = global.get(\"enable\");\n\nif (enable == 1)\n{\n    return msg;\n}\n\n","outputs":1,"noerr":0,"x":271.01733779907227,"y":333.0104103088379,"wires":[["eba55760.5c1758","65e1fa60.546634"]]},{"id":"65e1fa60.546634","type":"debug","z":"5b475aaa.0d7364","name":"","active":true,"console":"false","complete":"false","x":467.0695037841797,"y":284.5937509536743,"wires":[]},{"id":"43403932.42d7d8","type":"comment","z":"5b475aaa.0d7364","name":"Persist values on reboot using MQTT","info":"","x":195.07640075683594,"y":52.17361640930176,"wires":[]},{"id":"59902e0c.7247b","type":"comment","z":"5b475aaa.0d7364","name":"Share values globally for functions","info":"","x":194.01734924316406,"y":243.0103988647461,"wires":[]},{"id":"ef3d1f53.8d09a","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"NodeRedUI","usetls":false,"compatmode":true,"keepalive":"15","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"7b11ed0f.750644","type":"ui_group","z":"","name":"MQTT","tab":"670bfbe.e66d904","disp":true,"width":"6"},{"id":"ad2ac2df.6f4bf","type":"ui_group","z":"","name":"Context","tab":"670bfbe.e66d904","disp":true,"width":"6"},{"id":"670bfbe.e66d904","type":"ui_tab","z":"","name":"Demo","icon":"dashboard"}]
ElectronHacks

Flow Info

created 2 weeks, 2 days ago

Node Types

Core
  • comment (x2)
  • debug (x2)
  • function (x2)
  • mqtt in (x2)
  • mqtt out (x1)
  • mqtt-broker (x1)
Other
  • ui_button (x1)
  • ui_group (x2)
  • ui_switch (x2)
  • ui_tab (x1)
  • ui_text (x1)
  • ui_text_input (x1)

Tags

  • Persist
  • store
  • variables
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option