# Node-Red to InfluxDB

**URL:** https://discourse.nodered.org/t/node-red-to-influxdb/85639
**Category:** General
**Created:** [18 February 2024 13:55 UTC](https://discourse.nodered.org/t/node-red-to-influxdb/85639 "2024-02-18T13:55:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jonas123](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jonas123](https://discourse.nodered.org/u/jonas123)
#### Post date: [18 February 2024 13:55 UTC](https://discourse.nodered.org/t/node-red-to-influxdb/85639/1 "2024-02-18T13:55:50Z")

</div>

Hello all, I'm relatively new in programming and microcontrollers. I hope to find her some help for my project.  
I want to read out a BME280 sensor (Temperature, humidity, pressure) with an ESP32. The ESP32 is serial connected with a Raspberry Pi 3B.  
The sensor values should then be transfereved via Node-Red to InfluxDB. The values should also be displayed with Grafana.

I was able to configurate the InfluxDB server, it also gets data but shows errors. It would be nice to transform the string from the serial connection into separated Integer values (Temperature, humidity, pressure) that are readable by InfluxDB. Hope you have some advices or even a runnable code. I'm really struggling here.

Here the Flow:

 ![Nodes](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/21380d03343b6e9f3850311c25bc336d675e3d95.png)

Function 2 Code:

```auto
var data = msg.payload;

var patterns = {
    O1H: /O1H([0-9.]+)/,
    O1T: /O1T([0-9.]+)/,
    O1P: /O1P([0-9.]+)/,
    OCT: /OCT([0-9.]+)/,
    OCH: /OCH([0-9.]+)/,
    OCP: /OCP([0-9.]+)/,
};

var influxMsgs = [];

for (var key in patterns) {
    var match = data.match(patterns[key]);
    if (match) {
        var value = parseFloat(match[1]);
        var influxData = key + " value=" + value;
        var influxMsg = {
            topic: key, // Verwende den Key als Thema für InfluxDB
            payload: influxData
        };
        influxMsgs.push(influxMsg);
    }
}

return influxMsgs; 

```

Here the InfluxDBout node:

 ![Function2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/6/e6c445771020be59f70d451e774d6e9a51677f2c.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [18 February 2024 14:35 UTC](https://discourse.nodered.org/t/node-red-to-influxdb/85639/2 "2024-02-18T14:35:40Z")

</div>

> [@jonas123](#):
>
> `var influxData = key + " value=" + value;`

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d349e66cb74ef7a797958a7ad1961b9521131f0c.png)

The payload should be an object or array (not a string)

there are quite a few examples in the [readme](https://flows.nodered.org/node/node-red-contrib-influxdb)

and a lot of examples on the forum.

I appreciate you are new to programming, but you have already managed to convert the string to an object in function 1. I would branch out of function 1 and format the object(s) to the format you need for influx.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [18 February 2024 14:38 UTC](https://discourse.nodered.org/t/node-red-to-influxdb/85639/3 "2024-02-18T14:38:17Z")

</div>

> [@jonas123](#):
>
> ```auto
> var influxMsg = {
> topic: key, // Verwende den Key als Thema für InfluxDB
> payload: influxData
> };
> 
> ```

in fact, you may simply need to do this:

```auto
        var influxMsg = {
            topic: key, // Verwende den Key als Thema für InfluxDB
            payload: { [key]: value }
        };

```

but I am far from an expert on influx.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [18 February 2024 15:39 UTC](https://discourse.nodered.org/t/node-red-to-influxdb/85639/4 "2024-02-18T15:39:30Z")

</div>

Since you are receiving all the values at the same time you should send them all together to influx as named fields in a measurement. To do that you should build a msg.payload containing something similar to that seen in debug 3. You should be able to send that directly to influx, assuming those are the key names that you want.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [18 May 2024 15:39 UTC](https://discourse.nodered.org/t/node-red-to-influxdb/85639/5 "2024-05-18T15:39:50Z")

</div>

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