Restore Chart data after power outage

hi,
I'm trying to save and read chart data into a context var.
I'v edited the setting.js file to save the context variables into a file.
In this example flow I inject random numbers to a chart and save them to a Global variable.
my goal is to load all the data before the power outage and continue collecting new data.

below is my global.json file:

{
    "test": [
        {
            "series": [
                ""
            ],
            "data": [
                [
                    {
                        "x": 1593425610978,
                        "y": 2
                    },
                    {
                        "x": 1593425612255,
                        "y": 6
                    }
                ]
            ],
            "labels": [
                ""
            ]
        }
    ]
}

when I reboot my RPi3 and manually try to restore the data, the global variable is deleted and all I see in the global.json file is: {}.
how can I restore the data from my global variable?
When I tried to restore the data Without rebooting the Pi the global.json file is not deleted.

[{"id":"b836f2eb.f1541","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"7cbee4b7.7a8bcc","type":"function","z":"b836f2eb.f1541","name":"","func":"\nmsg.payload=global.get(\"test\")\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":260,"wires":[["2c4f22d.a1ba3de"]]},{"id":"a16bf2ff.1e57e","type":"random","z":"b836f2eb.f1541","name":"","low":"1","high":"10","inte":"true","property":"payload","x":320,"y":80,"wires":[["2c4f22d.a1ba3de"]]},{"id":"2c4f22d.a1ba3de","type":"ui_chart","z":"b836f2eb.f1541","name":"","group":"57c05f8a.c2026","order":1,"width":22,"height":5,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"Loading Please Wait....","dot":false,"ymin":"","ymax":"","removeOlder":"10","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#308230","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":510,"y":180,"wires":[["1eab9642.54b4ca"]]},{"id":"1eab9642.54b4ca","type":"function","z":"b836f2eb.f1541","name":"","func":"msg.rr=\"msg.payload= \"+msg.payload\nglobal.set(\"test\",msg.payload)\nreturn msg;","outputs":1,"noerr":0,"x":630,"y":120,"wires":[["a64ff192.a5c38"]]},{"id":"67610454.4baf3c","type":"inject","z":"b836f2eb.f1541","name":"run once","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":260,"wires":[["7cbee4b7.7a8bcc"]]},{"id":"45257baf.79c324","type":"inject","z":"b836f2eb.f1541","name":"run","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":160,"wires":[["a16bf2ff.1e57e"]]},{"id":"a64ff192.a5c38","type":"debug","z":"b836f2eb.f1541","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":810,"y":180,"wires":[]},{"id":"57c05f8a.c2026","type":"ui_group","z":"","name":"Default","tab":"b800bcf9.1a431","order":1,"disp":true,"width":22,"collapse":false},{"id":"b800bcf9.1a431","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Show us what you have done in settings.js

// 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"
},
},

Also put a debug node on the output of the function node picking up the data and see what it shows.
Does the chart restore if you restart node-red without rebooting?

yes, the chart is restored without rebooting.
this is the output from the function node:
msg.payload : array[1]

array[1]

0: object

series: array[1]

0: ""

data: array[1]

0: array[5]

0: object

1: object

2: object

3: object

4: object

Is that what the function node shows after boot? If so then it does not appear to be empty.

this was the debug Without rebooting as you requested.
After reboot I get a "msg.payload undefined" reply.

OK, that isn't what I meant, but I see my message was ambiguous.

What time range have you specified in the chart?

Try building a chart then remove the wire out of the chart and deploy. The chart should re-draw. Restart node red and make sure it still redraws.

Then stop node-red from starting on boot, by running
sudo systemctl disable nodered
then reboot and see if the context file is still there. If it is then restart node red and see what happens.

dear Colin,
Let me clarify:
All injected values are stored in the Global.json file in a variable.
I've checked the Global.json file after a reboot and the variable is there and contains data.
Whenever the Chart is empty (via reboot or by msg.payload= [])
and when I inject the saved data from the Global.json file variable
the variable is deleted, and in the debug window I receive the massage:
msg.payload=undefined

After checking the data is there, but before injecting it, remove the wire coming out of the chart and deploy. That will prevent the chart output from affecting the stored data. Then try injecting it.

when I removed the wire coming out of the chart, and deploing,
the data is still stored in the Global.json and appears in the chart.

Good, put a debug node on the chart output and see what comes out, there must be a message that empties the data.
Are you sending anything to the chart other than the from the inject? If you do then that will immediately send out a message from the chart and update the stored data. On your example flow there is another inject but it is not configured to auto inject so that should not be a problem.

Press the 'Run' inject three times, then go look at the graph
Next, in the dashboard sideboard
Screen Shot 2020-06-30 at 6.26.30 AM

  1. click the expand button to open the dropdown
  2. click on the 'Contest Data' option
  3. click on the refresh button
  4. click on all the triangle to expand the object and ist dependents

What do you see and what does it tell you?

global

Please do this - in a terminal go to yout NR folder - on a pi it will be $home/.node-red and look at the 'global.json' file, (cat global.json)

now start node-red and wait a 30 seconds

in another terminal window, look at the global.json again - is it empty?
I seem to remember rnning into this aat one point and am trying to find the issue...

found a solution:
I've added a switch node after the Chart node and my function node:
that states if msg.payload is undefined don't save it.


thank you very much for your help.

Ahhh the problem is that when a deploy happens or you start NR, the chart node is refreshing and sending an output message. Since the message has no msg.payload the global is over written.

This opens up a new question of why the chart node runs on a deploy but I'll ask that in a new thread.

I don't think that is correct is it? The output from the chart contains data for the whole chart and the saved data needs to be updated after every sample is added.

Oops! @Colin I do believe you are correct. I've stricken that advice from my previous post :flushed:

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