Persistent states - node red?

Can anyone advise me how to store persistent states via node red? Should I be using a database or is there an easier way?

Thank you

no database needed. Node-red can store flow and global variables on some folder (in json format – but not meant for you to use the file directly). You can then use nodes and/or function nodes to access them.

Edit /config/node-red/settings.js and add the following:

contextStorage: {
	store: { module: "localfilesystem"},
    default: { module: "memory" }
},

Restart node-red.

Now if you want to store a variable to file using the change node, change context variable to either flow or global and you’ll see the two types of contextStorage you have defined as dropdown options:

6458f681da95858a5b6c060292f706c05f7ba6f1

Hope this helps.

4 Likes

That’s is perfect - thank you

Do you know if it is possible to store a variable from within the function node and if so could you explain how please?

Thank you again for you help.

Yes. Something like:

	flow.set("status", "on");              // the value is stored in memory
	flow.set("status", "on", "default");   // the value is also stored in memory -- same as above
	flow.set("status", "on", "store");     // the value is stored in a file

You can return the variable from the file in a function using this:

    return flow.get("status","store") || "undefined";

Btw, there is a new “Context Data” viewer that shows all the values of all your variables (in memory and file). Not sure which version it came out:

2018-11-01_09-44-13

Hope this helps.

2 Likes

Sorry I forgot to say thank you. This worked perfectly. Thank you.

You’re welcome.

Hi @rsuplido, apologies for resurrecting such an old thread, but, could you post a bit about where in the settings.js file your insert should go? I stuck it in at the bottom, which produced an error that caused node-red to be unable to start at all until I removed it.

Accepting that I am not a clever person at all, should adding this to the bottom have worked, or should it be inserted in a particular section of the settings file?

Pity my simplicity. Thank you.

the node-red settings file needs to be valid JSON.

copy/paste your config file here: https://jslint.com/ and see if it throws any errors

Check the Version of Node-red - i believe persistent context only come out in 19.1 and above

This is what the END of my settings file looks like

contextStorage: {
default: {
module: “localfilesystem”
},
memoryOnly: {
module: “memory”
}
}

1 Like

Don’t add it at the end of the file. Try adding it maybe before this line:

logging: {

1 Like

Mine is definitely at the end of the file on my 3 NR instances and after the logging section - no issues

Craig

I put mine as well at the end of the file but did online json-syntax check before saving.

All fine

1 Like

Spinning up an older thread again since I have the same question but not able to find an answer.

I have the node-red addon in hass, where can I enable context ? How do I access the nod red settings file?

Edit: found it. it is very simple… in the config folder :slight_smile:

1 Like