Persistent chart data in node-red

Node-red is a great environment for implementing home automation and monitoring. It comes with a dashboard ui that allows you to show charts and gauges of useful data.

By default the charts it provides lose their data on every reboot of your system or restart of node-red. Often you want to keep your chart data.

This guide shows you how to implement a re-usable subflow to make all your chart data persistent across node-red restarts or system reboots.

Create a new subflow

Create a new subflow

Once you have created the subflow rename it SaveChart and import the following flow into it:

[
    {
        "id": "35ad4fe4.069a3",
        "type": "subflow",
        "name": "SaveChart",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 80,
                "y": 60,
                "wires": [
                    {
                        "id": "f4457c36.28ce1"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 820,
                "y": 80,
                "wires": [
                    {
                        "id": "cf553dbd.50f9a",
                        "port": 0
                    }
                ]
            }
        ]
    },
    {
        "id": "db9cbd5c.fb547",
        "type": "file",
        "z": "35ad4fe4.069a3",
        "name": "",
        "filename": "",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "true",
        "x": 690,
        "y": 40,
        "wires": [
            []
        ]
    },
    {
        "id": "f7f46486.9881a8",
        "type": "file in",
        "z": "35ad4fe4.069a3",
        "name": "",
        "filename": "",
        "format": "utf8",
        "sendError": true,
        "x": 530,
        "y": 80,
        "wires": [
            [
                "cf553dbd.50f9a"
            ]
        ]
    },
    {
        "id": "ac2168dc.ee28f8",
        "type": "json",
        "z": "35ad4fe4.069a3",
        "name": "",
        "x": 530,
        "y": 40,
        "wires": [
            [
                "db9cbd5c.fb547"
            ]
        ]
    },
    {
        "id": "cf553dbd.50f9a",
        "type": "json",
        "z": "35ad4fe4.069a3",
        "name": "",
        "x": 690,
        "y": 80,
        "wires": [
            []
        ]
    },
    {
        "id": "f4457c36.28ce1",
        "type": "function",
        "z": "35ad4fe4.069a3",
        "name": "LoadSave",
        "func": "var strSafe=msg.topic;\nif(strSafe)\n{\n    strSafe=strSafe.replace(/[^a-z0-9]/gi, '_').toLowerCase();\n    var loaded=context.get(strSafe)||0;\n    msg.filename =\"/home/pi/charts/\"+strSafe+\".dat\";\n    //node.warn(loaded);\n    if(0===loaded)\n    {\n        //load chart!! \n        msg.payload=\"load\";\n        context.set(strSafe,1);\n        \n        //node.warn(\"Loading \" +strSafe);\n    }\n    else\n    {\n        //node.warn(\"Writing \" +strSafe);\n    }\n    return msg;    \n}\nreturn null;\n\n",
        "outputs": 1,
        "noerr": 0,
        "x": 230,
        "y": 60,
        "wires": [
            [
                "99a5d40b.95d618"
            ]
        ]
    },
    {
        "id": "99a5d40b.95d618",
        "type": "switch",
        "z": "35ad4fe4.069a3",
        "name": "Load data",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "load",
                "vt": "str"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 380,
        "y": 60,
        "wires": [
            [
                "ac2168dc.ee28f8"
            ],
            [
                "f7f46486.9881a8"
            ]
        ]
    }
]

It should now look like this

The new subflow

Using the subflow

To use the persistent chart data subflow you just need to drag it onto your flow near your chart and wire up the input and output of the chart to the SaveChart subflow as shown:

The chart with persistent data
Things to note:
  1. Ensure that the incoming data messages have a unique topic for each chart you want to save as the filename used is based on the msg.topic of the incoming data.
  2. The chart data is only loaded when the first message is received, so charts will remain blank until a new data point arrives
  3. By default chart data is stored in /home/pi/charts/{msg.topic}.dat to change this edit the function node in the subflow and change the path in there