Sending data from Node-Red to InfluxDB
This post was started because I was unable to get data from Node-Red into InfluxDB in the format that I wanted. With some pointers provided in the post I still wasn’t able to get the coding necessary to get the data into InfluxDB in the format I was after. Although I had (sort of) read the Node-Red info for ‘InfluxDB Out’ node, it didn’t seem to provide the information I was seeking, but eventually I found it did have it, just not in a sufficiently basic manner of an Idiot’s Guide that I would have liked.
My browsing and looking at flows I found in other forums got me a basic method of writing data into InfluxDB, but not using ‘tags’. Reading the ‘InfluxDB Out’ node info, the text didn’t help, but the flows provided once loaded allowed me to play with a working example of writing data to InfluxDB with tags and from there I was able to write my data the way I wanted to.
So, my Idiot’s Guide.
To write data, you need to populate the InfluxDB Out node with some details, only one is optional. The none optional details are:-
Server, which requires dB version, dB url, Username and Password
Database name
The optional detail:-
Measurement, this has to be provided, but it can be done either here in the InfluxDB Out node, or as a variable set earlier in the flow by another node (msg.measurement = “”;), this latter method allows the measurement to be changed from one set of data to the next based on data being fed in.
All of the above is straight forward.
Scenario 1, Writing a single data value without tags:-
The following flow, simply writes a random number to a field in database ‘dB1’ and measurement ‘test’.

[{"id":"1cd3d3436ac99b73","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"80cc26f1225bcff7","type":"influxdb out","z":"1cd3d3436ac99b73","influxdb":"7111b0228e82b576","name":"Single Value","measurement":"_test","precision":"","retentionPolicy":"","database":"dB1","retentionPolicyV18Flux":"","org":"","bucket":"","x":590,"y":120,"wires":[]},{"id":"c8b314fe7ab955e3","type":"function","z":"1cd3d3436ac99b73","name":"single value","func":"msg.payload = Math.random()*10;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":120,"wires":[["80cc26f1225bcff7"]]},{"id":"f406b8bfc2f56bd6","type":"inject","z":"1cd3d3436ac99b73","name":"","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"str"}],"repeat":"","crontab":"","once":false,"topic":"","payload":"","payloadType":"date","x":280,"y":120,"wires":[["c8b314fe7ab955e3"]]},{"id":"7111b0228e82b576","type":"influxdb","hostname":"homeassistant.local","port":"8086","protocol":"http","database":"user","name":"","usetls":false,"tls":"","influxdbVersion":"1.8-flux","url":"http://homeassistant.local:8086","rejectUnauthorized":false}]
InfluxDB before running the above:-
InfluxDB after running the above:-
The measure ‘_test’ is created and a field ‘value’, and there are no ‘tags’.
Scenario 2, Writing a single data value with a tag:-

The following flow, writes a random number to a field in database ‘dB1’ and measurement ‘test’, but with two tags.
[{"id":"80cc26f1225bcff7","type":"influxdb out","z":"1cd3d3436ac99b73","influxdb":"7111b0228e82b576","name":"Single Value with tag","measurement":"_test","precision":"","retentionPolicy":"","database":"DB1","retentionPolicyV18Flux":"","org":"","bucket":"","x":680,"y":300,"wires":[]},{"id":"c3b7b127909164ce","type":"inject","z":"1cd3d3436ac99b73","name":"","repeat":"","crontab":"","once":false,"topic":"","payload":"","payloadType":"date","x":280,"y":300,"wires":[["addbfbfef2286103"]]},{"id":"addbfbfef2286103","type":"function","z":"1cd3d3436ac99b73","name":"Fields and Tags","func":"msg.payload = [{\n Value1: Math.random()*10\n},\n{\n tag1:\"tag1-name\",\n tag2:\"tag2-name\"\n}];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":300,"wires":[["80cc26f1225bcff7"]]},{"id":"7111b0228e82b576","type":"influxdb","hostname":"homeassistant.local","port":"8086","protocol":"http","database":"CaponGJ","name":"","usetls":false,"tls":"","influxdbVersion":"1.8-flux","url":"http://homeassistant.local:8086","rejectUnauthorized":false}]
InfluxDB before running the above:-
InfluxDB after running the above:-
The measure ‘_test’ is created and a field ‘value’, with two 'tags'.
The code for creating this is:-
msg.payload =
[
{
Value1: Math.random()*10
},
{
tag1:"tag1-name",
tag2:"tag2-name"
}
];
return msg;
If you want multiple values with tags, then simply take the above, as a block of code, stick a comma after the closing square bracket and wrap the whole lot up in another pair of square brackets:-
msg.payload =
[ //(new)
//---------------------------------------------- 1st value
[
{
Value1: Math.random()*10
},
{
tag1:"tag1-name",
tag2:"tag2-name"
}
], // stick the comma on the end of this original block
// from here down is a repeat of the above
//---------------------------------------------- 2nd value
[
{
Value2: Math.random()*15
},
{
tag1:"tag1-name",
tag3:"tag3-name"
}
] // this is the end of the second block
] // and the final closing square bracket to match the first
;
which gives this:-
Hopefully, this is of help to other newbies.
