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.
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
{
"outsideTemp":outsideTemp,
"insideTemp":insideTemp,
"seaPressure":sea,
"backLux":lux
}
[edit] the InfluxDB node documentation has examples too.
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.
Any function node required to read ModbusTCP data before change node ??
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
It's array of 10, temp & humidity signals,, coming out of ModbusTCP, names to be assigned to each value,to log into influx DB
Have a look at
https://nodered.org/docs/user-guide/messages
Thanks, let me read it.
What are the labels for each field 0:998 1:23 ..... etc?
For example
0=Pressure
1=Temperature
.......
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,
That is only 9, not 10
Is the example you posted real data? 1997 seems rather high for humidity.
PRESSURE1,2 Array 0-9
All simulated values with decimals 99.8.....19.97.....78.6
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.
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
[{"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"]]}]
Thanks Colin, trying, inside answer bracket,empty?
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.
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).
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).
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.