Trouble getting Persistent Storage to work

When attempting to store variables, they simply get lost upon reboot.

This is my settings.js:

module.exports = {
flowFile: 'flows.json',
flowFilePretty: true,
contextStorage: {
default: "memoryOnly",
memoryOnly: { module: 'memory' },
file: { module: 'localfilesystem' }
},

Startup Log:

26 Jul 10:13:23 - [info]

Welcome to Node-RED

===================

26 Jul 10:13:23 - [info] Node-RED version: v2.2.2

26 Jul 10:13:23 - [info] Node.js version: v14.18.2

26 Jul 10:13:23 - [info] Linux 4.15.0 x64 LE

26 Jul 10:13:24 - [info] Loading palette nodes

26 Jul 10:13:27 - [info] Dashboard version 3.1.6 started at /ui

26 Jul 10:13:28 - [warn] rpi-gpio : Raspberry Pi specific node set inactive

26 Jul 10:13:28 - [info] Settings file : /data/settings.js

26 Jul 10:13:28 - [info] Context store : 'memoryOnly' [module=memory]

26 Jul 10:13:28 - [info] Context store : 'file' [module=localfilesystem]

26 Jul 10:13:28 - [info] User directory : /data

26 Jul 10:13:28 - [info] Projects directory: /data/projects

26 Jul 10:13:28 - [info] Server now running at https://127.0.0.1:1880/

26 Jul 10:13:28 - [info] Active project : Test2

26 Jul 10:13:28 - [info] Flows file : /data/projects/tests2/flows.json

26 Jul 10:13:28 - [info] Starting flows

26 Jul 10:13:28 - [info] Started flows

26 Jul 10:13:28 - [info] [mqtt-broker:randomID] Connected to broker: mqtt://my.secret.IP:1883

To test, I set the variable, restart nodered, then try to retrieve the value.

Set the value:

flow.set("ID", 123, "file");
msg.payload = flow.get("ID", "file");
return msg;

Retrieve the value:

var count = flow.get("ID", "file")||0;
msg.payload = count;
return msg;

Expected result:
After a restart, the msg.payload value should be "123".

Actual result:
After a restart, the msg.payload value is "0".

In addition, the change node doesn't give an option to select contextStorage, as if there's only one option available.

Does anyone have an idea, why this doesn't work.

Your code is working for me. What version of NR are you running?
When you run the 'add' flow, what do you see if you go to the 'Context data' option in the sidebar and click the flow refresh?
context
And what do you see after a stop/start of NR?

I would suggest you change your settings file to use:

			default    : { module: "localfilesystem" },
 			memoryOnly     : { module: "memory" }

Then it will always go to the file unless you specify 'memoryOnly'

I'm using Nodered v2.2.2

This is how the 'Context data' looks after setting the variable:
Screenshot 2022-07-26 at 13.19.41

And this is how the 'Context data' looks after restarting the flow and after retrieving the variable:
Screenshot 2022-07-26 at 13.20.31

Edit:
I installed NR following the NR docker install tutorial. Might that pose any problems?

Have you checked following ?

  • it appears you are using default settings which uses dir=~/.node-red and base=context -> can you see if node-red produces files in that directory -> ~/.node-red/context/ ?

  • is the directory writeable for node-red daemon user?

  • if you are using docker - is the direcotry on a persistent filesystem (bind-mount or volume) ?

It might, you might want to ask on a docker forum. I've never used docker.

Have you tried what @iRob suggested?

I set all my Node-RED setting.js up like this for context:

contextStorage: {
                default: "file",
                memoryOnly: {
                        module: 'memory' },
                file: {
                        module: 'localfilesystem' },
     },

basically like zenofmud suggested.

This should default to saving context as a file rather than in memory, unless you specify that you want memoryOnly when setting the variable.

Scratch that, I see you are already specifying file. Perhaps this is issue with Docker not being able to create the context file due to permissions or something like that?

Ok, i changed the default directory and now the variables persist through a restart. The default dir must have been inaccessible.

However, there's a new issue: When changing the project and then reopening the old project, the variables get lost, but as long as I only use one project, I can restart without loosing data.

I believe context storage is separate for each project when using file, because they are stored in a separate file in each projects folder.

In Docker all path/file references are INSIDE the container, not to the host's filesystem. To enable using the host's filesystem you need to add "-v /path-to-desired-host-directory:/data" to your docker command that starts NR. You may also need to add privileges with "--group-add whatever-group" which can be repeated as many times as needed. More information on docker

1 Like

Just to clarify, these are the steps:

  • use flow.set("ID", 123, file) in project1
  • open project2
  • do nothing
  • open project1
  • run flow.get("ID, "file")||0
    and it returns 0.

I'm not expecting the variable to be available in another project, just that it's still there, when I return to the project.

Sorry, that was a spelling mistake on my part. I did in fact use flow.set("ID, 123, file) and have edited the previous post for clarity.

No this not true. All persistant data is stored in the .node-red/context folder. Within that folder is a folder called global where global data resides. Flow data is stored seperately in folders where the folder name is the ID of the flow.

So persistant flow data is not shared between projects but global data is.

1 Like

I see, thank you for that clarification.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.