Array send to InfluxDB

I'm working with an API that is giving me a nice array in one shot with all the data I need. I want to use the influxdb batch node to write the data into influxdb

I get back a variable number of objects, roughly 100 of them. I need to pull out the id, numeric values, and the timestamps.

payload[1].serial
payload[1].id
payload[1].measurements.numeric["value.EXAMPLE_PV"].value
payload[1].measurements.numeric["value.EXAMPLE_PV"].time
payload[1].geo.latitude
payload[2].id
payload[2].serial
payload[2].measurements.numeric["value.EXAMPLE_SP"].value
payload[2].measurements.numeric["value.EXAMPLE_SP"].time
payload[2].geo.latitude
... etc

I do not know how many objects there will be, so I need to cycle through the array.

influxdb batch node requires the following format:

msg.payload = [
    {
        measurement: "weather_sensor", 
        fields: {
            temp: 5.5,
            light: 678,
            humidity: 51
        },
        tags: {
            location: "garden"
        },
        timestamp: new Date()
    },
    {
        measurement: "alarm_sensor",  
        fields: {
            proximity: 999,
            temp: 19.5
        },
        tags: {
            location: "home"
        },
        timestamp: new Date()
    }
];
return msg;

Is there some example code I can reference to parse the objects and place the data I want in the correct parts of the message?

I want the "measurement" to be "geo", "measurements" etc
I want the "fields" to be "EXAMPLE_PV", "EXAMPLE_SP" etc
I want the "tags" to be the value of payload[1].serial, and payload[1].id
I want the "timestamp" to be the value of payload[1].measurements.numeric["value.EXAMPLE_PV"].time

TIA!

I used a slightly different API call to just get an array from a single machine so I could get some data going.

the following code is a decent start and puts the data where I need it in influxdb

var id = 133899;
var name = "fakename";
var continent = "Europe";
var valve_angle = msg.payload.numeric["value.Valve_Angle"].value;
var valve_angle_timestamp = new Date(msg.payload.numeric["value.Valve_Angle"].time);
var mcu_status = msg.payload.numeric["value.MCU_Status"].value;
var mcu_status_timestamp = new Date(msg.payload.numeric["value.MCU_Status"].time);
var Fan_DutyCycle = msg.payload.numeric["value.Fan_DutyCycle"].value;
var Fan_DutyCycle_timestamp = new Date(msg.payload.numeric["value.Fan_DutyCycle"].time);


msg.payload = [
    {
        measurement: "signals",
        fields: {
            valve_angle_value: valve_angle,
        },
        tags: {
            machine_id: id,
            machine_name: name,
            machine_continent: continent
        },
        timestamp: valve_angle_timestamp
    },
    {
        measurement: "signals",
        fields: {
            fan_dutycycle_value: Fan_DutyCycle,
        },
        tags: {
            machine_id: id,
            machine_name: name,
            machine_continent: continent
        },
        timestamp: Fan_DutyCycle_timestamp
    },
    {
        measurement: "signals",
        fields: {
            mcu_status_value: mcu_status,
        },
        tags: {
            machine_id: id,
            machine_name: name,
            machine_continent: continent
        },
        timestamp: mcu_status_timestamp
    }
];
return msg;

The part I'm missing now is how to cycle through the Array in the bigger API call and process each object this way. Do I use a split node and break it up into individual messages and stream them through the function?

You could use a for loop and iterate through the array.

// using the length property of the the array in msg.payload, makes the loop run once for each entry in the array
for (let i = 0; i < msg.payload.length; i++) {
    // your code here, something like msg.payload[i] = "whatever you want here";
};

Have a look at Array map(). This iterates through an array, calling a function for each entry, and builds a new array where each element in the new array is a transformed version of the original. The examples in the link should help you see how to use it.

When you do need to iterate an array manually for some reason then a good way, which saves messing about with incrementing indexes and so on, is to use Array forEach().

THANK YOU! Definitely going to play around with this.

I've used a split node to break up an array of arrays into separate messages. That way, it doesn't matter if the number of arrays is variable and you can generate code to manipulate a single array. Just split using a fixed length of 1.

This is what I ended up doing. It worked well with the code I posted above. I suppose it is also the 'node red way'

1 Like