# Persistent global variables

**URL:** https://discourse.nodered.org/t/persistent-global-variables/41559
**Category:** General
**Created:** [24 February 2021 14:14 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559 "2021-02-24T14:14:38Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)
#### Post date: [24 February 2021 14:14 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559/1 "2021-02-24T14:14:39Z")

</div>

Hi,

I have a certain amount of global variable saved in the localfilesystem. I have seen that the variables are saved in `.node-red/context/global/global.json`, but there is something weird: not all the variables are in that file, and their state is not synchronized in real time. In other words, if a variable passes from state "true" to state "false", in that file it stays "true" for a while (about a minute), before changing state.

Is there some doc where I can study how this mechanism works?

Thanks

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [24 February 2021 14:21 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559/2 "2021-02-24T14:21:07Z")

</div>

lots of info on the forum (search persistent context )

e.g...

> [@Questions about persistent context](https://discourse.nodered.org/t/questions-about-persistent-context/5547/2):
>
> To begin with, as a node author, you just use Context. You don't get to pick if its persistent or not as that is up to the end user. No. It only flushes when there are changes to flush, but will ensures it doesn't flush any more frequently than the configured interval. Right. No. The context api only lets you get/set values - that is all. You don't know what instance of context plugin the user has configured. Yes. Assuming NR is shutdown cleanly, then the filesystem context plugin flush…

> [@A catch with persistent file stores](https://discourse.nodered.org/t/a-catch-with-persistent-file-stores/21062/2):
>
> You can always change the flush interval; contextStorage: { default: "file", memoryOnly: { module: 'memory' }, file: { module: 'localfilesystem', config: { flushInterval: '10' }, }, },

If you want more control, there are contrib nodes that can read/write to JSON in a very similar fasion to persistent storage e.g...

> **[node-red-contrib-persist](https://flows.nodered.org/node/node-red-contrib-persist)**
>
> A node-red node to persist data between Node Red deploys and restarts.

> **[node-red-contrib-key-value-store](https://flows.nodered.org/node/node-red-contrib-key-value-store)**
>
> Simple, persistent, JSON-based key-value store for Node-RED

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 February 2021 16:07 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559/3 "2021-02-24T16:07:36Z")

</div>

Context stored to a file is only done every 30 seconds - see [Working with context : Node-RED](https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system)

There might be a setting in [settings.us](http://settings.us) that can change the value. It was setup that way because of wear on SD cards

---

<div class="post-metadata">

### Author: ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)
#### Post date: [24 February 2021 16:10 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559/4 "2021-02-24T16:10:21Z")

</div>

@zenofmud thank you. On a Raspberry, do you suggest to extend it to 1 minute or so?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 February 2021 16:38 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559/5 "2021-02-24T16:38:28Z")

</div>

Note that it only writes if a value has changed. If you monitor /var/log/syslog you will see that writing the context out at that rate will not be an issue unless you have a large amount of data.

---

<div class="post-metadata">

### Author: ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)
#### Post date: [24 February 2021 17:23 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559/6 "2021-02-24T17:23:16Z")

</div>

Hi Colin,

indeed, I was looking at the syslog this morning, but didn’t find any entry related to saving context. Just starting/stopping flows while deploying...

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 February 2021 20:41 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559/7 "2021-02-24T20:41:32Z")

</div>

I didn't mean there was anything related to context, just that there is continual background chatter there, making saving context every 30 seconds irrelevant, unless you have a very lightly loaded pi, in which case you don't need to worry anyway.  
I just did `ls -al /var/log/` on my main pi, which is a pi3 (B?) to see how much there is and realise that pihole is logging about 25MB/day and has been doing that for years on the same card without issue. I think I might look how to turn that down a bit though, it is a bit excessive.

---

<div class="post-metadata">

### Author: ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)
#### Post date: [25 February 2021 07:12 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559/8 "2021-02-25T07:12:38Z")

</div>

Understand. You're right.  
Thank you!

---

<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: [11 March 2021 07:13 UTC](https://discourse.nodered.org/t/persistent-global-variables/41559/9 "2021-03-11T07:13:15Z")

</div>

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