How to check function node is working correctly

I am needing help trying to figure out why my function node isn't separating the time, date and TC data correctly. My code is below. Any help would be greatly appreciated.

[
    {
        "id": "73e4e16.4d9742",
        "type": "inject",
        "z": "f5c39bc78c9f43a9",
        "name": "Inject",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[{\"Date\":\"2023-09-03\",\"Time\":\"12:37:43\",\"TC1\":7.814468887624555,\"TC2\":9.010489315422356,\"TC3\":2.2051948357794204,\"TC4\":4.634575718286296},{\"Date\":\"2023-09-03\",\"Time\":\"12:40:42\",\"TC1\":1.2247009897949532,\"TC2\":4.502620787639197,\"TC3\":4.220188281544026,\"TC4\":1.9340397044971718},{\"Date\":\"2023-09-03\",\"Time\":\"14:18:02\",\"TC1\":2.5243351824265963,\"TC2\":6.036543260510911,\"TC3\":6.538703425751857,\"TC4\":3.7277151250599543}]",
        "payloadType": "jsonata",
        "x": 350,
        "y": 200,
        "wires": [
            [
                "90ed51dc.dcc71"
            ]
        ]
    },
    {
        "id": "90ed51dc.dcc71",
        "type": "csv",
        "z": "f5c39bc78c9f43a9",
        "name": "",
        "sep": ",",
        "hdrin": true,
        "hdrout": "once",
        "multi": "mult",
        "ret": "\\n",
        "temp": "",
        "skip": "1",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 530,
        "y": 200,
        "wires": [
            [
                "9aace6e7.adc538",
                "b05f7f856d809326"
            ]
        ]
    },
    {
        "id": "9aace6e7.adc538",
        "type": "debug",
        "z": "f5c39bc78c9f43a9",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 670,
        "y": 140,
        "wires": []
    },
    {
        "id": "137d11d9.8c82ee",
        "type": "ui_chart",
        "z": "f5c39bc78c9f43a9",
        "name": "",
        "group": "a649fab2.daf3d8",
        "order": 7,
        "width": 18,
        "height": 6,
        "label": "Temperature Chart",
        "chartType": "line",
        "legend": "false",
        "xformat": "auto",
        "interpolate": "linear",
        "nodata": "",
        "dot": false,
        "ymin": "",
        "ymax": "",
        "removeOlder": 1,
        "removeOlderPoints": "",
        "removeOlderUnit": "3600",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#1f77b4",
            "#aec7e8",
            "#ff7f0e",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 1010,
        "y": 340,
        "wires": [
            []
        ]
    },
    {
        "id": "b05f7f856d809326",
        "type": "function",
        "z": "f5c39bc78c9f43a9",
        "name": "DateTime_Data_Split",
        "func": "function toTimestamp(strDate) {\n    var datum = Date.parse(strDate);\n    return datum / 1000;\n}\n\nvar payload = msg.payload;\n\nvar data_out = [];\nvar a1 = [];\nvar a2 = [];\nvar a3 = [];\nvar a4 = [];\n\nfor (let i = 0; i < payload.length; i++) {\n    if (payload[i].Date) {\n        console.log(\"Date:\", payload[i].Date);\n        console.log(\"Time:\", payload[i].Time);\n        var Data = payload[i].Date.split(\"-\");\n        var t = Data[2] + \"/\" + Data[1] + \"/\" + Data[0];\n        var timestamp = toTimestamp(t + \" \" + payload[i].Time);\n\n        var temp1 = { \"x\": timestamp, \"y\": parseFloat(payload[i].TC1) };\n        var temp2 = { \"x\": timestamp, \"y\": parseFloat(payload[i].TC2) };\n        var temp3 = { \"x\": timestamp, \"y\": parseFloat(payload[i].TC3) };\n        var temp4 = { \"x\": timestamp, \"y\": parseFloat(payload[i].TC4) };\n\n        a1.push(temp1);\n        a2.push(temp2);\n        a3.push(temp3);\n        a4.push(temp4);\n\n        console.log(\"temp1:\", temp1);\n        console.log(\"temp2:\", temp2);\n        console.log(\"temp3:\", temp3);\n        console.log(\"temp4:\", temp4);\n    }\n}\n\ndata_out = [a1, a2, a3, a4];\n\nmsg.payload = [{\n    \"series\": [\"TC1\", \"TC2\", \"TC3\", \"TC4\"],\n    \"data\": data_out,\n    \"labels\": [\"TC1\", \"TC2\", \"TC3\", \"TC4\"]\n}];\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 740,
        "y": 340,
        "wires": [
            [
                "137d11d9.8c82ee",
                "b27011309a3a3080"
            ]
        ]
    },
    {
        "id": "b27011309a3a3080",
        "type": "debug",
        "z": "f5c39bc78c9f43a9",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 970,
        "y": 200,
        "wires": []
    },
    {
        "id": "a649fab2.daf3d8",
        "type": "ui_group",
        "name": "File Browser",
        "tab": "75e4e587.f4c0ac",
        "order": 1,
        "disp": true,
        "width": "18",
        "collapse": false
    },
    {
        "id": "75e4e587.f4c0ac",
        "type": "ui_tab",
        "name": "Files",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

It would be better if you can show us the function code, the msg going in and the value you expect Vs the value you are getting.

Your function works for me when the csv node is removed, as it converts your injected array of objects into a csv text string.
Also your timestamp function divides by 1000 which converts the timestamp to a unix timestamp in seconds, when the chart wants a unix timestamp in milliseconds or a ISO formated timestamp.

[edit]
If the input is a csv then this example of how to simulate csv input may help

[{"id":"d7cd17c8792d2a86","type":"inject","z":"b9860b4b9de8c8da","name":"csv input","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":440,"y":460,"wires":[["52accde3cd474244"]]},{"id":"52accde3cd474244","type":"template","z":"b9860b4b9de8c8da","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Date,Time,TC1,TC2,TC3,TC4\n2023-09-03,12:37:43,7.814468887624555,9.010489315422356,2.2051948357794204,4.634575718286296\n2023-09-03,12:40:42,1.2247009897949532,4.502620787639197,4.220188281544026,1.9340397044971718\n2023-09-03,14:18:02,2.5243351824265963,6.036543260510911,6.538703425751857,3.7277151250599543\n","output":"str","x":540,"y":420,"wires":[["ba455151a9c1b04f"]]},{"id":"ba455151a9c1b04f","type":"csv","z":"b9860b4b9de8c8da","name":"","sep":",","hdrin":true,"hdrout":"all","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":650,"y":460,"wires":[["9aace6e7.adc538","b05f7f856d809326"]]},{"id":"9aace6e7.adc538","type":"debug","z":"b9860b4b9de8c8da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":360,"wires":[]},{"id":"b05f7f856d809326","type":"function","z":"b9860b4b9de8c8da","name":"DateTime_Data_Split","func":"function toTimestamp(strDate) {\n    var datum = Date.parse(strDate);\n    return datum;\n}\n\nvar payload = msg.payload;\n\nvar data_out = [];\nvar a1 = [];\nvar a2 = [];\nvar a3 = [];\nvar a4 = [];\n\nfor (let i = 0; i < payload.length; i++) {\n    if (payload[i].Date) {\n        console.log(\"Date:\", payload[i].Date);\n        console.log(\"Time:\", payload[i].Time);\n        var Data = payload[i].Date.split(\"-\");\n        var t = Data[2] + \"/\" + Data[1] + \"/\" + Data[0];\n        var timestamp = toTimestamp(t + \" \" + payload[i].Time);\n\n        var temp1 = { \"x\": timestamp, \"y\": parseFloat(payload[i].TC1) };\n        var temp2 = { \"x\": timestamp, \"y\": parseFloat(payload[i].TC2) };\n        var temp3 = { \"x\": timestamp, \"y\": parseFloat(payload[i].TC3) };\n        var temp4 = { \"x\": timestamp, \"y\": parseFloat(payload[i].TC4) };\n\n        a1.push(temp1);\n        a2.push(temp2);\n        a3.push(temp3);\n        a4.push(temp4);\n\n        console.log(\"temp1:\", temp1);\n        console.log(\"temp2:\", temp2);\n        console.log(\"temp3:\", temp3);\n        console.log(\"temp4:\", temp4);\n    }\n}\n\ndata_out = [a1, a2, a3, a4];\n\nmsg.payload = [{\n    \"series\": [\"TC1\", \"TC2\", \"TC3\", \"TC4\"],\n    \"data\": data_out,\n    \"labels\": [\"TC1\", \"TC2\", \"TC3\", \"TC4\"]\n}];\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":540,"wires":[["b27011309a3a3080"]]},{"id":"73e4e16.4d9742","type":"inject","z":"b9860b4b9de8c8da","name":"object input","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Date\":\"2023-09-03\",\"Time\":\"12:37:43\",\"TC1\":7.814468887624555,\"TC2\":9.010489315422356,\"TC3\":2.2051948357794204,\"TC4\":4.634575718286296},{\"Date\":\"2023-09-03\",\"Time\":\"12:40:42\",\"TC1\":1.2247009897949532,\"TC2\":4.502620787639197,\"TC3\":4.220188281544026,\"TC4\":1.9340397044971718},{\"Date\":\"2023-09-03\",\"Time\":\"14:18:02\",\"TC1\":2.5243351824265963,\"TC2\":6.036543260510911,\"TC3\":6.538703425751857,\"TC4\":3.7277151250599543}]","payloadType":"json","x":410,"y":520,"wires":[["b05f7f856d809326"]]},{"id":"b27011309a3a3080","type":"debug","z":"b9860b4b9de8c8da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1010,"y":420,"wires":[]}]

As to how to check function is working correctly, you can use node.warn() as well as the console.log() you are already using.

OK, I wasn't sure why I needed the CSV node. Now I know I don't need it. Thank You!

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