NodeRED and InfluxDB - One Measurement more fields

Hi all, I'm new to the Forum and also to NodeRED. I hope you can help me:
I created a container with NodeRED Ver. 1.3.4 and I'm doing various experiments.
One of them is to extrapolate the payload of this request from this message https://api.waqi.info/feed/A144943/?token=8f3999b079d169a52edadec8124708c605e63e90

I managed to identify the fields I need and they are:

  • payload.data.aqi
  • payload.data.iaqi.pm10.v
  • payload.data.iaqi.pm25.v

I managed to write these values in InfluxDB but each with a different "measurement".
I would like these values to be written under one "measurement" with 3 fields (Time, AQI, PM10 and PM25).

To write to InfluxDB I use "node-red-contrib-influxdb".

I've read a lot on the net (discussion and official guides...) and I've managed to do a lot of things but I'm missing the last piece that I couldn't understand... could you help me?

Thank you all.

Just send the data to the same influxdb out node. create on entry and assign your assignment there.

Hi @jodelkoenig, could you show me how to do it?
Which nodes should I use?
What code should I write to isolate these fields and tell InfluxDB to create a "Measurement" with 4 fields (Time, AQI, PM10 and PM25)?

Thanks!

Bildschirm­foto 2022-12-21 um 16.51.53

This I send to the one influxdb out node via different function nodes:

msg.payload = [{
    light: state
},
{
    room: flow.get("name"),
    category:"Beleuchtung",
    device:"Decke"
}];
return msg;

light could be Time, AQI, PM10 and PM25
room, category, device could be any tag you like

Or, if you don't have any tags then you just need the payload to be a simple object containing the fields. Something like this in a function node

msg.payload = {
  PM10:  msg.payload.data.iaqi.pm10.v,
  PM25:  msg.payload.data.iaqi.pm25.v,
  etc...
}
return msg

Or you can do the same in a Change node.

I try to write what I understand.
I have:

  • an "INJECT" node that acts as a trigger;
  • an "HTTP REQUEST" node to capture the msg (return: a parsed JSON object)
  • an "FUNCTION" node to tell InfluxDB how to save the data;
msg.payload = [{
    aqi: state,
    pm10: state,
    pm25: state
},
{
    room: flow.get("name"),
    category:"Beleuchtung",
    device:"Decke"
}];
return msg;
  • finally an "INFLUXDB OUT" node to save the data in a certain DB with a certain "Measurement"

Is the flow correct?
Is the code in the "FUNCTION" node correct?

Thank you very much!

You should skip:

,
{
    room: flow.get("name"),
    category:"Beleuchtung",
    device:"Decke"
}

or adopt correspondingly if you would like to use tags. You might not need it, then just delete it from your code.

EDIT:

Or ... just take Colin's example :smile:

I didn't say previously, but you don't need to provide a time to influx, if the data is for the current time. Influx will add it automatically.

In addition, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

I tried to put the code you suggested in the "FUNCTION" node:

msg.payload = {
  PM10:  msg.payload.data.iaqi.pm10.v,
  PM25:  msg.payload.data.iaqi.pm25.v,
  AQI:   payload.data.aqi
}
return msg

...but i have this error:

21/12/2022, 20:32:16node: Formatting
function : (error)
"ReferenceError: payload is not defined (line 4, col 10)"

Check line 4 and compare it to line 3 and 2. I assume you want to refer to msg.payload.data.aqi, right?

Thanks, now work!!
I added msg. before payload.data.aqi
It's very strange because if I copy the path from the original message payload printed by NodeRED (I followed the official guide...) I have this: payload.data.aqi, without msg. front!

It gives you the path within the message.

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