Version 1.0
·
Last edited on 2025-10-29
Retain Global Variables in Node Red on Restart
Contents
Attachments
No attachments
1. Prerequisites
-
- Node Red Container running
- “node-red-dashboard” nodes installed in Node Red
2. Open the Execute Console
-
- Go to your node red container in Portainer and click on the “Exec Console”
- Select “/bin/sh” as the command and press “Connect”
- Type the following commands:
- cd /data
- press enter
- vi settings.js
- press enter

- cd /data
- Scroll down and find “contextStorage” (should be around line 340)

- Type i (typing i allows us to edit)
- Press enter
- Make the following changes
- Remove the comment //
- Add a comma after “local file systems”
- Add config: { flushInterval: 5 }
- Add memory: { module: “memory” }
- The flushInterval (set at 5) refers to how long it will take to load the current global variable value into memory. With a value of 5, it will take 5 seconds before the value is retained. If power is cycled, or the Node Red container is stopped before 5 seconds after changing the value, it will not be saved.
- Press ESC
- Type
- :wq
- Additional Commands
- :wq à save and exit
- :q! à exit without saving
- :w à save but do not exit
- Additional Commands
- :wq
- Go to your node red container in Portainer and click on the “Exec Console”
Press Enter
3. Restart Node Red Container
-

- After the restart, confirm the Node Red container is healthy. While modifying the settings.js file from the execute console, if there are any syntax errors, the Node Red container will fail to restart properly. Since the container does not restart properly, you are unable to go back into the execute console to fix the syntax error. To correct this, follow the document “Fix Node Red Settings with Alpine”, or create a new volume for your Node Red container.
4. Open Node Red
-
- Import the “PersistGlobalVariable.json” flow into Node Red
- Inject Node – “Load retained at start”
- This is set inject 0.3 seconds after startup
- This is set inject 0.3 seconds after startup
- Function Node – “Load/init Temp_Threshold”
- In this example the global variable is “Temp_Threshold”. Change this name according to your application.
- In this example the global variable is “Temp_Threshold”. Change this name according to your application.
- Dashboard Node – “Contact Temperature Threshold”
- Configure the Dashboard node
- Configure the Dashboard node
- Function Node – “Persist Temp_Threshold”
- Change “Temp_Threshold” to match your application.

- Inject Node – “Load retained at start”
5. Open the Dashboard Web UI
-
- [CMTK IP address]:[Node Red Port Number]/ui
- 192.168.10.1:50000/ui
- Any number set from this Web UI will be retained when power is cycled, or the Node Red Container is restarted.
- [CMTK IP address]:[Node Red Port Number]/ui
6. Reference to Node Red JSON
- Node Red JSON code for reference
- [
{
"id": "6a87208a99f3fba6",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": "",
"env": []
},
{
"id": "inj_boot",
"type": "inject",
"z": "6a87208a99f3fba6",
"name": "Load retained at start",
"props": [],
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": "0.3",
"topic": "",
"x": 200,
"y": 480,
"wires": [
[
"fn_load_retained"
]
]
},
{
"id": "fn_load_retained",
"type": "function",
"z": "6a87208a99f3fba6",
"name": "Load/init Temp_Threshold",
"func": "const STORE = 'default';\nlet v = global.get('Temp_Threshold', STORE);\nif (v === undefined || v === null) {\n v = 30; // set your desired default once\n global.set('Temp_Threshold', v, STORE);\n}\nmsg.payload = v; // show in UI numeric\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 440,
"y": 480,
"wires": [
[
"ui_temp_num"
]
]
},
{
"id": "fn_save_threshold",
"type": "function",
"z": "6a87208a99f3fba6",
"name": "Persist Temp_Threshold",
"func": "global.set('Temp_Threshold', msg.payload, 'default');\nreturn null;",
"outputs": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1030,
"y": 480,
"wires": []
},
{
"id": "ui_temp_num",
"type": "ui_numeric",
"z": "6a87208a99f3fba6",
"name": "Contact Temperature Threshold",
"label": "Contact Temperature Threshold",
"tooltip": "",
"group": "grp1",
"order": 1,
"width": 0,
"height": 0,
"wrap": false,
"passthru": true,
"topic": "",
"topicType": "str",
"format": "{{value}}",
"min": 0,
"max": 200,
"step": 0.1,
"className": "",
"x": 750,
"y": 480,
"wires": [
[
"fn_save_threshold"
]
]
},
{
"id": "grp1",
"type": "ui_group",
"name": "Thresholds",
"tab": "tab1",
"order": 1,
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "tab1",
"type": "ui_tab",
"name": "Balluff Condition Monitoring",
"icon": "dashboard",
"order": 1
}
]
- [





