# Input data to Influxdb

**URL:** <https://discourse.nodered.org/t/input-data-to-influxdb/53324>\
**Category:** General\
**Tags:** database\
**Created:** [7 November 2021 09:58 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324 "2021-11-07T09:58:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [7 November 2021 09:58 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/1 "2021-11-07T09:58:20Z")

</div>

Hi All  
I have created a flow to pull data from a solar charge controller and send to MQTT then a String node to select the 'voltage' data from the string. I would like to input this data to Influxdb but im stuck on the code needed in the Function node to change the data into the right format for Influxdb.

Via a debug node the output from my String node is

> Mppt/Sunsaver : msg.payload : string[5]
> 
> "24.89"

With the Function node how do I input this to Influxdb.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![\_R\_A\_L\_F](https://avatars.discourse-cdn.com/v4/letter/_/9fc348/32.png) [@\_R\_A\_L\_F](https://discourse.nodered.org/u/_R_A_L_F)\
**Post date:** [7 November 2021 10:16 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/2 "2021-11-07T10:16:12Z")

</div>

Hi,  
have you already installed this node?

> **[node-red-contrib-influxdb](https://flows.nodered.org/node/node-red-contrib-influxdb)**
>
> Node-RED nodes to save and query data from an influxdb time series database

There is also a good example described.

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [7 November 2021 13:27 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/3 "2021-11-07T13:27:12Z")

</div>

Hi,  
Yes i have, its the coding that i cant work out,  
to turn my 'msg.payload: string[5] "24.89" ' into a field and value in influxdb.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [7 November 2021 15:03 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/4 "2021-11-07T15:03:00Z")

</div>

In a function node before the influx node, try something like this:

```auto
msg.payload={"the name of your field" : your value };
return msg;

```

This is how I do it when receiving and posting to influx multiple values:

```auto
var t=msg.payload;
if(t!=="")
{
k=t.split(':');
for (i = 0; i < k.length; i++)
{
    k[i]=parseInt(k[i]);
}
msg.topic="Analogues";
	if(k[0]&&k[1]&&k[2]&&k[3]&&k[4]&&k[5]&&k[6]&&k[7]&&k[8]&&k[9]&&k[10]&&k[11]&&k[12]&&k[13]&&k[14]&&k[15])
		{
		msg.payload={"AP0":k[0],"AP1":k[1],"AP2":k[2],"AP3":k[3],"BP0":k[4],"BP1":k[5],"BP2":k[6],"BP3":k[7],"CP0":k[8],"CP1":k[9],"CP2":k[10],"CP3":k[11],"DP0":k[12],"DP1":k[13],"DP2":k[14],"DP3":k[15]};
		return msg;

    }
}

```

---

<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:** [7 November 2021 17:35 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/5 "2021-11-07T17:35:05Z")

</div>

What did you have before the string node?

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [8 November 2021 08:18 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/6 "2021-11-08T08:18:47Z")

</div>

the node before is a MQTT in.

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [8 November 2021 08:28 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/7 "2021-11-08T08:28:12Z")

</div>

thank you, but would i have to manually write the value each time i want to input?

---

<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:** [8 November 2021 08:33 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/8 "2021-11-08T08:33:50Z")

</div>

I meant what does the data look like before the string node, it may be there is a better way.

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [8 November 2021 08:46 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/9 "2021-11-08T08:46:45Z")

</div>

Ah ok,, , its a string

> msg.payload : string[6328]

`

---

<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:** [8 November 2021 08:48 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/10 "2021-11-08T08:48:29Z")

</div>

Show us the start of the string, we need to see the structure.

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [8 November 2021 08:51 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/11 "2021-11-08T08:51:43Z")

</div>

heres an example

```auto
> Morningstar SunSaver MPPT Charge Controller Settings
> 
> Choose mode...
> 1. Read
> 2. Write
> 3. Logs
> 4. Write Coils
> 5. Live Reload
> Mode: 
> Attempting to connect to /dev/ttyUSB0
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> Adc_vb_f = 25.01 V (1sec avg)
> Adc_va_f = 30.96 V (1sec avg)
> Adc_vl_f = 24.91 V (1sec avg)
> Adc_ic_f = 0.57 A (1sec avg)
> Adc_il_f = 1.19 A (1sec avg)
> T_hs = 15 °C
> T_batt = 11 °C
> Power_out = 14.15 W
> Vb_min_daily (resets after dark) = 24.15 V
> Vb_max_daily (resets after dark) = 25.36 V
> Ahc_daily (resets after dark) = 0.60 Ah
> Ahl_daily (resets after dark) = 5.10 Ah
> charge_state = 5 (0=start,1=night_check,2=disconnect,3=night,4=fault,5=bulk_charge,6=absorption,7=float,8=equalize)
> Vb_f = 25.05 V (25sec avg)
> Vb_ref = 24.22 V
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> Adc_vb_f = 25.01 V (1sec avg)
> Adc_va_f = 30.97 V (1sec avg)
> Adc_vl_f = 24.90 V (1sec avg)
> Adc_ic_f = 0.57 A (1sec avg)
> Adc_il_f = 1.17 A (1sec avg)
> T_hs = 15 °C
> T_batt = 11 °C
> Power_out = 14.18 W
> Vb_min_daily...

```

its the Adc\_vb\_f = that i want to monitor.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [8 November 2021 09:14 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/12 "2021-11-08T09:14:38Z")

</div>

It is not clear how you get the values into red-node environment.  
Can you post your flow?

---

<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:** [8 November 2021 09:29 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/13 "2021-11-08T09:29:19Z")

</div>

You are not making this easy. Feed the mqtt node into a debug node and show us what you get.

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [8 November 2021 09:44 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/14 "2021-11-08T09:44:43Z")

</div>

Sorry for my confusion, the example above is what i get from a debug node connected to the mqtt in.

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [8 November 2021 09:50 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/15 "2021-11-08T09:50:48Z")

</div>

this is the 'value copied' from the debug node

```auto

```

Morningstar SunSaver MPPT Charge Controller Settings

Choose mode...

1. Read
2. Write
3. Logs
4. Write Coils
5. Live Reload  
Mode:  
Attempting to connect to /dev/ttyUSB0

Adc\_vb\_f = 25.60 V (1sec avg)  
Adc\_va\_f = 33.89 V (1sec avg)  
Adc\_vl\_f = 25.51 V (1sec avg)  
Adc\_ic\_f = 1.41 A (1sec avg)  
Adc\_il\_f = 1.13 A (1sec avg)  
T\_hs = 19 °C  
T\_batt = 11 °C

Adc\_vb\_f = 25.60 V (1sec avg)  
Adc\_va\_f = 33.89 V (1sec avg)  
Adc\_vl\_f = 25.50 V (1sec avg)  
Adc\_ic\_f = 1.41 A (1sec avg)  
Adc\_il\_f = 1.14 A (1sec avg)  
T\_hs = 19 °C  
T\_batt = 11 °C

Adc\_vb\_f = 25.60 V (1sec avg)  
Adc\_va\_f = 33.89 V (1sec avg)  
Adc\_vl\_f = 25.51 V (1sec avg)  
Adc\_ic\_f = 1.41 A (1sec avg)  
Adc\_il\_f = 1.10 A (1sec avg)  
T\_hs = 19 °C  
T\_batt = 11 °C  
Power\_out = 36.04 W  
Vb\_min\_daily (resets after dark) = 24.15 V  
Vb\_max\_daily (resets after dark) = 26.24 V  
Ahc\_daily (resets after dark) = 2.10 Ah  
Ahl\_da...

```auto

```

---

<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:** [8 November 2021 09:56 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/16 "2021-11-08T09:56:01Z")

</div>

OK, in that case you will have to extract it using string manipulation, as you are doing. To convert the msg.payload string to a number all you need is a function node containing  
`msg.payload = Number(msg.payload)`  
`return msg`

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [8 November 2021 10:14 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/17 "2021-11-08T10:14:52Z")

</div>

Thank you, I now have a number,  
Can i send this straight to the influxdb out node?

---

<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:** [8 November 2021 10:22 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/18 "2021-11-08T10:22:28Z")

</div>

If you want it be in a field called `value` in the Measurement that you configure in the Influx Out node then yes. The help text for the node describes the payload formats supported.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 November 2021 10:50 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/19 "2021-11-08T10:50:10Z")

</div>

Your string is 6328 characters and we only have 535 of those to look at, but it's clear that you have multiple lines containing

> Adc\_vb\_f = nn.nn V

I think that your String node to extract the value will only return the first matching line.  
Is that OK or do you need all matches?

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [8 November 2021 13:11 UTC](https://discourse.nodered.org/t/input-data-to-influxdb/53324/20 "2021-11-08T13:11:21Z")

</div>

No its not a problem, the code that pulls the data from the Modbus unit every 5 seconds for 30 seconds, It maybe a bit messy but is accurate enough for what i want.

[Next page](https://discourse.nodered.org/t/input-data-to-influxdb/53324.md?page=2)
