Products
Service & Support
Industries & Solutions
Company
Version 1.0
·
Last edited on 2025-10-29

Retain Global Variables in Node Red on Restart

1. Prerequisites

    1. Node Red Container running
    2. “node-red-dashboard” nodes installed in Node Red
      1. A white rectangular object with black textAI-generated content may be incorrect.

2. Open the Execute Console

    1. Go to your node red container in Portainer and click on the “Exec Console”
    2. Select “/bin/sh” as the command and press “Connect”
      1. A close-up of a screenAI-generated content may be incorrect.
    3. Type the following commands:
      1. cd /data
        1. press enter
      2. vi settings.js
        1. press enter
    4. Scroll down and find “contextStorage” (should be around line 340)
      1. A screen shot of a computerAI-generated content may be incorrect.
      2. Type i (typing i allows us to edit)
        1. Press enter
    5. Make the following changes
      1. Remove the comment //
      2. Add a comma after “local file systems”
      3. Add config: { flushInterval: 5 }
      4. Add memory: { module: “memory” }
      5. A screenshot of a computerAI-generated content may be incorrect.
        1. 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.
      6. Press ESC
      7. Type
        1. :wq
          1. Additional Commands
            1. :wq à save and exit
            2. :q! à exit without saving
            3. :w à save but do not exit

Press Enter

3. Restart Node Red Container

    1. 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

    1. Import the “PersistGlobalVariable.json” flow into Node Red
      1. Inject Node – “Load retained at start”
        1. This is set inject 0.3 seconds after startup
          1. A screenshot of a computerAI-generated content may be incorrect.
      2. Function Node – “Load/init Temp_Threshold”
        1. In this example the global variable is “Temp_Threshold”.  Change this name according to your application.
          1. A computer code with yellow textAI-generated content may be incorrect.
      3. Dashboard Node – “Contact Temperature Threshold”
        1. Configure the Dashboard node
          1. A screenshot of a computerAI-generated content may be incorrect.
      4. Function Node – “Persist Temp_Threshold”
        1. Change “Temp_Threshold” to match your application.
        2. A close-up of a signAI-generated content may be incorrect.

5. Open the Dashboard Web UI

    1. [CMTK IP address]:[Node Red Port Number]/ui
      1. 192.168.10.1:50000/ui
      2. Any number set from this Web UI will be retained when power is cycled, or the Node Red Container is restarted.
        1. A screenshot of a computerAI-generated content may be incorrect.

6. Reference to Node Red JSON

  1. Node Red JSON code for reference
    1. [
          {
              "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
          }
      ]
能耗标签
能耗标签

EPREL - 欧洲能源标签产品数据库

Free sample product

In order to add a free sample product to the cart we will need to remove all the normal products from the cart. Are you sure you want to continue