Node-Red data showing NaN

Hello everybody!

I'm new to Node-Red so please go easy on me :slightly_smiling_face:

I am trying to populate sensor data collected from an Arduino on to google sheets using Node-Red. However, the sensor data comes out as NaN all the time.

I've made sure to have my Arduino data come out in JSON format.

Please tell me where i went wrong

[
    {
        "id": "3a5ea13849faccec",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "ed5bdc1bbfa436e9",
        "type": "serial in",
        "z": "3a5ea13849faccec",
        "name": "ghARD",
        "serial": "fc4c36463f1c3b45",
        "x": 210,
        "y": 140,
        "wires": [
            [
                "59c9a7d0c6cbb8dd"
            ]
        ]
    },
    {
        "id": "59c9a7d0c6cbb8dd",
        "type": "json",
        "z": "3a5ea13849faccec",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 370,
        "y": 140,
        "wires": [
            [
                "fa4e033682da9e8a",
                "4664a32c8bf03024",
                "08307f51761c60e7"
            ]
        ]
    },
    {
        "id": "c3b261b41476b5ea",
        "type": "inject",
        "z": "3a5ea13849faccec",
        "name": "2s Counter",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "2",
        "crontab": "",
        "once": true,
        "onceDelay": "2",
        "topic": "",
        "payloadType": "date",
        "x": 270,
        "y": 400,
        "wires": [
            [
                "6657077ae35e3eb7"
            ]
        ]
    },
    {
        "id": "6657077ae35e3eb7",
        "type": "function",
        "z": "3a5ea13849faccec",
        "name": "",
        "func": "var ghARD = flow.get('ghARD')||{\"MoistureValue\":0, \"hum\":0, \"temp\":0, \"phValue\":0,\"light_intentsity\":0, \"count\":0};\n\nvar output = new Array(6);\n\noutput[0] = new Date();\noutput[0].setHours(output[0].getHours()+8);\noutput[1] = Math.round(ghARD.MoistureValue/ghARD.count);\noutput[2] = Math.round(ghARD.hum/ghARD.count);\noutput[3] = Math.round(ghARD.temp/ghARD.count);\noutput[4] = Math.round(ghARD.phValue/ghARD.count);\noutput[5] = Math.round(ghARD.light_intensity/ghARD.count);\n\nflow.set('ghARD',{\"MoistureValue\":0, \"hum\":0, \"temp\":0, \"phValue\":0,\"light_intentsity\":0, \"count\":0});\n\nmsg.payload = output;\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 400,
        "wires": [
            []
        ]
    },
    {
        "id": "5091f180495e94f8",
        "type": "GSheet",
        "z": "3a5ea13849faccec",
        "creds": "34b64851ad69da8f",
        "method": "append",
        "action": "",
        "sheet": "1WQDeAQET3ZA-nVZ89tmNycOpvItl7_k8CSzZJUtUU80",
        "cells": "Datasheet!A:Z",
        "flatten": false,
        "name": "",
        "x": 1130,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "fa4e033682da9e8a",
        "type": "mqtt out",
        "z": "3a5ea13849faccec",
        "name": "",
        "topic": "ghARD",
        "qos": "2",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "ca2321cc.9faff",
        "x": 680,
        "y": 180,
        "wires": []
    },
    {
        "id": "4664a32c8bf03024",
        "type": "debug",
        "z": "3a5ea13849faccec",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 100,
        "wires": []
    },
    {
        "id": "08307f51761c60e7",
        "type": "function",
        "z": "3a5ea13849faccec",
        "name": "",
        "func": "var ghARD = flow.get('ghARD')||{\"MoistureValue\":0, \"hum\":0, \"temp\":0, \"phValue\":0, \"light_intensity\":0, \"count\":0};\n\nghARD.MoistureValue += msg.payload.MoistureValue;\nghARD.hum += msg.payload.hum;\nghARD.temp += msg.payload.temp;\nghARD.phValue += light_intensity;\nghARD.count += 1;\nflow.set('ghARD',ghARD);\n\nreturn ghARD;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 690,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "93204e69d7aea425",
        "type": "debug",
        "z": "3a5ea13849faccec",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 790,
        "y": 400,
        "wires": []
    },
    {
        "id": "fc4c36463f1c3b45",
        "type": "serial-port",
        "serialport": "COM9",
        "serialbaud": "57600",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "\\n",
        "bin": "false",
        "out": "char",
        "addchar": "",
        "responsetimeout": "10000"
    },
    {
        "id": "34b64851ad69da8f",
        "type": "gauth",
        "name": "greenhouse@green-house-340803.iam.gserviceaccount.com"
    },
    {
        "id": "ca2321cc.9faff",
        "type": "mqtt-broker",
        "name": "",
        "broker": "tcp://broker.hivemq.com",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": false,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]

Hi.

Just something you should do:

Try to make a smaller flow that only causes the problem you have.

Get rid of the extra nodes.

Have an inject node that injects the value you think is in the file.
Inject that into the node that gives the error. See what happens.

If that works, then change the flow slightly so you read in from the file and pass the value to the node. See what happens.

One thing to be aware of is the type of variable it is.
If you (by what ever means) write a string value to the vile there's your problem.

One way around this is to read the file and convert the value read to an integer.

Look for parseINT()

Welcome to the forum @weishoose

I can't look at your flow at the moment, but what you need to do is to start where the data comes into node red and use debug nodes to check it is what you expect. If that is correct then move past the next node in the flow and check it is correct there, and so on.

Once you work out exactly where it is failing, if you can't understand why, then show us what is going into that node and what is coming out so we can help.

Thank you for the reply!


It seems like the problem is coming from the data coming out from my Arduino. However my data is already arranged in JSON format.

Is my data formatted properly? or am i missing something?

Add the Debug node to the output of the serial node so we can see what it is recieving and in what format.

What version of Node-RED are you using?

Hi Knolleary,

Capture
This is what I get when i connect the Serial Out to the debug node. A string which contains my sensor data.

However, when i connect Serial Out > json > Debug, it shows "Ignored non object payload"

If it is the first one, that is an array.
So you will have to look at / parse msg.payload[x] where x points to the part of the array you want to use.

And what version of Node-RED are you using?

The problem is the payload coming out of the Serial node is a Buffer object, not a String.

Node-RED 2.2 included an update for the JSON node that would allow it to handle Buffer type payloads.

On earlier versions, you would have to add a Function node after the Serial node to convert the Buffer to a String:

msg.payload = msg.payload.toString()
return msg

This worked! I updated node-red and my data begun populating.

You have no idea how happy i am right now. Thank you! I really appreciate it

1 Like

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