How do I set flow variables to preserve their value over a restart?

I have variables which add up during the day, for example power usgae, solar production etc. At midnight, I reset them to 0. So all is fine until I restart Node Red, as these values (I use flow.set/flow.get) then get lost. I have set Node Red to save them to disk. (Everything is set to save to local disk in the setings.js). After a restart I noticed that the values were not what I expected them to be. I think I have found the problem. In my function node, as an example I have this:

var powertotal = flow.get('powertotal') || 0;

So after a restart, it would set it to 0 not using the saved to disk value file I believe. How can I best save values so that they are accessible after a restart (out of a function node)

Can you share how exactly you have configured Node-RED to save them to disk? Please share the whole contextStorage setting if possible.

Somehow I had the changes not saved, so it was still only saving to memory. Nevertheless, I dont think jus saving to disk will solve that issue. I am running Node Red in iobroker, and use the setting.js in this directory (I think thats the right one to change but not 100% sure)

C:\iobroker\node_modules\iobroker.node-red/userdata/settings.js

This is the context part (now changed to save to disk)

/** Context Storage
      * The following property can be used to enable context storage. The configuration
      * provided here will enable file-based context that flushes to disk every 30 seconds.
      * Refer to the documentation for further options: https://nodered.org/docs/api/context/
      */
     contextStorage: {
         default: {
             module:"localfilesystem"
        },
     },

     /** `global.keys()` returns a list of all properties set in global context.
      * This allows them to be displayed in the Context Sidebar within the editor.
      * In some circumstances it is not desirable to expose them to the editor. The
      * following property can be used to hide any property set in `functionGlobalContext`
      * from being list by `global.keys()`.
      * By default, the property is set to false to avoid accidental exposure of
      * their values. Setting this to true will cause the keys to be listed.
      */
     exportGlobalContextKeys: false,

     /** Configure how the runtime will handle external npm modules.
      * This covers:
      *  - whether the editor will allow new node modules to be installed
      *  - whether nodes, such as the Function node are allowed to have their
      * own dynamically configured dependencies.
      * The allow/denyList options can be used to limit what modules the runtime
      * will install/load. It can use '*' as a wildcard that matches anything.
      */

If you weren't saving to disk before, then there was nothing for Node-RED to restore to context.

As you've now updated the config, it should restore the values after a restart.

After saving, let it run for a little while, I have restarted Node Red. And the values were gone. So saving to disk is not enough I guess

The settings.js I used, is always overwritten on restart. That was the issue. I had to change the settings.js in
C:\iobroker\node_modules\iobroker.node-red/settings.js
I am going to restart after a few values have accreued, then see if its ok now

I can confirm it is saving the values, and I did not have to change anything in my function nodes. Great.

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