# Sensor data turned into null value after function node

**URL:** <https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719>\
**Category:** General\
**Tags:** function-node\
**Created:** [22 August 2023 16:28 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719 "2023-08-22T16:28:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mstk\_olsen](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mstk\_olsen](https://discourse.nodered.org/u/mstk_olsen)\
**Post date:** [22 August 2023 16:28 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/1 "2023-08-22T16:28:05Z")

</div>

Hi everyone, I'm developing a smart weather monitoring system which I mainly use Arduino Uno microcontroller, DHT11 for collecting temperature and humidity, BMP180 for pressure and altitude, and LDR sensor for light level. I've uploaded my arduino sketch to the microcontroller and its working.  
The output will be something like this:

Humidity,Temperature(Celcius): 65.00,24.40  
Altitude, Atmospheric Pressure: 65.65,1005.28  
Luminousity: 423

I tried to use node red as a platform and I've noticed my sensor data coming serial port got turned into null values after passing through my function node. I have been scratching my head for hours now still don't know how to solve it.

Here is my function node code:

```auto
let data = (typeof msg.payload === 'string' ? msg.payload : msg.payload.toString()).split(',');

let Humidity = { payload: parseFloat(data[0]) };

let Temperature = { payload: parseFloat(data[1]) };

return [Humidity, Temperature];

```

Here is the debug output I connected the serial port node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ffb6fcc595a090882e581ae6aa3702c101e81614.png)

And here is the debug output when I connect the debug node to the function node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd6ea3a84c3ccca5c44817bc22dc459e581b662c.png)

I'm trying to solve for temperature & humidity first, I'll do the same thing for pressure & altitude and light levels if the solution have been found.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [22 August 2023 17:28 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/2 "2023-08-22T17:28:43Z")

</div>

Hi @mstk_olsen, welcome to the forum.

The data coming into Node-red looks like a string:  
 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f67513e2cb3742e6f6e337d273d844d1e03359c5.jpeg)  
Your function first makes certain that it _is_ a string and then assumes it's an array of strings.  
I do not know what data[0] contains but for sure it can't be recognised as a number, so you end up with two messages whose payload is NaN (Not A Number)

You could handle it by splitting the string into an array at the ':' and splitting array[1] into another array at the ','

But it would life much easier if you made your Arduino send the data formatted as a Json string

```auto
{
"Humidity": 60.0,
"Temperature": 24.10,
"TemperatureUnits": "Celsius"
}

```

Then pass the serial output through a Json parser node and the values will be available as msg.payload.Humidity and msg.payload.Temperature.

---

<div class="post-metadata">

**Author:** ![mstk\_olsen](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mstk\_olsen](https://discourse.nodered.org/u/mstk_olsen)\
**Post date:** [22 August 2023 17:55 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/3 "2023-08-22T17:55:44Z")

</div>

I see... But I suspect that it should be my Arduino Serial.println that messed up. But mind if you can take a look at it and see if it was problem there?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fbf8e4f02ad4347d01262b6be36c1200b5a06b71.png)

EDIT: I think I managed to seperate somehow after i deleted the description of "Humidity, Temperature(Celcius): "......, but the values is messed up in node red.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/89837a7b9b77140c2d28512e3a8b996b48e23a37.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 August 2023 18:34 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/4 "2023-08-22T18:34:16Z")

</div>

> [@mstk\_olsen](#):
>
> but the values is messed up in node red.

That is exactly why @jbudd suggested

> [@jbudd](#):
>
> But it would life much easier if you made your Arduino send the data formatted as a Json string
> 
> ```auto
> {
> "Humidity": 60.0,
> "Temperature": 24.10,
> "TemperatureUnits": "Celsius"
> }
> 
> ```

Because JavaScript knows EXACTLY how to convert the parts of JSON into string/number without any `parseInt` calls, without any string splitting or other parsing.

* * *

In short: modify your Arduino code to send `{ "Humidity": 60.0, "Temperature": 24.10, "TemperatureUnits": "Celsius" }` and you will have no problems. And if you change the data at some future point to include `stationId: 1` it will "just work" (because thats what JSON was designed for) 🙂

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [22 August 2023 18:47 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/5 "2023-08-22T18:47:04Z")

</div>

Have a look at the ArduinoJson library to format a Json string for you.  
Version 6 is I believe the latest released version.  
It works really well with my Wemos D1 Minis (ESP8266) sending data to Node-RED via MQTT.

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6eb6e733281c2662bc747e521667ae8f1d378278.jpeg "How to upgrade from ArduinoJson 5 to 6") ](https://www.youtube.com/watch?v=l8TYLFbo9ws)

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [22 August 2023 19:08 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/6 "2023-08-22T19:08:21Z")

</div>

Or..... send the data in three or four messages. There's more than one way to skin, filet and make cat soup.

---

<div class="post-metadata">

**Author:** ![mstk\_olsen](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mstk\_olsen](https://discourse.nodered.org/u/mstk_olsen)\
**Post date:** [22 August 2023 19:54 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/7 "2023-08-22T19:54:35Z")

</div>

Ok so now i formatted the data into JSON but in my node red is not showing up anything.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/3894bf51b2ca3358cef8fb886a6fe193cc4b9a55.png)  
Arduino code:

```auto
#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include <DHT.h>

#define DHTPIN 7 
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);  

Adafruit_BMP085 bmp;

void setup() {
  Serial.begin(9600); 
  dht.begin();

  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1);
  }
}

void loop() {
   float Temperature = dht.readTemperature();   
   float Humidity = dht.readHumidity();  

 

    if (isnan(Temperature) || isnan(Humidity)){
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
    float bmppressure = bmp.readPressure()/100.00;
    float bmpaltitude = bmp.readAltitude();

    unsigned int ldrValue;
    ldrValue = analogRead(A0);
    StaticJsonDocument<200> jsonDoc;
    jsonDoc["dht"]["humidity"] = Humidity;
    jsonDoc["dht"]["temperature"] = Temperature;
    jsonDoc["bmp"]["altitude"] = bmpaltitude;
    jsonDoc["bmp"]["pressure"] = bmppressure;
    jsonDoc["ldr"]["lightLevel"] = ldrValue;
    serializeJson(jsonDoc, Serial);
  
    /*String dhtData = String(Humidity) + "," + String(Temperature);
    String bmpData = String(bmpaltitude) + "," + String(bmppressure);
    String ldrData = String(ldrValue);
    
    Serial.println(dhtData);
    Serial.println(bmpData);
    Serial.println(ldrData);*/
    delay(3000); 
}

```

This is my node red flow:

```auto
[
    {
        "id": "187b0bb9eda06271",
        "type": "tab",
        "label": "Smart Weather Monitoring System",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "d884e680153f7ef9",
        "type": "serial in",
        "z": "187b0bb9eda06271",
        "name": "Arduino Maker Uno",
        "serial": "f8031aa692189f38",
        "x": 110,
        "y": 220,
        "wires": [
            [
                "298062ff1bce572c"
            ]
        ]
    },
    {
        "id": "43b64526193c24c0",
        "type": "function",
        "z": "187b0bb9eda06271",
        "name": "Data",
        "func": "let data = (typeof msg.payload === 'string' ? msg.payload : msg.payload.toString()).split(',');\n\nlet Humidity = { payload: parseFloat(data[0]) };\nlet Temperature = { payload: parseFloat(data[1]) };\n\nlet Altitude = { payload: parseFloat(data[2]) };\nlet Pressure = { payload: parseFloat(data[3]) };\n\nlet LightLevel = { payload: parseInt(data[4]) }\n\nreturn [Humidity,Temperature,Altitude,Pressure,LightLevel];\n",
        "outputs": 3,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 220,
        "wires": [
            [
                "cb2b8668573a675c",
                "3c5c16bcb2531195"
            ],
            [
                "9521c4917a80a9a0",
                "a83551e53e6e8cf9"
            ],
            [
                "fe061fa0a7a3a932",
                "efb25ca3a309af9a"
            ]
        ]
    },
    {
        "id": "cb2b8668573a675c",
        "type": "change",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "rules": [
            {
                "t": "set",
                "p": "fields.humidity",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "tags.region",
                "pt": "msg",
                "to": "KL",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "fields.temperature",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$append(fields,tags)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 770,
        "y": 60,
        "wires": [
            [
                "f6c309b7549c21c0"
            ]
        ]
    },
    {
        "id": "3c5c16bcb2531195",
        "type": "debug",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 770,
        "y": 100,
        "wires": []
    },
    {
        "id": "f6c309b7549c21c0",
        "type": "mqtt out",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "topic": "sensor/dht11/HumidANDTemp",
        "qos": "0",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "262512044f4f61ac",
        "x": 1190,
        "y": 60,
        "wires": []
    },
    {
        "id": "1c98f6a210d9a5cb",
        "type": "mqtt in",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "topic": "sensor/dht11/HumidANDTemp",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "262512044f4f61ac",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 120,
        "y": 420,
        "wires": [
            []
        ]
    },
    {
        "id": "0327f64d58edc99b",
        "type": "influxdb out",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Humidity & Temperature",
        "measurement": "HumidTemp",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 470,
        "y": 420,
        "wires": []
    },
    {
        "id": "b1d8a857d8b58b2b",
        "type": "inject",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 770,
        "y": 420,
        "wires": [
            [
                "f317d195f17e386f"
            ]
        ]
    },
    {
        "id": "f317d195f17e386f",
        "type": "influxdb in",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Humidity & Temperature",
        "query": "select * from HumidTemp",
        "rawOutput": false,
        "precision": "",
        "retentionPolicy": "",
        "org": "organisation",
        "x": 1130,
        "y": 420,
        "wires": [
            [
                "9b67bc942370f8f1",
                "95e5f54cb3cb3f0c"
            ]
        ]
    },
    {
        "id": "9b67bc942370f8f1",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Humidity",
        "group": "73e85ccf4436dcc5",
        "order": 1,
        "width": 6,
        "height": 4,
        "gtype": "gage",
        "title": "Humidity",
        "label": "%rh",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1460,
        "y": 400,
        "wires": []
    },
    {
        "id": "95e5f54cb3cb3f0c",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Temperature",
        "group": "73e85ccf4436dcc5",
        "order": 2,
        "width": 6,
        "height": 4,
        "gtype": "gage",
        "title": "Temperature",
        "label": "Celcius",
        "format": "{{value}}°C",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1470,
        "y": 440,
        "wires": []
    },
    {
        "id": "9521c4917a80a9a0",
        "type": "change",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "rules": [
            {
                "t": "set",
                "p": "fields.altitude",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "tags.location",
                "pt": "msg",
                "to": "indoors",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "fields.pressure",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$append(fields,tags)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 790,
        "y": 180,
        "wires": [
            [
                "ea005c1d37e31c5e"
            ]
        ]
    },
    {
        "id": "ea005c1d37e31c5e",
        "type": "mqtt out",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "topic": "sensor/bmp180/AltitudeANDPressure",
        "qos": "0",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "262512044f4f61ac",
        "x": 1210,
        "y": 180,
        "wires": []
    },
    {
        "id": "a83551e53e6e8cf9",
        "type": "debug",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 790,
        "y": 220,
        "wires": []
    },
    {
        "id": "fe061fa0a7a3a932",
        "type": "change",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "rules": [
            {
                "t": "set",
                "p": "tags.status",
                "pt": "msg",
                "to": "Good",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "fields.lightLevel",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$append(fields,tags)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 300,
        "wires": [
            [
                "8e1747b1f9b24bef"
            ]
        ]
    },
    {
        "id": "8e1747b1f9b24bef",
        "type": "mqtt out",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "topic": "sensor/ldr/luminousity",
        "qos": "0",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "262512044f4f61ac",
        "x": 1150,
        "y": 300,
        "wires": []
    },
    {
        "id": "efb25ca3a309af9a",
        "type": "debug",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 730,
        "y": 340,
        "wires": []
    },
    {
        "id": "de3c5ea50f1fea9e",
        "type": "mqtt in",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "topic": "sensor/bmp180/AltitudeANDPressure",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "262512044f4f61ac",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 150,
        "y": 480,
        "wires": [
            []
        ]
    },
    {
        "id": "7e7eb7b086be8aea",
        "type": "mqtt in",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "topic": "sensor/ldr/luminousity",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "262512044f4f61ac",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 90,
        "y": 540,
        "wires": [
            []
        ]
    },
    {
        "id": "077cdcf90a88e107",
        "type": "influxdb out",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Altitude & Atmospheric Pressure",
        "measurement": "AltiPres",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 490,
        "y": 480,
        "wires": []
    },
    {
        "id": "107ef1e8df34e717",
        "type": "influxdb out",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Luminousity",
        "measurement": "luminousity",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 430,
        "y": 540,
        "wires": []
    },
    {
        "id": "19c0a1e84ad3b9f3",
        "type": "inject",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 800,
        "y": 500,
        "wires": [
            [
                "a66fd1da4f53e1b6"
            ]
        ]
    },
    {
        "id": "a66fd1da4f53e1b6",
        "type": "influxdb in",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Altitude & Atmospheric Pressure",
        "query": "select * from AltiPres",
        "rawOutput": false,
        "precision": "",
        "retentionPolicy": "",
        "org": "organisation",
        "x": 1150,
        "y": 500,
        "wires": [
            [
                "8a575bbfbfd7d159",
                "655c9885536327a3"
            ]
        ]
    },
    {
        "id": "8a575bbfbfd7d159",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Altitude",
        "group": "73e85ccf4436dcc5",
        "order": 3,
        "width": 6,
        "height": 4,
        "gtype": "wave",
        "title": "Altitude",
        "label": "meters",
        "format": "{{value}}",
        "min": "500",
        "max": "1200",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1460,
        "y": 480,
        "wires": []
    },
    {
        "id": "655c9885536327a3",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Atmospheric Pressure",
        "group": "73e85ccf4436dcc5",
        "order": 4,
        "width": 6,
        "height": 4,
        "gtype": "wave",
        "title": "Atmospheric Pressure",
        "label": "Pa",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1500,
        "y": 520,
        "wires": []
    },
    {
        "id": "a64c47964ffb2163",
        "type": "inject",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 730,
        "y": 580,
        "wires": [
            [
                "d7cda6c27c50de2c"
            ]
        ]
    },
    {
        "id": "d7cda6c27c50de2c",
        "type": "influxdb in",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Luminousity",
        "query": "select * from luminousity",
        "rawOutput": false,
        "precision": "",
        "retentionPolicy": "",
        "org": "organisation",
        "x": 1090,
        "y": 580,
        "wires": [
            [
                "4ea5d6334f76f042"
            ]
        ]
    },
    {
        "id": "4ea5d6334f76f042",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "group": "73e85ccf4436dcc5",
        "order": 6,
        "width": 6,
        "height": 4,
        "gtype": "donut",
        "title": "Luminousity",
        "label": "lx",
        "format": "{{value}}",
        "min": 0,
        "max": "1000",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1470,
        "y": 580,
        "wires": []
    },
    {
        "id": "298062ff1bce572c",
        "type": "json",
        "z": "187b0bb9eda06271",
        "name": "JSON to String/Object",
        "property": "payload",
        "action": "obj",
        "pretty": false,
        "x": 320,
        "y": 220,
        "wires": [
            [
                "43b64526193c24c0"
            ]
        ]
    },
    {
        "id": "326407df7896fa4f",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 5,
        "width": 3,
        "height": 1
    },
    {
        "id": "ab4e8ece052ee7df",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 7,
        "width": 3,
        "height": 1
    },
    {
        "id": "7118a611b5e78c39",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 8,
        "width": 3,
        "height": 1
    },
    {
        "id": "5bd96b67d74d8666",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 9,
        "width": 3,
        "height": 1
    },
    {
        "id": "25d2d5596d5348c2",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 10,
        "width": 3,
        "height": 1
    },
    {
        "id": "a8ed22c0c0383de1",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 11,
        "width": 3,
        "height": 1
    },
    {
        "id": "76569fc26d088856",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 5,
        "width": 3,
        "height": 1
    },
    {
        "id": "958aabaf0ed9df4d",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 7,
        "width": 3,
        "height": 1
    },
    {
        "id": "df3831187c0a8804",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 8,
        "width": 3,
        "height": 1
    },
    {
        "id": "10eae83ae4ef8890",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 9,
        "width": 3,
        "height": 1
    },
    {
        "id": "86724259dec5773c",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 10,
        "width": 3,
        "height": 1
    },
    {
        "id": "56da73672c15615f",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 11,
        "width": 3,
        "height": 1
    },
    {
        "id": "552fe493d28b50b5",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 12,
        "width": 3,
        "height": 1
    },
    {
        "id": "3572c60d2d1c408d",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 13,
        "width": 3,
        "height": 1
    },
    {
        "id": "f8031aa692189f38",
        "type": "serial-port",
        "serialport": "COM7",
        "serialbaud": "9600",
        "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": "262512044f4f61ac",
        "type": "mqtt-broker",
        "name": "",
        "broker": "broker.emqx.io",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "921764b5c0c308e9",
        "type": "influxdb",
        "hostname": "127.0.0.1",
        "port": "8086",
        "protocol": "http",
        "database": "SWMS",
        "name": "SWMS",
        "usetls": false,
        "tls": "",
        "influxdbVersion": "1.x",
        "url": "http://localhost:8086",
        "rejectUnauthorized": true
    },
    {
        "id": "73e85ccf4436dcc5",
        "type": "ui_group",
        "name": "Assignment",
        "tab": "c9feb0502bf4a2fb",
        "order": 1,
        "disp": true,
        "width": 12,
        "collapse": false
    },
    {
        "id": "dcee4deb41a081e3",
        "type": "ui_group",
        "name": "Assignment",
        "tab": "13ff37f4e3992732",
        "order": 1,
        "disp": true,
        "width": 12,
        "collapse": false
    },
    {
        "id": "c9feb0502bf4a2fb",
        "type": "ui_tab",
        "name": "SWMS",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    },
    {
        "id": "13ff37f4e3992732",
        "type": "ui_tab",
        "name": "Smart Weather Monitoring System",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 August 2023 20:03 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/8 "2023-08-22T20:03:49Z")

</div>

If your JSON is valid, there is no need for splitting and parsing. Just pass the JSON through a JSON node. That is all.

There are lots of online JSON checkers to verify.

---

<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:** [22 August 2023 21:12 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/9 "2023-08-22T21:12:05Z")

</div>

> [@mstk\_olsen](#):
>
> ```auto
> unsigned int ldrValue;
> ldrValue = analogRead(A0);
> StaticJsonDocument<200> jsonDoc;
> jsonDoc["dht"]["humidity"] = Humidity;
> jsonDoc["dht"]["temperature"] = Temperature;
> jsonDoc["bmp"]["altitude"] = bmpaltitude;
> jsonDoc["bmp"]["pressure"] = bmppressure;
> jsonDoc["ldr"]["lightLevel"] = ldrValue;
> serializeJson(jsonDoc, Serial);
>   
> /*String dhtData = String(Humidity) + "," + String(Temperature);
> String bmpData = String(bmpaltitude) + "," + String(bmppressure);
> String ldrData = String(ldrValue);
>     
> Serial.println(dhtData);
> Serial.println(bmpData);
> Serial.println(ldrData);*/
> delay(3000); 
> }
> 
> ```

So where in your code are you actually sending the data? I see a lot of code commented out....

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [22 August 2023 21:22 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/10 "2023-08-22T21:22:21Z")

</div>

I don't think it's MQTT it looks like it's going serial.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [22 August 2023 21:22 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/11 "2023-08-22T21:22:28Z")

</div>

> [@mstk\_olsen](#):
>
> Ok so now i formatted the data into JSON but in my node red is not showing up anything.

So that we can see your actual data, put a debug node on the output of your serial node. copy the value and paste it here.

ps I am not very familiar with Arduino code but it looks like you might need to add this after your serializeJson call  
`jsondoc.printTo(Serial)`

---

<div class="post-metadata">

**Author:** ![mstk\_olsen](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mstk\_olsen](https://discourse.nodered.org/u/mstk_olsen)\
**Post date:** [23 August 2023 02:50 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/12 "2023-08-23T02:50:48Z")

</div>

That commented lines i just want to see if the JSON format is good to go, then i can straight away erase these lines.

---

<div class="post-metadata">

**Author:** ![mstk\_olsen](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mstk\_olsen](https://discourse.nodered.org/u/mstk_olsen)\
**Post date:** [23 August 2023 03:08 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/13 "2023-08-23T03:08:25Z")

</div>

It seems that my output on the first line is stacked together, I checked the output with a JSON editor online and it gives an error about I not quite understand what it means. Mind if you can point it out where my code should be changed or added?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac3ab8949f8769797f95fdbdc6cceb4e61906fef.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c99205a591b8d4c0e5df4125bbc41adc306a95c0.png)

```auto
#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include <DHT.h>

#define DHTPIN 7 
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);  

Adafruit_BMP085 bmp;

void setup() {
  Serial.begin(9600); 
  dht.begin();

  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1);
  }
}
  
  
void loop() {
   float Temperature = dht.readTemperature();   
   float Humidity = dht.readHumidity();  

    if (isnan(Temperature) || isnan(Humidity)){
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
    float bmppressure = bmp.readPressure()/100.00;
    float bmpaltitude = bmp.readAltitude();

    unsigned int ldrValue;
    ldrValue = analogRead(A0);
    
    StaticJsonDocument<200> jsonDoc;
    jsonDoc["DHT11"]["Humidity"] = Humidity;
    jsonDoc["DHT11"]["Temperature"] = Temperature;
    jsonDoc["DHT11"]["TemperatureUnits"] = "Celcius"; 
    jsonDoc["BMP180"]["Altitude"] = bmpaltitude;
    jsonDoc["BMP180"]["Pressure"] = bmppressure;
    jsonDoc["LDR"]["LightLevel"] = ldrValue;
    serializeJson(jsonDoc, Serial);
    serializeJsonPretty(jsonDoc,Serial);
    delay(3000); 
}

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2023 06:00 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/14 "2023-08-23T06:00:05Z")

</div>

Since you have a 3s delay between transmissions, you should set the serial config in node-red to release data after a delay. 500ms or 1000ms should easily be enough to split the strings correctly and only output 1 valid json string at a time.

Ps. Please don't ever post pictures of text. I have no ability to copy and paste and check it the json is valid for myself. Instead, always post code and logs in a ````` code fence `````

---

<div class="post-metadata">

**Author:** ![mstk\_olsen](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mstk\_olsen](https://discourse.nodered.org/u/mstk_olsen)\
**Post date:** [23 August 2023 08:41 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/15 "2023-08-23T08:41:12Z")

</div>

How do I do that? I only found the `Default response timeout ___ms` in the serial port node. I also getting errors in node red:  
Ps. Thanks for the tip, Sorry for making everything messy.

```auto
[error] [json:JSON to String/Object] Unexpected end of JSON input
[error] [json:JSON to String/Object] Unexpected end of JSON input
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 9
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 14
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 17
[error] [json:JSON to String/Object] Unexpected token } in JSON at position 2
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 10
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 14
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 14
[error] [json:JSON to String/Object] Unexpected token } in JSON at position 2
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 7
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 
[error] [json:JSON to String/Object] Unexpected token } in JSON at position 2

```

While the JSON output in Arduino im not sure whether is there anything is wrong after I input these into my `void loop()`:

```auto
    StaticJsonDocument<200> jsonDoc;
    JsonObject dhtObject = jsonDoc.createNestedObject("DHT11");
    dhtObject["Humidity"] = Humidity;
    dhtObject["Temperature"] = Temperature;

    JsonObject bmpObject = jsonDoc.createNestedObject("BMP180");
    bmpObject["Altitude"] = bmpaltitude;
    bmpObject["Pressure"] = bmppressure;

    JsonObject ldrObject = jsonDoc.createNestedObject("LDR");
    ldrObject["LightLevel"] = ldrValue;
    Serial.println("\n");

    serializeJsonPretty(jsonDoc,Serial);

```

And my Arduino output will be like this:

```auto

{
  "DHT11": {
    "Humidity": 83,
    "Temperature": 26.3
  },
  "BMP180": {
    "Altitude": 99.63486,
    "Pressure": 1001.33
  },
  "LDR": {
    "LightLevel": 836
  }
}

{
  "DHT11": {
    "Humidity": 83,
    "Temperature": 26.3
  },
  "BMP180": {
    "Altitude": 100.1395,
    "Pressure": 1001.3
  },
  "LDR": {
    "LightLevel": 835
  }
}

{
  "DHT11": {
    "Humidity": 83,
    "Temperature": 26.3
  },
  "BMP180": {
    "Altitude": 99.21474,
    "Pressure": 1001.3
  },
  "LDR": {
    "LightLevel": 835
  }
}

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 August 2023 08:51 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/16 "2023-08-23T08:51:43Z")

</div>

> [@mstk\_olsen](#):
>
> And my Arduino output will be like this:

"Will be" is ambiguous. I assume you mean this **is** what you are getting.

So with default response timeout 10000ms you are getting three copies of your data stacked up, which is invalid Json

If you set the default response timeout to 1000 do you get one copy and only one copy of the data in Node-red?

Please show us the data arriving in Node-red along with any errors.

---

<div class="post-metadata">

**Author:** ![mstk\_olsen](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mstk\_olsen](https://discourse.nodered.org/u/mstk_olsen)\
**Post date:** [23 August 2023 09:03 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/17 "2023-08-23T09:03:18Z")

</div>

> [@jbudd](#):
>
> "Will be" is ambiguous. I assume you mean this **is** what you are getting.

Yeah i'm constantly getting this output in Arduino.

> [@jbudd](#):
>
> If you set the default response timeout to 1000 do you get one copy and only one copy of the data in Node-red?

I've set the default response timeout to 1000ms and still getting the same number of errors in node-red

> [@mstk\_olsen](#):
>
> ```auto
> [error] [json:JSON to String/Object] Unexpected end of JSON input
> [error] [json:JSON to String/Object] Unexpected end of JSON input
> [error] [json:JSON to String/Object] Unexpected token : in JSON at position 9
> [error] [json:JSON to String/Object] Unexpected token : in JSON at position 14
> [error] [json:JSON to String/Object] Unexpected token : in JSON at position 17
> [error] [json:JSON to String/Object] Unexpected token } in JSON at position 2
> [error] [json:JSON to String/Object] Unexpected token : in JSON at position 10
> [error] [json:JSON to String/Object] Unexpected token : in JSON at position 14
> [error] [json:JSON to String/Object] Unexpected token : in JSON at position 14
> [error] [json:JSON to String/Object] Unexpected token } in JSON at position 2
> [error] [json:JSON to String/Object] Unexpected token : in JSON at position 7
> [error] [json:JSON to String/Object] Unexpected token : in JSON at position 16
> [error] [json:JSON to String/Object] Unexpected token } in JSON at position 2
> 
> ```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2023 09:05 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/18 "2023-08-23T09:05:58Z")

</div>

Show us a screenshot of your serial config in Node-red

---

<div class="post-metadata">

**Author:** ![mstk\_olsen](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mstk\_olsen](https://discourse.nodered.org/u/mstk_olsen)\
**Post date:** [23 August 2023 09:10 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/19 "2023-08-23T09:10:02Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e82cdc42cba907430ee2e21306a4729aaeed6e25.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2023 09:18 UTC](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719/20 "2023-08-23T09:18:22Z")

</div>

So as I said, because you only send data every 3 seconds, try setting "Split input" option to "after silence" 500ms. Sorry, I don't remember the exact wording of the option.

[Next page](https://discourse.nodered.org/t/sensor-data-turned-into-null-value-after-function-node/80719.md?page=2)
