Node-Red -> InfluxDB error

Hi
I am trying to store my data in influxdb.
I am using Node-RED between TTN and the database, but i am not sure about how to set it up.
I have currently got it to connect, and receives packages from TTN, but it complains about the database.

It say "TypeError: points.forEach is not a function"

But i don’t know what this means.
I should have created the database called ttndb in influx.

Maybe you should tell us how your payload looks like.
From the error message I would expect one field being an iterable, which won’t work…

I guess it’s something like this

{"app_id":"dtu-iot-platform","dev_id":"1st","hardware_serial":"0004A30B0021950B","port":1,"counter":1283,"payload_raw":[1,103,1,15,2,104,75],"payload_fields":{"relative_humidity_2":37.5,"temperature_1":27.1},"metadata":{"time":"2018-06-25T12:38:17.33832273Z","frequency":868.5,"modulation":"LORA","data_rate":"SF7BW125","airtime":56576000,"coding_rate":"4/5","gateways":[{"gtw_id":"eui-fcc23dfffe0f4f8f","timestamp":125648083,"time":"2018-06-25T12:38:17.318121Z","channel":2,"rssi":-111,"snr":3.8,"rf_chain":1},{"gtw_id":"dtu-gateway","gtw_trusted":true,"timestamp":750903947,"time":"2018-06-25T12:42:08Z","channel":2,"rssi":-45,"snr":7.75,"rf_chain":1,"latitude":55.78286,"longitude":12.517916,"location_source":"registry"}]},"payload":{"relative_humidity_2":37.5,"temperature_1":27.1},"_msgid":"1da9d683.172709"}

I want to store at least the dev_id and the payload. but more is also apochiated

Payload looks OK, but there are 2 things I am seeing…

  1. I don’t think you did specify a measurement in the InfluxDB node (otherwise it woudl appear in the label) and you don’t have a measurement element in your payload. You need to specify the measurement somewhere. (but that should not cause the error you have)
  2. I see overall blue dots in your screenshot, which means you have not deployed, or what you show is not what you deployed…

Other than that it should work… :roll_eyes:

… Or at least you should have the payload in InfluxDB.
Adding a label (dev_id) requires an additional step, but you first need to have the basic stuff working.

I got it working by adding a measurement name. Even though i don’t know what they mean by this measurement.

I used the name ttn.
In grafana i can find the measurement ttn, and the temp and humidity values, but i dont know how to only show the values form a specific node.

Can i see somewhere what is stored in the database?

If you compare to a ‘traditional database’, a measurement is the equivalent of a table.
So you need to tell in which measurement (table) it needs to store the data.

In InfuxDB, from the command line, you can do:

use ttndb
select * from ttn

to see the data

I can’t realy get my CLI for influx working.
I am running it in raspberian Debian, and the command influx does not work. Influxd does, but complains about a port already in use (8088) and then exits.
So i just run influx as a service.
I have access to the admin interface. i run “select * from ttn” and can see the data saved. and it is only time, Rel Hum, and temperature.

So can i just add the dev_id to the payload with a function in Node-RED? Or what is the process of sorting by dev_id?

I strongly encourage you to read the InfluxDB concepts to understand how InfuxDB works, in particular the difference between fields and tags.

In a couple of words, you don’t want to have your device_id as field, it should be a tag.

To make that happening you need to insert a function node between theTTN and the Influx ones – E.g.:
21

In this node, you can inject the device_id as a tag, e.g.:

return { payload: 
    [
        // Measure
        msg.payload,
        // Tags
        {
            dev_id: msg.dev_id
        }   
    ]
};
2 Likes

It is very interesting.
I am reading the concepts. Thank you :slight_smile:

In your example, how can i see that a value is a tag or a field?
as i see it, this should be equivelent

return { payload: 
[
    // Measure
    msg.payload,
    // Tags
    dev_id: msg.dev_id
]

};
But maybe it is not?
Maybe that extra bracket makes it a tag value?

No it is not equivalent, it is just how the InfluxNode expect data – from the node help:

If msg.payload is an array containing two objects, the first object will be written as the set of named fields, the second is the set of named tags.

So you need to return an array with 2 hashes:

  • msg.payload is the hash of fields
  • you need a second hash (or object) for the tags (even if you only have one), so you need the braces.

Here is an actual Function node I use, where I add a field (message counter) and have 2 tags (node id and data rate):

// Add metadata to the payload
msg.payload['counter'] = msg.counter;
return { payload: 
    [
        // Measure
        msg.payload,
        // Tags
        {
            dev_id: msg.dev_id,
            data_rate: msg.metadata.data_rate
        }   
    ]
};

There are other ways of writing this, but bottom line, you need to return an array of 2 objects

The following does the same, maybe more readable:

var measures = msg.payload;
measures['counter'] = msg.counter;

var tags = {
            dev_id: msg.dev_id,
            data_rate: msg.metadata.data_rate
        } ;

return { payload: 
    [
        measures,
        tags
    ]
};

Thank you very much, now i understand it much better, and for now, i got what i was looking for, and it works fine with Grafana at the moment.

@AmedeeThank you for sharing and caring …

1 Like

I do get the same error and my payload looks like:

“{“time” : “2018-07-29 17:56:08”, “model” : “Fine Offset Electronics WH1080/WH3080 Weather Station”, “msg_type” : 0, “id” : 197, “temperature_C” : 28.000, “humidity” : 40, “direction_str” : “N”, “direction_deg” : “0”, “speed” : 1.224, “gust” : 2.448, “rain” : 51.900, “battery” : “OK”}”