# Save&Restore Chart Data

**URL:** <https://discourse.nodered.org/t/save-restore-chart-data/39808>\
**Category:** Dashboard\
**Created:** [26 January 2021 18:02 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808 "2021-01-26T18:02:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![pDR8iLQ4go](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pdr8ilq4go/32/35983_2.png) [@pDR8iLQ4go](https://discourse.nodered.org/u/pDR8iLQ4go)\
**Post date:** [26 January 2021 18:02 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/1 "2021-01-26T18:02:34Z")

</div>

I had the problem that my chart data was deleted when I deployed my workspace. I was able to solve this topic using this subflow:

> **[Persistent chart data in node-red](https://www.hexamail.com/guides/node-red/persistcharts.htm)**
>
> Persistent chart data in node-red

But I'm wondering if my file system will be damaged if there is a periodically (every minute = 1440 times a day) access to the file system (USB stick or SD card). Therefore I tried to add a feature to save the chart data manually, before I'll deploy my workspace. And I found this example:  
[https://flows.nodered.org/flow/92920b4fd450bf92a3035e061947daa0](https://flows.nodered.org/flow/92920b4fd450bf92a3035e061947daa0)

Now I want to mix both features:

1. Store chart data manually (e. g. using the inject node)
2. Load data automatically

As I am a Node-RED rookie, I need some help to implement that!

---

<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:** [27 January 2021 11:20 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/2 "2021-01-27T11:20:25Z")

</div>

Since no-one else has answered I will jump in here. If you use the subflow you mention then you can easily limit how often it writes by putting a Delay node configured something like this to limit often it saves the chart  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da6e1d1ea18fbd50535a6d51d4ecfa52c57c08ab.png)

Alternatively, if you use a Trigger node configured as below then it will do the same thing but has the advantage that you could send it a reset message which would reset it so the next time the chart sends it current state that would get through immediately, giving you your 'save on demand' feature, or at least a 'save on next update'

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/824ec7ec51867b2d93fd4e7edf5133ef2a0718dd.png)

I haven't looked at the subflow but if it uses a file for storage then that is no longer the easiest way of doing this. The best way now would be to use persistent context store. See the docs on [Working with Context](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![Folgore](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@Folgore](https://discourse.nodered.org/u/Folgore)\
**Post date:** [27 January 2021 12:26 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/3 "2021-01-27T12:26:41Z")

</div>

Hi, I have used the same solution with a small upgrade, in the chart I have set a period of 24 hour. I create a txt file for every day for the chart.

---

<div class="post-metadata">

**Author:** ![pDR8iLQ4go](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pdr8ilq4go/32/35983_2.png) [@pDR8iLQ4go](https://discourse.nodered.org/u/pDR8iLQ4go)\
**Post date:** [28 January 2021 19:35 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/4 "2021-01-28T19:35:25Z")

</div>

This seems to be helpful as well:

> **[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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [28 January 2021 20:46 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/5 "2021-01-28T20:46:22Z")

</div>

Why not use node-RED's inbuilt file based context to save/restore your chart, it's easy to setup and uses 2 standard nodes.  
See [Working with context : Node-RED](https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system) to set up file context, then add a change node to the output of your chart to save the chart data to your file context.

![context](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/247e3594933203a8ac498e9d3fcf33a4a4da347e.jpeg)

Then add an inject node to inject the file context into the chart node

![newone](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/f/1f6549f9bf7ed2bbbc2f712acd23b00d3773a098.jpeg)

Set the inject node just to inject once

![inject](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/202342a5e45d9d559364227d558d74c95b310e78.jpeg)

If you are concerned about continually writing data to the file system, you can then edit your `contextStorage` settings, and add the `flushInterval` value, to only write data to your file system every, for example 1200 seconds (the example below) OR if you stop node-RED it will also write it.  
So although your chart updates every minute, the chart data will be initially saved to memory, but then node-RED will flush it to the filesystem as per the `flushInterval`.

```auto
    contextStorage: {
      default: "memoryOnly",
    memoryOnly: { module: 'memory' },
          file: { module: 'localfilesystem',
          config: {
                  flushInterval: '1200'
                  },
          },
    },

```

---

<div class="post-metadata">

**Author:** ![pDR8iLQ4go](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pdr8ilq4go/32/35983_2.png) [@pDR8iLQ4go](https://discourse.nodered.org/u/pDR8iLQ4go)\
**Post date:** [28 January 2021 21:02 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/6 "2021-01-28T21:02:50Z")

</div>

Sounds as this is exactly what I‘m looking for. I‘ll give it a try tomorrow...

---

<div class="post-metadata">

**Author:** ![pDR8iLQ4go](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pdr8ilq4go/32/35983_2.png) [@pDR8iLQ4go](https://discourse.nodered.org/u/pDR8iLQ4go)\
**Post date:** [29 January 2021 15:40 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/7 "2021-01-29T15:40:57Z")

</div>

I think I got it. Did it that way:

First: Changed file "settings.js" as suggested by [Paul-Reed](https://discourse.nodered.org/u/Paul-Reed).

My node...

 ![B1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cd41ed98100701dc80f4c66b33304ed58ab7b894.png)

Setup of my _inject_ node, with restore from _memoryOnly_ and  
setup of my _change_ node, with storage to _memoryOnly_:

 ![Setup](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d93bb8e2aef18baa4f1d7e169eb6b8878f1aab30.png)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [29 January 2021 16:46 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/8 "2021-01-29T16:46:54Z")

</div>

> [@pDR8iLQ4go](#):
>
> Setup of my _inject_ node, with restore from _memoryOnly_ and  
> setup of my _change_ node, with storage to _memoryOnly_ :

You should be saving and restoring if from `file` not `memory`.  
If you use `memory` and reboot your pi, all data will be lost.

---

<div class="post-metadata">

**Author:** ![pDR8iLQ4go](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pdr8ilq4go/32/35983_2.png) [@pDR8iLQ4go](https://discourse.nodered.org/u/pDR8iLQ4go)\
**Post date:** [29 January 2021 16:52 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/9 "2021-01-29T16:52:51Z")

</div>

The chart data files should be stored in...

> /home/pi/.node-red/context

Right?

Will the flush (copy memory content to file) be triggered if I'll deploy my workspace (Full Deploy)?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [29 January 2021 17:05 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/10 "2021-01-29T17:05:56Z")

</div>

> [@pDR8iLQ4go](#):
>
> The chart data files should be stored in...

That's what the docs say...

> [@Paul-Reed](#):
>
> See [Working with context : Node-RED](https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system)

> [@pDR8iLQ4go](#):
>
> Will the flush (copy memory content to file) be triggered if I'll deploy my workspace (Full Deploy)?

There would be no need to 'flush the data to file' for a full deployment, as the chart will retain it's data anyway when a full deployment is made.  
Are you saying that your chart doesn't?

I believe that a flush occurs when either node-RED is stopped, when the OS is shutdown, or the time listed in `flushInterval` is reached _(and then only if the data has actually changed)_.

---

<div class="post-metadata">

**Author:** ![pDR8iLQ4go](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pdr8ilq4go/32/35983_2.png) [@pDR8iLQ4go](https://discourse.nodered.org/u/pDR8iLQ4go)\
**Post date:** [29 January 2021 17:31 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/11 "2021-01-29T17:31:30Z")

</div>

I've changed it as suggested by [Paul-Reed](https://discourse.nodered.org/u/Paul-Reed):  
 ![file](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b3767fe84eaafe696ef6629b24a41b46f5ff42b.png)

Now the chart data is stored & restored as expected.  
Thanks to everybody for helping me!

---

<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:** [28 February 2021 17:31 UTC](https://discourse.nodered.org/t/save-restore-chart-data/39808/12 "2021-02-28T17:31:59Z")

</div>

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