# How to preserve the data of a chart node across a restart of Node Red?

**URL:** <https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879>\
**Category:** General\
**Created:** [1 August 2022 12:01 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879 "2022-08-01T12:01:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harpau](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harpau/32/8425_2.png) [@Harpau](https://discourse.nodered.org/u/Harpau)\
**Post date:** [1 August 2022 12:01 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/1 "2022-08-01T12:01:54Z")

</div>

I visualize in a Chart Node the data of a climate sensor over a longer period of time.  
Unfortunately, every time Node Red is restarted, all data recorded up to that point is lost.  
Has anyone already solved this problem and can give me a corresponding example flow?  
Many thanks in advance!  
Harald

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 August 2022 12:08 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/2 "2022-08-01T12:08:36Z")

</div>

The obvious answer is to use a database.

I do this with Mariadb and the basic Node-red dashboard charts.

Many others use influxdb and grafana to draw the graphs.

If you search the forum you should find many examples.

---

<div class="post-metadata">

**Author:** ![M-a-r-t-i-n](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m-a-r-t-i-n/32/52391_2.png) [@M-a-r-t-i-n](https://discourse.nodered.org/u/M-a-r-t-i-n)\
**Post date:** [1 August 2022 12:13 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/3 "2022-08-01T12:13:09Z")

</div>

A simple way to persist the graph info is to use the node-red-contrib-persist nodes.

The output of the chart node is connected to the input of the ‘persist in’ node.

The ‘persist out’ node connects to the input of the chart node.  
(Along with whatever feeds data to your chart)

Before shutdown, the data will be saved to a file.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 August 2022 12:15 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/4 "2022-08-01T12:15:19Z")

</div>

or you could use the cache node from uibuilder along with a persistent context store.

---

<div class="post-metadata">

**Author:** ![Harpau](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harpau/32/8425_2.png) [@Harpau](https://discourse.nodered.org/u/Harpau)\
**Post date:** [1 August 2022 13:33 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/5 "2022-08-01T13:33:17Z")

</div>

> [@M-a-r-t-i-n](#):
>
> A simple way to persist the graph info is to use the node-red-contrib-persist nodes.
> 
> The output of the chart node is connected to the input of the ‘persist in’ node.
> 
> The ‘persist out’ node connects to the input of the chart node.  
> (Along with whatever feeds data to your chart)
> 
> Before shutdown, the data will be saved to a file.

Many thanks, this is what I was looking for.

Unfortunately, however, I have now destroyed my Node Red installation: Since the file was not saved in the desired folder, I deleted the configuration of the file and performed a deploy. Now Node Red does not start anymore, because the persistence nodes produce an uncaught exception:

```auto
1 Aug 15:25:36 - [red] Uncaught Exception:
1 Aug 15:25:36 - [error] TypeError: Cannot read properties of null (reading 'getMessage')
    at EventEmitter.PersistOutNode.node.restore (/root/.node-red/node_modules/node-red-contrib-persist/persist.js:159:40)
    at EventEmitter.emit (node:events:539:35)
    at Object.start [as startFlows] (/usr/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/flows/index.js:407:12)
nodered.service: Main process exited, code=exited, status=1/FAILURE
nodered.service: Failed with result 'exit-code'.

```

Do you have any idea how I can get my installation working again?

---

<div class="post-metadata">

**Author:** ![Harpau](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harpau/32/8425_2.png) [@Harpau](https://discourse.nodered.org/u/Harpau)\
**Post date:** [1 August 2022 13:51 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/6 "2022-08-01T13:51:00Z")

</div>

> [@Harpau](#):
>
> Do you have any idea how I can get my installation working again?

I have found it: node-red --safe

---

<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:** [1 August 2022 13:53 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/7 "2022-08-01T13:53:41Z")

</div>

This flow shows you to save and restore a chart.  
[https://flows.nodered.org/flow/92920b4fd450bf92a3035e061947daa0](https://flows.nodered.org/flow/92920b4fd450bf92a3035e061947daa0)

---

<div class="post-metadata">

**Author:** ![Harpau](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harpau/32/8425_2.png) [@Harpau](https://discourse.nodered.org/u/Harpau)\
**Post date:** [1 August 2022 14:48 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/8 "2022-08-01T14:48:32Z")

</div>

Many thanks. I think I will build on this example as the persistence nodes also throw some other warnings (use of deprecated function).

---

<div class="post-metadata">

**Author:** ![Harpau](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harpau/32/8425_2.png) [@Harpau](https://discourse.nodered.org/u/Harpau)\
**Post date:** [3 August 2022 13:35 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/9 "2022-08-03T13:35:38Z")

</div>

I have now designed a subflow that solves the problem using SQLite:

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

Example flow:

```auto
[{"id":"0cb6bf1c544ebd85","type":"subflow","name":"Chart Persistence","info":"","category":"","in":[{"x":60,"y":80,"wires":[{"id":"ef3ff83b.aa5008"}]}],"out":[{"x":840,"y":280,"wires":[{"id":"4f8411277831e52e","port":0}]}],"env":[{"name":"Node","type":"str","value":""}],"meta":{},"color":"#DDAA99","status":{"x":840,"y":80,"wires":[{"id":"fa4020ebfee2ed9a","port":0}]}},{"id":"ef3ff83b.aa5008","type":"json","z":"0cb6bf1c544ebd85","name":"","x":170,"y":80,"wires":[["26de10021a1548a7"]]},{"id":"7a999f0bb3f4d802","type":"sqlite","z":"0cb6bf1c544ebd85","mydb":"d98f5a8b0d088b01","sqlquery":"prepared","sql":"INSERT OR REPLACE INTO Node_Data VALUES($Node,$Data)","name":"Write","x":510,"y":80,"wires":[["fa4020ebfee2ed9a"]]},{"id":"26de10021a1548a7","type":"function","z":"0cb6bf1c544ebd85","name":"Set msg.params","func":"if (flow.get(\"initialized\") === true) {\n msg.params = { \n \"$Node\": env.get(\"Node\"),\n \"$Data\": msg.payload \n }\n return msg;\n}\nelse\n return null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":80,"wires":[["7a999f0bb3f4d802"]]},{"id":"4f8411277831e52e","type":"json","z":"0cb6bf1c544ebd85","name":"","x":730,"y":280,"wires":[[]]},{"id":"6c4202929fcc9b69","type":"sqlite","z":"0cb6bf1c544ebd85","mydb":"d98f5a8b0d088b01","sqlquery":"prepared","sql":"SELECT DATA FROM Node_Data WHERE(NODE=$Node)","name":"Read","x":510,"y":220,"wires":[["cf17623e6f0e211b"]]},{"id":"cf17623e6f0e211b","type":"change","z":"0cb6bf1c544ebd85","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[0].DATA","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":220,"wires":[["4f8411277831e52e"]]},{"id":"037ab12c9bca1d22","type":"function","z":"0cb6bf1c544ebd85","name":"Set msg.params","func":"msg.params = { \n \"$Node\": env.get(\"Node\")\n}\nflow.set(\"initialized\",true)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":220,"wires":[["6c4202929fcc9b69"]]},{"id":"fd11c6e044c9d6ce","type":"sqlite","z":"0cb6bf1c544ebd85","mydb":"d98f5a8b0d088b01","sqlquery":"fixed","sql":"CREATE TABLE IF NOT EXISTS Node_Data(NODE TEXT PRIMARY KEY NOT NULL,DATA TEXT NOT NULL)","name":"Create Table","x":330,"y":160,"wires":[["037ab12c9bca1d22"]]},{"id":"fa4020ebfee2ed9a","type":"function","z":"0cb6bf1c544ebd85","name":"Status \"dot\"","func":"msg.payload = {fill:\"green\",shape:\"dot\",text:env.get(\"Node\")}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":80,"wires":[[]]},{"id":"bd3b56346aacbcf1","type":"inject","z":"0cb6bf1c544ebd85","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.1","topic":"","payload":"true","payloadType":"bool","x":170,"y":160,"wires":[["fd11c6e044c9d6ce"]]},{"id":"d98f5a8b0d088b01","type":"sqlitedb","db":"/home/node-red/persistence.sql","mode":"RWC"},{"id":"0b6404af633acaac","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"cd9313eba6a1ec1a","type":"subflow:0cb6bf1c544ebd85","z":"0b6404af633acaac","name":"Chart Persistence Demo","env":[{"name":"Node","value":"Chart Persistence Demo","type":"str"}],"x":630,"y":120,"wires":[["7e36c3ccf9ce0072"]]},{"id":"b045f9e321fcc5b3","type":"inject","z":"0b6404af633acaac","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"10","crontab":"","once":true,"onceDelay":0.1,"topic":"random","payload":"true","payloadType":"bool","x":110,"y":120,"wires":[["a50c50c540272816"]]},{"id":"7e36c3ccf9ce0072","type":"ui_chart","z":"0b6404af633acaac","name":"","group":"884daaba08bae0a4","order":0,"width":0,"height":0,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":430,"y":120,"wires":[["cd9313eba6a1ec1a"]]},{"id":"a50c50c540272816","type":"function","z":"0b6404af633acaac","name":"Random","func":"msg.payload = Math.random()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":120,"wires":[["7e36c3ccf9ce0072"]]},{"id":"884daaba08bae0a4","type":"ui_group","name":"Chart Persistence Demo","tab":"5dd649e34ebbd68e","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"5dd649e34ebbd68e","type":"ui_tab","name":"Persistence Demo","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<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:** [17 August 2022 13:35 UTC](https://discourse.nodered.org/t/how-to-preserve-the-data-of-a-chart-node-across-a-restart-of-node-red/65879/10 "2022-08-17T13:35:43Z")

</div>

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