# Bad data inject in cart

**URL:** <https://discourse.nodered.org/t/bad-data-inject-in-cart/86351>\
**Category:** Dashboard\
**Tags:** database\
**Created:** [11 March 2024 22:38 UTC](https://discourse.nodered.org/t/bad-data-inject-in-cart/86351 "2024-03-11T22:38:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nichlas](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Nichlas](https://discourse.nodered.org/u/Nichlas)\
**Post date:** [11 March 2024 22:38 UTC](https://discourse.nodered.org/t/bad-data-inject-in-cart/86351/1 "2024-03-11T22:38:30Z")

</div>

Hi i using Sqlite database and store my temperature readings, i want to get it back in my chart when i have for example reboot my pi.  
So i am receiving it back but the chart says bad data.

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e5a81e9e3dd25e9ac2c3f86fb1e9e3b344948df1.png)

```auto
[
    {
        "id": "a5f54cfd7a4398c2",
        "type": "join",
        "z": "f5573b729c358c30",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "timeout": "",
        "count": "",
        "x": 1230,
        "y": 980,
        "wires": [
            [
                "65c4ac924b837210"
            ]
        ]
    },
    {
        "id": "9e9cf1fcd9d295c7",
        "type": "sqlite",
        "z": "f5573b729c358c30",
        "mydb": "d61203bc8b18fecb",
        "sqlquery": "msg.topic",
        "sql": "",
        "name": "temp",
        "x": 1090,
        "y": 980,
        "wires": [
            [
                "a5f54cfd7a4398c2"
            ]
        ]
    },
    {
        "id": "10be4ea27a8be610",
        "type": "function",
        "z": "f5573b729c358c30",
        "name": "SQL",
        "func": "// This will handle any device and any attribute as long as it is in the DB\nvar Week = 604800000 ; //7 Days\nvar Day = 86400000 ; // 1 Days\nvar d = new Date();\nvar epoch = d.getTime();\nvar fromdate = epoch - Day;\nvar enddate = epoch;\nvar output = [];\n\nfor (var property in msg.payload) {\n if (msg.payload.hasOwnProperty(property)) {\n //output.push({ payload: property + \" has value \"+msg.payload[property] });\n if (msg.payload[property]) {\n var parts = property.split(\"/\");\n if (parts[0]===\"period\") {\n switch (parts[1]) {\n case \"today\":\n fromdate = epoch-Day;\n enddate = epoch;\n break;\n case \"yesterday\":\n fromdate = epoch-2*Day;\n enddate = epoch-Day;\n break;\n case \"week\":\n fromdate = epoch-Week;\n enddate = epoch;\n break;\n }\n } else {\n output.push({ topic: \"SELECT * FROM temp WHERE device='tado' AND sensor='övervåning' AND epoch >= \" + fromdate + \" AND epoch <= \" + enddate });\n }\n }\n }\n}\n\noutput[output.length-1].complete=true;\n\nreturn [output];\n\n//msg.topic = \"SELECT * FROM sensor_data WHERE device='growatt' AND sensor='power' AND epoch >= \" + fromdate + \" AND epoch <= \" + epoch ;\n//return msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 910,
        "y": 980,
        "wires": [
            [
                "9e9cf1fcd9d295c7"
            ]
        ]
    },
    {
        "id": "52faf50518cdfbc4",
        "type": "ui_chart",
        "z": "f5573b729c358c30",
        "name": "",
        "group": "3492783650a37422",
        "order": 1,
        "width": 0,
        "height": 0,
        "label": "charttest",
        "chartType": "line",
        "legend": "false",
        "xformat": "%a %H:%M",
        "interpolate": "linear",
        "nodata": "",
        "dot": false,
        "ymin": "",
        "ymax": "",
        "removeOlder": 1,
        "removeOlderPoints": "",
        "removeOlderUnit": "604800",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#1f77b4",
            "#aec7e8",
            "#ff7f0e",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 1520,
        "y": 980,
        "wires": [
            []
        ]
    },
    {
        "id": "195b40d060715841",
        "type": "inject",
        "z": "f5573b729c358c30",
        "name": "inject JSON object",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "",
        "topic": "",
        "payload": "{ \"period/today\": true, \"period/yesterday\": false, \"period/week\": false, \"device\": false, \"complete\":true }",
        "payloadType": "json",
        "x": 710,
        "y": 980,
        "wires": [
            [
                "10be4ea27a8be610"
            ]
        ]
    },
    {
        "id": "65c4ac924b837210",
        "type": "function",
        "z": "f5573b729c358c30",
        "name": "Prep",
        "func": "var msg2 = [];\n\n\n// this is the logic when there are multiple data sets are received\nfor (var i=0; i<msg.payload.length; i++) {\n var output = [];\n ({\n \"seriers \": ( \"x\" )\n })\n for (var j=0; j<msg.payload[i].length; j++) {\n output.push([msg.payload[i][j].epoch, msg.payload[i][j].value]);\n }\n msg2.push({ key: +msg.payload[i][0].device+\"/\"+msg.payload[i][0].data_type, values : output});\n //msg2.push({ key: \"test\", values : output});\n\n\n}\n\nmsg.payload=msg2;\n//msg.payload = [{ key: \"Power\", values : output}];\n//msg.topic = \"Power\";\nreturn msg;\n\n",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1370,
        "y": 980,
        "wires": [
            [
                "52faf50518cdfbc4",
                "e2a88d51ea0860d7"
            ]
        ]
    },
    {
        "id": "e2a88d51ea0860d7",
        "type": "debug",
        "z": "f5573b729c358c30",
        "name": "debug 78",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1420,
        "y": 1080,
        "wires": []
    },
    {
        "id": "d61203bc8b18fecb",
        "type": "sqlitedb",
        "db": "/home/ander/sensordata.db",
        "mode": "RWC"
    },
    {
        "id": "3492783650a37422",
        "type": "ui_group",
        "name": "Group 1",
        "tab": "9307492ee488c070",
        "order": 1,
        "disp": true,
        "width": 6
    },
    {
        "id": "9307492ee488c070",
        "type": "ui_tab",
        "name": "Test återtäll databas",
        "icon": "dashboard",
        "order": 9,
        "disabled": false,
        "hidden": false
    }
]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 March 2024 23:02 UTC](https://discourse.nodered.org/t/bad-data-inject-in-cart/86351/2 "2024-03-11T23:02:15Z")

</div>

What does the schema look like?  
What does the code inserting into the sqlite db look like?

---

<div class="post-metadata">

**Author:** ![Nichlas](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Nichlas](https://discourse.nodered.org/u/Nichlas)\
**Post date:** [12 March 2024 06:10 UTC](https://discourse.nodered.org/t/bad-data-inject-in-cart/86351/3 "2024-03-12T06:10:05Z")

</div>

The schema looks as id, device, sensor, epoch, timestamp.

Here is the insert.

```auto
[{"id":"1cbd6d6efa672864","type":"mqtt in","z":"f5573b729c358c30","name":"","topic":"homie/homey-topic/overvaning---smart-thermostat/measure-temperature","qos":"2","datatype":"auto-detect","broker":"6707fe9a4feab087","nl":false,"rap":true,"rh":0,"inputs":0,"x":290,"y":1640,"wires":[["afdaccb278892027","76eb42d6a0851efe"]]},{"id":"afdaccb278892027","type":"ui_chart","z":"f5573b729c358c30","name":"Graf övervåning","group":"dc3e87c730d020c7","order":2,"width":11,"height":7,"label":"klimat övervåning","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#00ff00","#fbff00","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":1120,"y":1640,"wires":[[]]},{"id":"76eb42d6a0851efe","type":"function","z":"f5573b729c358c30","name":"Save to DB","func":"var sql = \"\";\nvar d = new Date();\nvar epoch = d.getTime();\nvar outputs = [];\n\nsql = \"INSERT INTO temp (device,sensor,value,epoch) \"+\n \"VALUES ('tado','övervåning',\"+msg.payload+\",\"+epoch+\")\";\noutputs.push({topic:sql}); \n\nreturn [outputs];\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":1640,"wires":[["f0a585505f05975e"]]},{"id":"f0a585505f05975e","type":"sqlite","z":"f5573b729c358c30","mydb":"d61203bc8b18fecb","sqlquery":"msg.topic","sql":"","name":"temp","x":1090,"y":1680,"wires":[["77b27f384c633ddb"]]},{"id":"77b27f384c633ddb","type":"debug","z":"f5573b729c358c30","name":"debug 81","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1240,"y":1680,"wires":[]},{"id":"6707fe9a4feab087","type":"mqtt-broker","name":"","broker":"192.168.86.27","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"dc3e87c730d020c7","type":"ui_group","name":"Graf","tab":"ade00013b9260b51","order":1,"disp":false,"width":22,"collapse":false,"className":""},{"id":"d61203bc8b18fecb","type":"sqlitedb","db":"/home/ander/sensordata.db","mode":"RWC"},{"id":"ade00013b9260b51","type":"ui_tab","name":"Grafer temp","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 March 2024 10:52 UTC](https://discourse.nodered.org/t/bad-data-inject-in-cart/86351/4 "2024-03-12T10:52:26Z")

</div>

Please add a debug node to the ouput of the `join` node and then copy and paste the results to a reply

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 March 2024 11:05 UTC](https://discourse.nodered.org/t/bad-data-inject-in-cart/86351/5 "2024-03-12T11:05:07Z")

</div>

Have you looked at this page detailing chart input data, as bad inject refers to data not being in correct format. [node-red-dashboard/Charts.md at master · node-red/node-red-dashboard · GitHub](https://github.com/node-red/node-red-dashboard/blob/master/Charts.md)

---

<div class="post-metadata">

**Author:** ![Nichlas](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Nichlas](https://discourse.nodered.org/u/Nichlas)\
**Post date:** [12 March 2024 20:57 UTC](https://discourse.nodered.org/t/bad-data-inject-in-cart/86351/6 "2024-03-12T20:57:58Z")

</div>

Exactly what i was looking for, thanks.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 March 2024 21:02 UTC](https://discourse.nodered.org/t/bad-data-inject-in-cart/86351/7 "2024-03-12T21:02:36Z")

</div>

Just to help you in future, This info was available in the nodes sidebar help text. Always a good idea to read the nodes help text, in most cases the info you require can be found there.

---

<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:** [26 March 2024 21:02 UTC](https://discourse.nodered.org/t/bad-data-inject-in-cart/86351/8 "2024-03-26T21:02:49Z")

</div>

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