Send JSON data to influxdb from Node-red

Can someone help me to send JSON data to Influxdb from node-red. You can find my output data in the attached image and the influx node I have. I am using influx 2.0 version. My data will change every 10secs so, I can't hard code anything here. Thank you :slight_smile:

Start by removing the json node.

Influx requires a measument, fields, values and timestamp - and unless you are not using them for aggregations, no strings.

I am using the influx batch node myself, which accepts arrays of timeseries measurements.

Example how I insert data (note the tag to distinguish between my indoor/outdoor measurements):

[
    {
        "measurement": "netatmo",
        "fields":
        {
            "time_utc": 1676466703,
            "Temperature": 18.9,
            "CO2": 609,
            "Humidity": 57,
            "Noise": 39,
            "Pressure": 1022.2,
            "AbsolutePressure": 1021.8,
            "min_temp": 18.8,
            "max_temp": 20.7,
            "date_max_temp": 1676418222,
            "date_min_temp": 1676449778,
            "temp_trend": "stable",
            "pressure_trend": "down"
        },
        "tags":
        {
            "location": "indoor"
        },
        "timestamp": "2023-02-15T13:17:51.559Z"
    },
    {
        "measurement": "netatmo",
        "fields":
        {
            "time_utc": 1676466674,
            "Temperature": 11.7,
            "Humidity": 71,
            "min_temp": 3.3,
            "max_temp": 11.7,
            "date_max_temp": 1676466674,
            "date_min_temp": 1676440681,
            "temp_trend": "up",
            "battery": 87,
            "battery_vp": 5774
        },
        "tags":
        {
            "location": "outdoor"
        },
        "timestamp": "2023-02-15T13:17:51.559Z"
    }
]

So you will need to redefine your message/arrays.

Thank you, but how can I generalise the code so that values are taken from ESP32? It would be a real-time data coming from ESP32.

Show us what the data from the esp looks like?

ESp32_Output
This is what my esp32 output looks like, I have two device_IDs, and every 5secs I get data from these two devices which come as a string.

As you can see in the first image, I just split the data and put it in JSON so that I can send it to AWS, but I need to do it with influxdb.

Do you have control of the esp code? If so then I suggest you change it to return a JSON string that you can feed into a JSON node to give you a javascript object.

In the debug it shows a whole array of data, are you combining multiple readings together? If so then don't bother, just send them one at a time to influxdb, with device id as a tag and temperature, humidity and wind speed as fields.

// Parse the incoming message data
var data = msg.payload;
var parts = data.split(';');
var geodata = parts[0];
var device_id = parts[1];
var temperature = parts[2];
var humidity = parts[3];
var wind_speed = parts[4];

// Build the InfluxDB data point
var timestamp = new Date().toISOString();
var measurement = "windanalysis";
var fields = {
    "temperature": parseFloat(temperature.split(':')[1]),
    "humidity": parseFloat(humidity.split(':')[1]),
    "wind_speed": parseFloat(wind_speed.split(':')[1])
};
var tags = {
    "geodata": geodata.split(':')[1],
    "device_id": device_id.split(':')[1]
};

// Send the data point to InfluxDB
msg.payload = [{ measurement: measurement, fields: fields, tags: tags, timestamp: timestamp }];
return msg;

I did change the code like this, but even then I am not able to see anything in Influxdb, none of the fields getting generated in Influxdb

Assuming you are sending to the Influx Out node then look in the help text and you will see that is not a valid structure for the data.
You need the measurement in msg.measurement, and in msg.payload you need

[
  {
    field1: value,
    field2: value
  },
  {
    tag1: value,
    tag2: value
  }
]

You don't need to provide a timestamp, influx will add the current time automatically

I have used the same syntax, I am just calling the measurement, field, and tags from the above piece of code. I tried doing it the same way as you mentioned and still, it doesn't give any output in influxdb.

The code you showed does not give anything like the structure I posted. Feed your output into a debug node and show us what it looks like.

[Edit] I mean show us what you are feeding to the influx node.

// Parse the incoming message data
var data = msg.payload;
var parts = data.split(';');
var geodata = parts[0];
var device_id = parts[1];
var temperature = parts[2];
var humidity = parts[3];
var wind_speed = parts[4];

// Send the data point to InfluxDB
[{ 
    measurement: "windanalysis",
    fields:
    {
        "temperature": parseFloat(temperature.split(':')[1]),
        "humidity": parseFloat(humidity.split(':')[1]),
        "wind_speed": parseFloat(wind_speed.split(':')[1])
    }, 
    tags:
    {
        "geodata": geodata.split(':')[1],
        "device_id": device_id.split(':')[1]
    }
}];
return msg;

This is the code right now, I believe this is according to the syntax.

Debug looks like this after this function node

geodata:15.34-34.67;device_ID:3;Temperature:30 degC;Humidity:33 RH;WindSpeed:333 m/s

Does that look like the structure I posted?

Could you please how can I change this in my code? I am a bit new to node-red and I am not understanding how can I write this. Sorry for the simple questions.

Something like

msg.measurement = "windanalysis"
msg.payload = [
  {
    temperature: parseFloat(temperature.split(':')[1]),
    humidity: parseFloat(humidity.split(':')[1]),
    etc
  },
  {
    geodata: ...
    ...
  }
]

Check it looks right by feeding it into a debug node before you try sending it to influx. Set the debug node to Show Full Message so that you can see msg.measurement.
If it still isn't right then show us the new function and show us what you see in the debug node.

Yeah, I misunderstood what you told me in the beginning. I had confusion batch mode and normal ones. I followed it again it worked perfectly fine. Thank you for the help :slight_smile:

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