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

**URL:** <https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923>\
**Category:** General\
**Created:** [9 December 2022 14:40 UTC](https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923 "2022-12-09T14:40:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [9 December 2022 14:40 UTC](https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923/1 "2022-12-09T14:40:59Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 December 2022 14:47 UTC](https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923/2 "2022-12-09T14:47:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [9 December 2022 14:59 UTC](https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923/3 "2022-12-09T14:59:49Z")

</div>

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)

```auto
/** 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.
      */

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 December 2022 15:28 UTC](https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923/4 "2022-12-09T15:28:39Z")

</div>

> [@Mikee1234K](#):
>
> so it was still only saving to memory. Nevertheless, I dont think jus saving to disk will solve that issue.

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.

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [9 December 2022 15:38 UTC](https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923/5 "2022-12-09T15:38:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [9 December 2022 15:54 UTC](https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923/6 "2022-12-09T15:54:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [9 December 2022 16:35 UTC](https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923/7 "2022-12-09T16:35:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 December 2022 16:36 UTC](https://discourse.nodered.org/t/how-do-i-set-flow-variables-to-preserve-their-value-over-a-restart/71923/8 "2022-12-23T16:36:14Z")

</div>

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