Hubitat > NodeRed > MySQL > Grafana (LONG READ)

I have this working for me. I used @btk's flow as a template and made a few tweaks. Essentially it does the following:

  • Queries the database for latest values of temp, battery and humidity measurements.
  • Scans the timestamps for anything that hasn't been updated in the past 15 minutes.
  • Pushes those measurements back to the database with updated timestamps.

This process repeats every 5 minutes. Here's a picture of my flow. The section described above, is below the red line.

This is that portion of the flow. You will need to attach the last node to you Influx node as pictured and also edit the "Build InfluxQL" node to set your influx database name.

[
    {
        "id": "c2d313b9.6088b",
        "type": "inject",
        "z": "6f5a02a4.0a0dac",
        "name": "w.1s r5m",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "300",
        "crontab": "",
        "once": true,
        "onceDelay": "0.1",
        "x": 170,
        "y": 420,
        "wires": [
            [
                "66c366b.cd2bc98",
                "dc937be3.d13a78",
                "d3b80106.60246"
            ]
        ]
    },
    {
        "id": "ef96983d.de2838",
        "type": "function",
        "z": "6f5a02a4.0a0dac",
        "name": "Find Stale Data",
        "func": "now = new Date();\n//state = flow.get(\"deviceList\");\n\nupdates = [];\nmsg.points = msg.payload;\npoints = msg.payload;\n\npoints.forEach(point => {\n    delta = ((now - point[\"time\"]) / 60000)\n\n    if (delta > 15) {\n        update = {\n            measurement: msg.measurement,\n            fields: {\n                // value: state[parseInt(point.deviceId)][]\n                value: point.value\n            },\n            tags:{\n                hub: msg.hubId ? msg.hubId : \"HE1\",\n                deviceId: parseInt(point.deviceId),\n                displayName: point.displayName,\n                unit: point.unit,\n                synthetic: true\n            },\n            delta: delta,\n            timestamp: new Date()\n        };\n        \n        updates.push(update);\n    }\n});\n\nmsg.payload = updates;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 920,
        "y": 420,
        "wires": [
            []
        ]
    },
    {
        "id": "dc937be3.d13a78",
        "type": "change",
        "z": "6f5a02a4.0a0dac",
        "name": "Humidity",
        "rules": [
            {
                "t": "set",
                "p": "measurement",
                "pt": "msg",
                "to": "humidity",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 350,
        "y": 380,
        "wires": [
            [
                "56cdf119.0ed5e"
            ]
        ]
    },
    {
        "id": "66c366b.cd2bc98",
        "type": "change",
        "z": "6f5a02a4.0a0dac",
        "name": "Temperature",
        "rules": [
            {
                "t": "set",
                "p": "measurement",
                "pt": "msg",
                "to": "temperature",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 350,
        "y": 420,
        "wires": [
            [
                "56cdf119.0ed5e"
            ]
        ]
    },
    {
        "id": "56cdf119.0ed5e",
        "type": "function",
        "z": "6f5a02a4.0a0dac",
        "name": "Build InfluxQL",
        "func": "msg.query  = 'SELECT \"deviceId\", last(\"value\") AS \"value\", unit ';\nmsg.query += 'FROM \"YOURinfluxDB\".\"autogen\".\"' + msg.measurement + '\" ';\nmsg.query += 'GROUP BY displayName';\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 540,
        "y": 420,
        "wires": [
            [
                "3e682976.fccf96"
            ]
        ]
    },
    {
        "id": "d3b80106.60246",
        "type": "change",
        "z": "6f5a02a4.0a0dac",
        "name": "Battery",
        "rules": [
            {
                "t": "set",
                "p": "measurement",
                "pt": "msg",
                "to": "battery",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 350,
        "y": 460,
        "wires": [
            [
                "56cdf119.0ed5e"
            ]
        ]
    },
    {
        "id": "3e682976.fccf96",
        "type": "influxdb in",
        "z": "6f5a02a4.0a0dac",
        "influxdb": "a621305e.2cea1",
        "name": "Get Latest Data",
        "query": "",
        "rawOutput": false,
        "precision": "",
        "retentionPolicy": "",
        "x": 730,
        "y": 420,
        "wires": [
            [
                "ef96983d.de2838"
            ]
        ]
    },
    {
        "id": "a621305e.2cea1",
        "type": "influxdb",
        "z": "",
        "hostname": "127.0.0.1",
        "port": "8086",
        "protocol": "http",
        "database": "nucflux1",
        "name": "NucFlux1",
        "usetls": false,
        "tls": ""
    }
]
3 Likes