# Node-Red -\> InfluxDB error

**URL:** <https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281>\
**Category:** Uncategorized\
**Created:** [June 25, 2018, 8:58am UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281 "2018-06-25T08:58:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Struck](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@Struck](https://www.thethingsnetwork.org/forum/u/Struck)\
**Post date:** [June 25, 2018, 8:58am UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/1 "2018-06-25T08:58:41Z")

</div>

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.  
 ![](https://www.thethingsnetwork.org/forum/uploads/default/original/2X/1/1bfad12b99dfb9cd7b7dc4312e5d2ed13ea69c87.png)  
**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.

---

<div class="post-metadata">

**Author:** ![Amedee](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/amedee/32/39610_2.png) [@Amedee](https://www.thethingsnetwork.org/forum/u/Amedee)\
**Post date:** [June 25, 2018, 12:08pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/2 "2018-06-25T12:08:46Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Struck](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@Struck](https://www.thethingsnetwork.org/forum/u/Struck)\
**Post date:** [June 25, 2018, 12:40pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/3 "2018-06-25T12:40:24Z")

</div>

> [@Amedee](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![Amedee](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/amedee/32/39610_2.png) [@Amedee](https://www.thethingsnetwork.org/forum/u/Amedee)\
**Post date:** [June 25, 2018, 1:20pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/4 "2018-06-25T13:20:27Z")

</div>

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… 🙄

… 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.

---

<div class="post-metadata">

**Author:** ![Struck](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@Struck](https://www.thethingsnetwork.org/forum/u/Struck)\
**Post date:** [June 25, 2018, 2:12pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/5 "2018-06-25T14:12:22Z")

</div>

I got it working by adding a measurement name. Even though i don’t know what they mean by this measurement.  
 ![](https://www.thethingsnetwork.org/forum/uploads/default/original/2X/b/bd0fb96c8d2a3309a9c5574d1130e008b74e8702.png)  
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?

---

<div class="post-metadata">

**Author:** ![Amedee](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/amedee/32/39610_2.png) [@Amedee](https://www.thethingsnetwork.org/forum/u/Amedee)\
**Post date:** [June 25, 2018, 3:16pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/6 "2018-06-25T15:16:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Struck](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@Struck](https://www.thethingsnetwork.org/forum/u/Struck)\
**Post date:** [June 25, 2018, 3:26pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/7 "2018-06-25T15:26:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Amedee](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/amedee/32/39610_2.png) [@Amedee](https://www.thethingsnetwork.org/forum/u/Amedee)\
**Post date:** [June 25, 2018, 4:02pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/8 "2018-06-25T16:02:11Z")

</div>

I strongly encourage you to read the [InfluxDB concepts](https://docs.influxdata.com/influxdb/v1.5/concepts/key_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](https://www.thethingsnetwork.org/forum/uploads/default/original/2X/8/8b6917cea28503d46935b5386e83edd934fabc06.png)

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
        }   
    ]
};
```

---

<div class="post-metadata">

**Author:** ![Struck](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@Struck](https://www.thethingsnetwork.org/forum/u/Struck)\
**Post date:** [June 25, 2018, 4:56pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/9 "2018-06-25T16:56:07Z")

</div>

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

> [@Amedee](#):
>
> ```
> return { payload: 
> [
> // Measure
> msg.payload,
> // Tags
> {
> dev_id: msg.dev_id
> }   
> ]
> };
> 
> ```

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?

---

<div class="post-metadata">

**Author:** ![Amedee](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/amedee/32/39610_2.png) [@Amedee](https://www.thethingsnetwork.org/forum/u/Amedee)\
**Post date:** [June 25, 2018, 5:05pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/10 "2018-06-25T17:05:05Z")

</div>

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
    ]
};

```

---

<div class="post-metadata">

**Author:** ![Struck](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@Struck](https://www.thethingsnetwork.org/forum/u/Struck)\
**Post date:** [June 25, 2018, 5:10pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/11 "2018-06-25T17:10:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tvloon](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/tvloon/32/20484_2.png) [@tvloon](https://www.thethingsnetwork.org/forum/u/tvloon)\
**Post date:** [July 2, 2018, 5:41pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/12 "2018-07-02T17:41:09Z")

</div>

@Amedee – _Thank you for sharing and caring …_

---

<div class="post-metadata">

**Author:** ![IOT-Jogi](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/i/eb8c5e/32.png) [@IOT-Jogi](https://www.thethingsnetwork.org/forum/u/IOT-Jogi)\
**Post date:** [July 29, 2018, 5:57pm UTC](https://www.thethingsnetwork.org/forum/t/node-red-influxdb-error/15281/13 "2018-07-29T17:57:18Z")

</div>

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”}”
