Backing up and restoring global variables

Global variables are likely to be used in most processes. When Node Red restarts these values are lost. This flow gives an option on how to back up and restore global variables automatically in Node Red.

This example extracts selected global variables and backs them up to a local file in JSON format. I am triggering backup using an inject node to backup global variables daily.

Restore is triggered on system startup, reads the backup file and creates the global variables.

This example is designed to back up and restore number, string and boolean type global variables. I believe there is sufficient checks in the process so it does not fail is the global variable does not exist at the time of the backup.

If you prefer watching my video on this, follow this link: https://youtu.be/JYQXF9JlOsg

[{"id":"43f75a01.74e884","type":"inject","z":"15e1f6a1.aeab19","name":"Daily backup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 2 * * *","once":false,"x":154.6666717529297,"y":997.9999523162842,"wires":[["d547483e.775438"]]},{"id":"d547483e.775438","type":"function","z":"15e1f6a1.aeab19","name":"Prep data","func":"// global variables separated by semicolon to be saved\nvar globallist = \"btn_state;btn_time;timehour;timeminute;start;irrenable;irrdays;alert1_triggered;alert1_value;alert2_triggered;alert2_value;torrent_keywords;tonernotification\";\nvar mylist = globallist.split(\";\");\n\nvar outputs = [];\n\nfor (i=0; i<mylist.length; i++) {\n    outputs.push({ key : mylist[i], type: typeof global.get(mylist[i]), value: global.get(mylist[i])});\n}\n      \n      \nmsg.payload = outputs;\nreturn msg;\n\n//msg.payload=typeof global.get(\"torrent_keywords\");\n","outputs":1,"noerr":0,"x":327.6666717529297,"y":1000.9999523162842,"wires":[["2c2d4269.a876de"]]},{"id":"9cec5c0d.e6de","type":"comment","z":"15e1f6a1.aeab19","name":"Save global variables","info":"","x":146.6666717529297,"y":952.9999523162842,"wires":[]},{"id":"666ca426.5be96c","type":"comment","z":"15e1f6a1.aeab19","name":"Restore global variables","info":"","x":154.6666717529297,"y":1043.9999523162842,"wires":[]},{"id":"8e7e958d.ce6698","type":"inject","z":"15e1f6a1.aeab19","name":"Startup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":115.66667175292969,"y":1090.9999523162842,"wires":[["d8fa7064.9f60a"]]},{"id":"4cb14d0.afc25b4","type":"file","z":"15e1f6a1.aeab19","name":"","filename":"/home/pi/global.json","appendNewline":true,"createDir":false,"overwriteFile":"true","x":783.6666870117188,"y":1011.9999389648438,"wires":[]},{"id":"d8fa7064.9f60a","type":"file in","z":"15e1f6a1.aeab19","name":"","filename":"/home/pi/global.json","format":"utf8","x":334.6666717529297,"y":1090.9999523162842,"wires":[["3cdb4133.0d461e"]]},{"id":"14b4208c.99aaaf","type":"function","z":"15e1f6a1.aeab19","name":"Restore","func":"var output = [];\n\nfor (var i=0; i<msg.payload.length; i++) {\n    switch (msg.payload[i].type) {\n        case 'undefined': \n            // the global variable probably had no value, nothing needs to be restored\n            output.push(msg.payload[i].key + \" is undefined.\");\n            break;\n        case 'number':\n            if (msg.payload[i].value===\"NaN\") {\n                // there is no valid value to be restored, skip this variable\n                output.push(msg.payload[i].key + \" is NaN.\");\n            } else {\n                if (msg.payload[i].value.toString().indexOf(\".\")>-1) {\n                    // the value appears to be a float\n                    global.set(msg.payload[i].key,parseFloat(msg.payload[i].value));\n                    output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n                } else {\n                    global.set(msg.payload[i].key,parseInt(msg.payload[i].value));\n                    output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n                }\n            }\n            break;\n        case 'string':\n            global.set(msg.payload[i].key,msg.payload[i].value);\n            output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n            break;\n        case 'boolean':\n            global.set(msg.payload[i].key,msg.payload[i].value===\"true\");\n            output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n            break;\n    }\n}\n\nmsg.payload = output;\nreturn msg;","outputs":1,"noerr":0,"x":125.66667175292969,"y":1165.9999523162842,"wires":[["13dbc787.3648b8","700f64c7.16704c","3c1c5d80.84cb92","4b5389e8.7ec3f8","eabd9285.07a73","d0d41491.fa7428","b7839544.79e088"]]},{"id":"2c2d4269.a876de","type":"json","z":"15e1f6a1.aeab19","name":"","x":556.6666717529297,"y":1009.9999523162842,"wires":[["4cb14d0.afc25b4"]]},{"id":"3cdb4133.0d461e","type":"json","z":"15e1f6a1.aeab19","name":"","x":540.6666717529297,"y":1090.9999523162842,"wires":[["14b4208c.99aaaf"]]},{"id":"13dbc787.3648b8","type":"function","z":"15e1f6a1.aeab19","name":"Alert 1 restore","func":"msg.topic = \"alert1\";\nmsg.payload = global.get(\"alert1_value\");\nreturn msg;","outputs":1,"noerr":0,"x":353.6666717529297,"y":1166.9999523162842,"wires":[["fa6f14b5.9e8898"]]},{"id":"700f64c7.16704c","type":"function","z":"15e1f6a1.aeab19","name":"Alert 2 restore","func":"msg.topic = \"alert2\";\nmsg.payload = global.get(\"alert2_value\");\nreturn msg;","outputs":1,"noerr":0,"x":354.6666717529297,"y":1210.9999523162842,"wires":[["441c4e8e.0d9c9"]]},{"id":"fa6f14b5.9e8898","type":"link out","z":"15e1f6a1.aeab19","name":"","links":["33ea2a87.bbf216"],"x":572.6666717529297,"y":1165.9999523162842,"wires":[]},{"id":"441c4e8e.0d9c9","type":"link out","z":"15e1f6a1.aeab19","name":"","links":["fb56e62a.86d0a8"],"x":574.6666717529297,"y":1209.9999523162842,"wires":[]},{"id":"3c1c5d80.84cb92","type":"function","z":"15e1f6a1.aeab19","name":"Irrigation hour restore","func":"msg.topic = \"timehour\";\nmsg.payload = global.get(\"timehour\");\nreturn msg;","outputs":1,"noerr":0,"x":376.6666717529297,"y":1252.9999523162842,"wires":[["a7481f7.b0107e"]]},{"id":"a7481f7.b0107e","type":"link out","z":"15e1f6a1.aeab19","name":"","links":["822055f4.a4b038"],"x":576.6666717529297,"y":1251.9999523162842,"wires":[]},{"id":"4b5389e8.7ec3f8","type":"function","z":"15e1f6a1.aeab19","name":"Irrigation minute restore","func":"msg.topic = \"timeminute\";\nmsg.payload = global.get(\"timeminute\");\nreturn msg;","outputs":1,"noerr":0,"x":381.6666717529297,"y":1296.9999523162842,"wires":[["6566100b.6370a"]]},{"id":"6566100b.6370a","type":"link out","z":"15e1f6a1.aeab19","name":"","links":["2312fe3d.563952"],"x":571.6666717529297,"y":1295.9999523162842,"wires":[]},{"id":"eabd9285.07a73","type":"function","z":"15e1f6a1.aeab19","name":"Irrigation enabler restore","func":"msg.topic = \"irrenable\";\nmsg.payload = global.get(\"irrenable\");\nreturn msg;","outputs":1,"noerr":0,"x":376.6666717529297,"y":1340.9999523162842,"wires":[["dd8532c3.a1061"]]},{"id":"dd8532c3.a1061","type":"link out","z":"15e1f6a1.aeab19","name":"","links":["4e8c0522.fe637c"],"x":566.6666717529297,"y":1339.9999523162842,"wires":[]},{"id":"d0d41491.fa7428","type":"function","z":"15e1f6a1.aeab19","name":"Irrigation enabler restore","func":"msg.topic = \"irrdays\";\nmsg.payload = global.get(\"irrdays\");\nreturn msg;","outputs":1,"noerr":0,"x":378.6666717529297,"y":1385.9999523162842,"wires":[["2dd73717.36ac78"]]},{"id":"2dd73717.36ac78","type":"link out","z":"15e1f6a1.aeab19","name":"","links":["b42ee7b4.c38618"],"x":568.6666717529297,"y":1384.9999523162842,"wires":[]},{"id":"b7839544.79e088","type":"function","z":"15e1f6a1.aeab19","name":"Torrent keyword restore","func":"msg.topic = \"torrent_keywords\";\nmsg.payload = global.get(\"torrent_keywords\");\nreturn msg;","outputs":1,"noerr":0,"x":381.6666717529297,"y":1429.9999523162842,"wires":[["95c19abc.16a1f8"]]},{"id":"95c19abc.16a1f8","type":"link out","z":"15e1f6a1.aeab19","name":"","links":["e3a03eba.fa3b4"],"x":571.6666717529297,"y":1428.9999523162842,"wires":[]}]
nygma2004

Flow Info

created 4 months, 3 weeks ago

Node Types

Core
  • comment (x2)
  • file (x1)
  • file in (x1)
  • function (x9)
  • inject (x2)
  • json (x2)
Other
  • link out (x7)

Tags

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