# Modbus TCP to InfluxDB datalogging

**URL:** <https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560>\
**Category:** Industrial\
**Created:** [26 April 2020 17:59 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560 "2020-04-26T17:59:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [26 April 2020 17:59 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/1 "2020-04-26T17:59:28Z")

</div>

I am new to node-red, json . want to log data of several signals - temperature, humidity, gas leak, oxygen level etc coming from a PLC ,Modbus TCP to InfluxDB, how to do with function node?  
Modbus Getter node - function node - InfluxDB node.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [26 April 2020 18:44 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/2 "2020-04-26T18:44:35Z")

</div>

Welcome to the forum! This is also possible with a change node using Jsonata instead of a function node:

I use a change node with the following contents to store weather data in an InfluxDB databank

```auto
{
"outsideTemp":outsideTemp,
"insideTemp":insideTemp,
"seaPressure":sea,
"backLux":lux
}

```

[edit] the InfluxDB node documentation has examples too.

---

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [26 April 2020 19:49 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/3 "2020-04-26T19:49:53Z")

</div>

Thanks Ghayne,made as follows with change,see the error.

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

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [26 April 2020 19:57 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/4 "2020-04-26T19:57:09Z")

</div>

My data was only meant as an example of the data structure, you have to use your own properties with appropriate labels. Put a debug node set to display the complete msg object on the output of the modbus node and post the output.

---

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [26 April 2020 20:05 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/5 "2020-04-26T20:05:08Z")

</div>

Any function node required to read ModbusTCP data before change node ??

---

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [26 April 2020 20:19 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/6 "2020-04-26T20:19:13Z")

</div>

10 values coming out of Modbus TCP, coming to debug node, these values to be logged on influxDB with 10 header like temp1,humd1,out temp1,2,3,........etc

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

---

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [26 April 2020 20:30 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/7 "2020-04-26T20:30:22Z")

</div>

It's array of 10, temp & humidity signals,, coming out of ModbusTCP, names to be assigned to each value,to log into influx DB

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

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [26 April 2020 20:32 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/8 "2020-04-26T20:32:16Z")

</div>

Have a look at  
[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [26 April 2020 20:35 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/9 "2020-04-26T20:35:43Z")

</div>

Thanks, let me read it.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [26 April 2020 20:47 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/10 "2020-04-26T20:47:50Z")

</div>

What are the labels for each field 0:998 1:23 ..... etc?  
For example  
0=Pressure  
1=Temperature  
.......

---

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [26 April 2020 21:15 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/11 "2020-04-26T21:15:48Z")

</div>

From Modbus TCP - fc3 values from register start address:200,qty:10. 200,201,...209

value names to be : Temp1,2,3,4,humidity1,,2,3,4,pressure,

---

<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:** [26 April 2020 21:34 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/12 "2020-04-26T21:34:53Z")

</div>

> [@Rsich](#):
>
> value names to be : Temp1,2,3,4,humidity1,,2,3,4,pressure

That is only 9, not 10  
Is the example you posted real data? 1997 seems rather high for humidity.

---

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [26 April 2020 21:44 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/13 "2020-04-26T21:44:58Z")

</div>

PRESSURE1,2 Array 0-9

---

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [26 April 2020 21:47 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/14 "2020-04-26T21:47:46Z")

</div>

All simulated values with decimals 99.8.....19.97.....78.6

---

<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:** [27 April 2020 08:13 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/15 "2020-04-27T08:13:56Z")

</div>

A function node containing something like this should do it. I have just shown the first three fields to save my typing but hopefully you will see how to add the rest. It isn't clear exactly how you get from the input value to the real value so I have assumed you want to divide by 100. Change that if it is not right.

```auto
const fieldNames = ["temp1", "temp2", "temp3"]
answer = {}
for (let i=0; i<fieldNames.length; i++) {
    answer[fieldNames[i]] = msg.payload[i] / 100
}
msg.payload = answer
return msg

```

Here is a test flow showing it in operation

```auto
[{"id":"340f8c08.03c45c","type":"inject","z":"bdd7be38.d3b55","name":"","topic":"","payload":"[123,89,10]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":106,"y":659,"wires":[["4a67d888.294228"]]},{"id":"2d837a5d.61aa86","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":447,"y":660,"wires":[]},{"id":"4a67d888.294228","type":"function","z":"bdd7be38.d3b55","name":"Build","func":"const fieldNames = [\"temp1\", \"temp2\", \"temp3\"]\nanswer = {}\nfor (let i=0; i<fieldNames.length; i++) {\n answer[fieldNames[i]] = msg.payload[i] / 100\n}\nmsg.payload = answer\nreturn msg","outputs":1,"noerr":0,"x":271,"y":660,"wires":[["2d837a5d.61aa86"]]}]

```

---

<div class="post-metadata">

**Author:** ![Rsich](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rsich](https://discourse.nodered.org/u/Rsich)\
**Post date:** [27 April 2020 10:38 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/16 "2020-04-27T10:38:03Z")

</div>

Thanks Colin, trying, inside answer bracket,empty?

---

<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:** [27 April 2020 12:34 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/17 "2020-04-27T12:34:21Z")

</div>

It starts empty then gets filled in with the fields inside the loop, one value each time round. The syntax `answer[fieldNames[i]]` means get the value of fieldNames[i] and then use that as the field name. So the first time round `i` is 0 so fieldNames[i] is "temp1", so `answer[fieldNames[i]]` is the same as answer.temp1, and that is set to the value in the appropriate element from the input data in msg.payload.

---

<div class="post-metadata">

**Author:** ![mturnbull](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mturnbull/32/20742_2.png) [@mturnbull](https://discourse.nodered.org/u/mturnbull)\
**Post date:** [27 April 2020 13:37 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/18 "2020-04-27T13:37:13Z")

</div>

Another option to consider for writing to the database is to just use the HTTP Request Node. With this you can better control the data types going into InfluxDB (string, integer, float or boolean). You simply connect a function node and then write it directly in. No need for the "influxdb in" node with this approach.... which I believe does not handle integers well (if my memory serves me correctly).

![Http request](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5b6fb9d823e05e9cf6e7c308f17f916593c99be4.jpeg)

---

<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:** [27 April 2020 14:00 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/19 "2020-04-27T14:00:07Z")

</div>

> [@mturnbull](#):
>
> No need for the "influxdb in" node with this approach.... which I believe does not handle integers well

I presume you mean Influx Out, I don't think there are any integer types in this requirement ( you may not have noticed the post suggesting they are scaled by 100, or something similar).

---

<div class="post-metadata">

**Author:** ![mturnbull](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mturnbull/32/20742_2.png) [@mturnbull](https://discourse.nodered.org/u/mturnbull)\
**Post date:** [27 April 2020 14:39 UTC](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560/20 "2020-04-27T14:39:54Z")

</div>

Yes “influx out” node for writing. My mistake - but it’s been a while because I’ve gone away from using these nodes - they don’t handle data types well. My comment was more a general one and as such not designed to side track the solution direction. For this application I’m not sure if integers are needed or not ( as you’ve pointed out ) but if you do enough of these projects eventually it will catch you because integer and boolean are very common modbus data types.

[Next page](https://discourse.nodered.org/t/modbus-tcp-to-influxdb-datalogging/25560.md?page=2)
