Saving and repopulating graph data using JSON

I have a graph that saves JSON data to a text on a Raspberry Pi and that works as it should.

When I restart Node-Red I want to retrieve that data and repopulate the graph.

I'm able to fetch the data but I get an error when I try to put it in a graph.

"Unexpected token [ in JSON at position 4747"

Is this the best way to do this? Would it be better to store/retrieve the data in MYSQL? CSV file?
Here is my flow

[
    {
        "id": "13f270994f6c5e18",
        "type": "ui_button",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "group": "212b7f06.b4731",
        "order": 2,
        "width": "2",
        "height": "1",
        "passthru": false,
        "label": "Inject",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "",
        "topicType": "str",
        "x": 490,
        "y": 920,
        "wires": [
            [
                "fbdb6408e9583ccf"
            ]
        ]
    },
    {
        "id": "c7d0cd72e5c99dea",
        "type": "ui_button",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "group": "212b7f06.b4731",
        "order": 4,
        "width": "2",
        "height": "1",
        "passthru": false,
        "label": "restore",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "",
        "topicType": "str",
        "x": 480,
        "y": 1040,
        "wires": [
            [
                "39c67627238ee8ec"
            ]
        ]
    },
    {
        "id": "fbdb6408e9583ccf",
        "type": "random",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "low": "0",
        "high": "10",
        "inte": "true",
        "property": "payload",
        "x": 640,
        "y": 920,
        "wires": [
            [
                "db55d33591c4fad2"
            ]
        ]
    },
    {
        "id": "db55d33591c4fad2",
        "type": "ui_chart",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "group": "212b7f06.b4731",
        "order": 1,
        "width": 0,
        "height": 0,
        "label": "chart",
        "chartType": "line",
        "legend": "false",
        "xformat": "HH:mm:ss",
        "interpolate": "bezier",
        "nodata": "",
        "ymin": "",
        "ymax": "",
        "removeOlder": "15",
        "removeOlderPoints": "100",
        "removeOlderUnit": "60",
        "cutout": 0,
        "outputs": 2,
        "x": 790,
        "y": 920,
        "wires": [
            [],
            [
                "50191a22b99f5085"
            ]
        ]
    },
    {
        "id": "50191a22b99f5085",
        "type": "json",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 930,
        "y": 920,
        "wires": [
            [
                "81a4cad39186b94a"
            ]
        ]
    },
    {
        "id": "d118be10ab2db950",
        "type": "json",
        "z": "30dc9a8aad2d70a1",
        "name": "Convert to JSON",
        "property": "payload",
        "action": "",
        "pretty": true,
        "x": 970,
        "y": 1040,
        "wires": [
            [
                "2b34cec3bd204b99"
            ]
        ]
    },
    {
        "id": "81a4cad39186b94a",
        "type": "function",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "func": "if (msg.topic === \"save\") {\n    msg.payload = context.last;\n    return msg;\n}\nelse {\n    context.last = msg.payload;\n}\nreturn null;",
        "outputs": 1,
        "noerr": 0,
        "x": 660,
        "y": 980,
        "wires": [
            [
                "f299d075174ca8bb"
            ]
        ]
    },
    {
        "id": "f61f82547089abcd",
        "type": "ui_button",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "group": "212b7f06.b4731",
        "order": 3,
        "width": "2",
        "height": "1",
        "label": "save",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "save",
        "x": 480,
        "y": 980,
        "wires": [
            [
                "81a4cad39186b94a"
            ]
        ]
    },
    {
        "id": "f299d075174ca8bb",
        "type": "file",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "filename": "/home/pi/scripts/chartData/chart.log",
        "filenameType": "str",
        "appendNewline": true,
        "createDir": false,
        "overwriteFile": "false",
        "encoding": "setbymsg",
        "x": 910,
        "y": 980,
        "wires": [
            []
        ]
    },
    {
        "id": "39c67627238ee8ec",
        "type": "file in",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "filename": "/home/pi/scripts/chartData/chart.log",
        "filenameType": "str",
        "format": "utf8",
        "encoding": "none",
        "allProps": false,
        "x": 700,
        "y": 1040,
        "wires": [
            [
                "d118be10ab2db950",
                "8d6f0176364cb65b"
            ]
        ]
    },
    {
        "id": "2b34cec3bd204b99",
        "type": "debug",
        "z": "30dc9a8aad2d70a1",
        "name": "chart debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1170,
        "y": 1040,
        "wires": []
    },
    {
        "id": "8d6f0176364cb65b",
        "type": "ui_chart",
        "z": "30dc9a8aad2d70a1",
        "name": "",
        "group": "212b7f06.b4731",
        "order": 1,
        "width": 0,
        "height": 0,
        "label": "Restored Data",
        "chartType": "line",
        "legend": "false",
        "xformat": "HH:mm:ss",
        "interpolate": "bezier",
        "nodata": "",
        "dot": false,
        "ymin": "",
        "ymax": "",
        "removeOlder": "15",
        "removeOlderPoints": "100",
        "removeOlderUnit": "60",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#1f77b4",
            "#aec7e8",
            "#ff7f0e",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 1140,
        "y": 1140,
        "wires": [
            []
        ]
    },
    {
        "id": "212b7f06.b4731",
        "type": "ui_group",
        "name": "Gauge",
        "tab": "e7f7ff66.2de07",
        "order": 2,
        "disp": true,
        "width": "6"
    },
    {
        "id": "e7f7ff66.2de07",
        "type": "ui_tab",
        "name": "Restore",
        "icon": "dashboard"
    }
]```

When saving to file you need to overwrite the whole file. and no need to convert to json to save, only when restoring
e.g.

[{"id":"13f270994f6c5e18","type":"ui_button","z":"65617ffeb779f51c","name":"","group":"212b7f06.b4731","order":2,"width":"2","height":"1","passthru":false,"label":"Inject","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"","topicType":"str","x":70,"y":1640,"wires":[["2b56677f3a45978e"]]},{"id":"2b56677f3a45978e","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round($random()*10)","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"line1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":1640,"wires":[["4679617326a022be"]]},{"id":"4679617326a022be","type":"ui_chart","z":"65617ffeb779f51c","name":"","group":"212b7f06.b4731","order":1,"width":0,"height":0,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"bezier","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"15","removeOlderPoints":"100","removeOlderUnit":"60","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":390,"y":1640,"wires":[["49f83468789a421d"]]},{"id":"49f83468789a421d","type":"join","z":"65617ffeb779f51c","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":210,"y":1700,"wires":[["bbf97705f8f67996"]]},{"id":"f61f82547089abcd","type":"ui_button","z":"65617ffeb779f51c","name":"","group":"212b7f06.b4731","order":3,"width":"2","height":"1","label":"save","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"save","x":60,"y":1700,"wires":[["49f83468789a421d"]]},{"id":"bbf97705f8f67996","type":"switch","z":"65617ffeb779f51c","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"save","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":330,"y":1700,"wires":[["278e4b74f1bca82b"]]},{"id":"278e4b74f1bca82b","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.line1","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":1700,"wires":[["f299d075174ca8bb"]]},{"id":"f299d075174ca8bb","type":"file","z":"65617ffeb779f51c","name":"","filename":"chart.log","filenameType":"str","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"utf8","x":640,"y":1700,"wires":[[]]},{"id":"c7d0cd72e5c99dea","type":"ui_button","z":"65617ffeb779f51c","name":"","group":"212b7f06.b4731","order":4,"width":"2","height":"1","passthru":false,"label":"restore","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"","topicType":"str","x":60,"y":1760,"wires":[["39c67627238ee8ec"]]},{"id":"39c67627238ee8ec","type":"file in","z":"65617ffeb779f51c","name":"","filename":"chart.log","filenameType":"str","format":"utf8","encoding":"none","allProps":false,"x":190,"y":1760,"wires":[["d118be10ab2db950"]]},{"id":"d118be10ab2db950","type":"json","z":"65617ffeb779f51c","name":"Convert to JSON","property":"payload","action":"","pretty":true,"x":390,"y":1760,"wires":[["2b34cec3bd204b99","8d6f0176364cb65b"]]},{"id":"8d6f0176364cb65b","type":"ui_chart","z":"65617ffeb779f51c","name":"","group":"212b7f06.b4731","order":1,"width":0,"height":0,"label":"Restored Data","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"bezier","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"15","removeOlderPoints":"100","removeOlderUnit":"60","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":620,"y":1820,"wires":[[]]},{"id":"2b34cec3bd204b99","type":"debug","z":"65617ffeb779f51c","name":"chart debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":1760,"wires":[]},{"id":"212b7f06.b4731","type":"ui_group","name":"Gauge","tab":"e7f7ff66.2de07","order":2,"disp":true,"width":"6"},{"id":"e7f7ff66.2de07","type":"ui_tab","name":"Restore","icon":"dashboard"}]

I also removed your function and set up the save using a join. You will also need to add your paths back to the file nodes.
Hope it helps
[edit] It would be better to save incoming data to a db, then you could restore or show any period you wished.

thanks for the response.

As I was working through this I decided to bail on this idea. I get it that a database like MYSQL is the way to go with this so I'm gonna purse that.

Thanks
Rich

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