IMD SolutionComputing LibraryShop main store

Troubleshooting

Node-RED OPC UA server persistence after a restart

Edge computing and IIoT Published 7 min read By IMD Solution engineering team

Short answer

Treat a Node-RED OPC UA server restart as two persistence problems. Save the address space — nodes, NodeIds and data types — to a nodeset.xml file with the save/load function node-red-contrib-opcua added in version 0.2.256, and keep the changing values in Node-RED's file-based context store, writing each one back to its node after the NodeSet loads. Restore only values your own flow owns; let device-owned tags refill from the device.

On this page
  1. Sort the symptom before configuring anything
  2. Check the installed version first
  3. Save and reload the address space as a NodeSet
  4. Keep the values in file-based context
  5. Restore in a fixed order, and only what you own
  6. Worked example: one setpoint across two restarts
  7. Faults that come back
  8. Questions asked

An OPC UA server built in Node-RED loses two different things at a restart, and two different mechanisms save them. The browsable structure — variables, NodeIds, data types, folder hierarchy — comes back only from a NodeSet XML file, which node-red-contrib-opcua can save and load since version 0.2.256. The runtime values never ride along with that file: they survive a restart only if your flow keeps them in Node-RED's file-based context store and writes each one back to its node after the NodeSet is in place. Configure both, in that order, and the server comes back the way your clients expect.

Sort the symptom before configuring anything #

Restart Node-RED once and browse the server's namespace with the OPC UA client you already use. The three possible outcomes point to three different fixes:

  • The variable is gone from the namespace. The address-space definition was never restored. Run the version check below, then set up the NodeSet path.
  • The variable is back, but its value is empty, a default or stale. The structure survived; the value did not. Set up the file-based context path and the ordered write-back.
  • The variable and its value both return. Persistence already works for that item. Verify its data type and NodeId against your client configuration, then repeat the test after a second restart before calling it done.

A variable's definition and its live value have separate lifecycles. The definition supplies the browsable node, its NodeId and its data type; the value changes every time the process or an operator moves it. Identify which of the two failed before you change any configuration, because the fixes do not substitute for each other.

Check the installed version first #

The save/load address-space feature entered node-red-contrib-opcua in version 0.2.256, tracked as issue #374 in the project's GitHub repository. The palette manager in the Node-RED editor lists the installed version of every package, so read it there before designing anything around the file.

If the installed release predates that one, decide deliberately: upgrade the package on a bench gateway and retest the server before touching production, or keep building the address space in the flow exactly as you do now and persist only the values through context. Both are valid. The second needs no upgrade, but it recreates nodes from the flow at every start — precisely the manual work the NodeSet removes.

Save and reload the address space as a NodeSet #

Build the required variables in the OpcUa-Server node, then use the save-address-space action that version 0.2.256 and later provide to write the definition to a nodeset.xml file. Confirm the file exists and is not empty before relying on it. If the save fails with a filesystem error, check the permissions of the folder Node-RED writes to first — the same mechanism behind the article on why a Node-RED deploy fails on OPC UA certificate file permissions. Which variables a server should expose, and how to structure them for an edge gateway, is the subject of the article on Node-RED OPC UA server node selection.

The load is not instantaneous: the feature's issue entry notes that loading the address space requires a server restart. The working sequence is therefore: save the file, select it for loading in the server node, restart Node-RED, then browse. Check every required variable against the commissioned list — presence, NodeId and data type. A matching browse name alone is not enough, because a client writes to the resolved node, not to a displayed label.

What a loaded NodeSet changes for clients — including whether it eliminates client browsing — is the subject of the knowledge-base article Does OPC UA NodeSet XML eliminate client browsing?

Keep the file's role straight: it restores structure, not history. Nothing in the save/load feature records the values written after the file was made, so the flow has to keep those itself.

Keep the values in file-based context #

Node-RED context is memory-only by default and cleared at every restart; configurable context stores exist since the 0.19 release. Enable the file-based context store in settings.js and route every value that must survive through it.

Three rules make the store trustworthy:

  • Key each entry by the node's identity. Tie the context key to the variable's NodeId so the restore step finds it unambiguously; for example, the key LineSpeed_Setpoint held against the NodeId string ns=3;s=LineSpeed_Setpoint.
  • Store plain types. Numbers, strings, booleans and JSON objects round-trip through the file store; keep the data type alongside the value so the write-back can be validated before it runs.
  • Update on every accepted change. A store refreshed only at deploy time holds a value that ages with every accepted write the flow misses.

Read the entry back through context before testing: the returned value and type must match the last accepted update. Then run every test in this article as a clean restart — how the store behaves after an uncontrolled shutdown is its own question, covered at the end of this page.

Restore in a fixed order, and only what you own #

Startup writes that race a server still building its namespace produce failed writes, type mismatches or a stale value overwriting a fresh reading. Run the restore as a fixed sequence:

  1. Start the server and load the NodeSet file.
  2. Restart so the load takes effect, then confirm the target node exists — browse it or read its NodeId.
  3. Read the context entry and check its stored data type against the node's configured type.
  4. Write the stored value to the node and read it back through OPC UA.
  5. Only then allow normal client writes and device synchronization to resume.

If the write-back itself errors while reads on the same node still succeed, the causes are collected in the article on why OPC UA writes fail while reads stay good.

Decide authority before enabling any automatic write-back: restore a persisted value only to nodes your flow owns, such as setpoints entered at the panel. For a measurement the connected device owns, recreate the node and let the device refill it — do not present a cached reading as current. A restored setpoint the device has since superseded can drive the process toward a target nobody chose, so verify against the device before turning write-back on.

Worked example: one setpoint across two restarts #

All numbers here are an example; substitute your own tag, type and units. Variable LineSpeed_Setpoint, data type Double, NodeId ns=3;s=LineSpeed_Setpoint, owned by the flow. At 09:47:12 an operator accepts 42.5 m/min; the flow stores 42.5 in the context entry keyed to that NodeId and writes it to the server.

First restart. The NodeSet loads, the flow reads the entry — 42.5, Double — checks the node's type, Double against Double, writes it, and the client reads it back. Arithmetic: read-back minus stored = 42.5 − 42.5 = 0, so the restore path passes.

Second restart. Accept 44.0 m/min and restart again. If the client sees 44.0, the difference is again 0 and both paths — restore and update — work. If it still sees the first value, the difference is 42.5 − 44.0 = −1.5 m/min: restore works, but the accepted-change branch never reached the store, so fix the update side.

The decision: a passing first restart proves restore; a passing second restart proves the store stays current. Run both before calling the server persistent.

Faults that come back #

Symptom Likely cause Fix
Variable missing after restart, context value present NodeSet not loaded, or load selected but restart skipped Load the file, restart, confirm the node exists before any write-back
Node present, first-cycle value returns Context updated only at deploy, not on accepted changes Store every accepted value at the moment it is accepted
Write-back fails after restart Target node absent, or stored type does not match the node type Verify NodeId and data type before writing
Duplicated variables accumulate at each start Flow recreates nodes the NodeSet already restores Let the NodeSet own structure and remove the flow's creation branch
Values vanish after a power cut, not a clean restart Newest context changes not yet written through Test with clean restarts and re-verify after any uncontrolled shutdown

The decision per case: structure missing — check the version, save and load a NodeSet, restart into it. Values missing — file-based context keyed to NodeIds, with the write-back held until the node is confirmed present. Both already return — verify data type and NodeId, then a second restart, and leave the configuration alone. In every case, restore only what the flow owns; device-owned tags wait for the device.

Questions asked

Is there a ready-made node that stores values without editing settings.js?

Yes. node-red-contrib-key-value-store is the contrib-node alternative suggested for the same job: it keeps key-value pairs on disk so they return after a restart. The built-in file-based context store stays the default recommendation because it needs no extra package, but where policy forbids editing settings.js on the gateway, this node does the persistence from inside the flow.

Which context objects do not survive the file-based store?

Map and Set objects do not survive the local filesystem context store: they come back as empty objects, a limitation reported in the Node-RED issue tracker. Store plain numbers, strings, booleans and JSON objects instead, and convert any Map or Set to an array or plain object before writing it to context; rebuild the original structure after the restore read.

Is there a server package that loads the NodeSet straight from its configuration?

node-red-contrib-opcua-server-refresh lists loading of nodeset XML files from inside the node configuration among its documented features, which removes the manual load step at each start. Treat it like any third-party package: prove it on a bench gateway and confirm the loaded nodes, NodeIds and data types against your commissioned list before it faces production clients.

Can a power cut lose context values even with the file store enabled?

Yes. File writes pass through the operating system's cache, so values accepted in the final moments before a power cut can be missing when Node-RED returns. After any uncontrolled shutdown, read the last accepted value back from context and compare it against the device or the operator log before trusting the restored state.

Keep reading

  • How-to

    MQTT to Modbus TCP with Node-RED on an edge gateway

    Yes: Node-RED with the node-red-contrib-modbus package can serve MQTT data to a SCADA client as Modbus TCP. Read the RTU device in Node-RED first, give the Modbus server node a port nothing else owns, and scale each MQTT payload to one integer per holding register before the first end-to-end test.

    Edge computing and IIoT

  • Explainer

    What a Node-RED simulator for OPC UA and MQTT data teaches

    A simulator stands in for the PLC, SCADA server or broker you cannot connect to yet, so you can build and prove the whole Node-RED acquisition path — input node, debug, one JSON decode, normalization, dashboard. The rule it teaches: decode each payload exactly once — set the MQTT input to a parsed JSON object and drop the JSON node, or keep the raw text and decode with one JSON node.

    Edge computing and IIoT

  • Troubleshooting

    Why a gateway shows a TCP connection but sends no payload

    The connection your TCP node shows is transport only. The gateway wraps its reporting upload in TLS, rejects a self-signed server certificate during the handshake, and closes the socket before a single datapoint is sent, so the log records the upload and then data lost. Give the gateway a certificate chain it trusts for the configured server name, or run a custom gateway application that talks to Node-RED directly.

    Edge computing and IIoT

Fix AVEVA Historian overnight data gaps after restart