Generate retain variables using global.set/get

Hi guys,
two variables that I enter in a form on the dashboard, should retain their values even after restarting node-red. It's not working in my project, so I will show you what I have done and hope you can give me a solution. Node-Red is running on a raspberry.

I have change the settings.js context storage property to:

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

I have write a function to store the values in a global array using global.set:

// after click on submit button the function get executed
// msg.payload = contain values written by user in form
ip_api=[ ]; // global array to store ip address and api-key provided by user
{
ip_api[0]=msg.payload["easyE4 IP-Adresse"];//storing the ip address in global array.
ip_api[1]=msg.payload["easyE4 API-SchlĂĽssel"];//storing the API-Key in global array.
}
global.set("ip",ip_api[0]); // method to set global varible/array
global.set("api",ip_api[1]); // method to set global varible/array
msg.payload = ip_api;
return msg;

Everything is working fine in my project, but after restart of the raspberry I have to enter values again.
What have I done wrong?

I hope, I can get a little support from the community. :smiley:
Ralf

The form fields will be empty after a restart, but the global variables should still exist, did you check the context data ? (you probably need to refresh it)

Screenshot 2019-12-15 at 15.15.53

Hi bakman2,
yes, I check this.

Before restart:


After restart:

Ralf

Did you click the refresh icon ?

Yes :slight_smile:

That is strange, you are not overwriting/clearing the variables anywhere ?

On the commandline could you check:

cd ~/.node-red/context/global
cat global.json

node-red-restart

cat global.json

I will check this.
And no, there is no overwriting, because this are the only variables I defined.

This directory will not be found :thinking:

contextStorage: {
default: {
module:"localfilesystem"
}, <-- this comma is redundant
},

Dont know if it breaks context storage option but wrong syntax anyway.

I have delete the comma, but this didn't solve my problem. :confused:

  1. What version of Node-RED?
  2. What version of Node.js
  3. please copy and paste a copy of your settings.js file (three back tics ` on the line before and after the code)
  4. how are you setting the valuse in the first place and/or checking their existence before doing something with them
  5. after making the correction @hotNipi suggested, did you stop and restart NR?

Hi guys,
I have found my mistake. I eddit the wrong settings.js.
I'm a linux beginner, and didn't nowing, that I have to look for a hide file.
So therefore I eddit the file found in the directory "/usr/lib/node_modules/node-red".
Now I have found the right file in "/home/pi/.node-red" and everythings working as expected.

Thanks for the patience!
Ralf

1 Like