Setting up Context Storage in the Node-RED plugin within Signal-K

Setting up Context Storage in the Node-RED plugin within Signal-K

The Node-RED plugin for Signal-K allows you to run automation flows within the navigation system, but by default, context storage is only kept in memory. If we want the data to persist over time, we need to configure contextStorage to use the file system.

1 Location of the configuration file

Unlike a standard Node-RED installation, the Signal-K plugin does not read settings.js, but its configuration is located in the following file:

/home/user/.signalk/plugin-config-data/signalk-node-red.json

(Replace user with your Linux username)

2 Configuring contextStorage in the JSON File

To enable disk storage, open the signalk-node-red.json file and add the following configuration in the settings section:

{
  "configuration": {
    "logging": "trace",
    "settings": [
      {
        "name": "contextStorage",
        "value": "{\"default\":{\"module\":\"localfilesystem\",\"config\":{\"dir\":\"/home/user/.signalk/red/context/\", \"flushInterval\":180000}}}"
      }
    ]
  },
  "enabled": true
}

How to properly write the settings array

In the Signal-K configuration file, the settings section is an array of objects, where each object defines a specific setting for Node-RED. Each object must have two keys:

  1. name: The name of the configuration we want to modify (in this case, contextStorage).
  2. value: The value of the configuration, which should be written as a string. If the value is a JSON object, the quotes (") must be escaped so that it is interpreted correctly within the string.

Example of a valid structure:

"settings": [
  {
    "name": "contextStorage",
    "value": "{\"default\":{\"module\":\"localfilesystem\",\"config\":{\"dir\":\"/path/to/storage/\", \"flushInterval\":60000}}}"
  },
  {
    "name": "anotherConfig",
    "value": "true"
  }
]

Important note about using flushInterval

While flushInterval can be useful for controlling the frequency of data written to disk, it has been observed that adding this configuration (e.g., with an interval of 3 minutes or 180000 ms) can lead to erratic behavior in the persistence of global or flow variables in Node-RED. In some cases, this may prevent data from being saved correctly or updated properly after a restart.

To avoid these issues, it is recommended to remove or not use the flushInterval parameter, as shown in the following example:

"settings": [
  {
    "name": "contextStorage",
    "value": "{\"default\":{\"module\":\"localfilesystem\",\"config\":{\"dir\":\"/home/user/.signalk/red/context/\"}}}"
  }
]

This way, the data will be stored immediately without waiting for the configured interval.

3 Restart Signal-K to apply changes

Once the file has been edited and saved, restart Signal-K to apply the configuration:

sudo systemctl restart signalk

Then, check the logs to confirm there are no errors:

journalctl -u signalk -n 50 --no-pager

If everything is correct, Node-RED will begin storing the context on disk in the specified folder.

Verify that it works correctly

To ensure Node-RED is using disk storage:

  1. Run a flow with context variables (global.set("test", 123)).
  2. Check the folder /home/user/.signalk/red/context/ and verify that files are being created.
  3. Restart Signal-K and check that the variable still exists using global.get("test").

Additional Notes

  • If you encounter the error Unknown context store 'file' specified. Using default store., check that the JSON is properly formatted and that value is a valid string with escaped quotes (").
  • In-memory storage remains active by default, but persistent data will be stored in the specified folder.

With this configuration, Node-RED's context data within Signal-K will now be persistent, improving system reliability. 🚀

Previous Post Next Post