Passing values to influxdb

I do get readings from my sensors via MQTT. Message payload looks like this:
"13.4375,0x6f01d51b07000728"
where 13.4375 is temp and value after come is sensor identifier. Since I will have several sensors connected, there will be different temps registered for several different sensors (with different identifier).

Influx db node is expecting to get value that will be loaded to Measurement that I would specify in the node. But I prefer not to add identifier manually, best would be if identifier that is within payload be used to define measurement for influxdb database. Is it possible to do?

Just a thought but is there a way to send a literial from the sensor like 'porch'. It would be much easier to get a list of temperatures using
select * from measurement where location = "porch"
than
select * from measurement where location = "0x6f01d51b07000728"

Eithor that or build a table in NR to equate the sensor ID to a location and stick both into the database.

Naming like "porch" would make it easier to follow, and I can code that on the sensor side to provide me name via MQTT message. So let say I can receive message "13.4, porch" instead. So I would like to send this data to influxdb, it would have field called porch with value 13.4 . I am looking for the right function in NodeRed to make correct message conversion from the one I get via MQTT to the one needed to import to influxdb. By they way, I can structure message differently before I pass it via MQTT. It can be "porch, 13.4" or "{porch: 13.4}", whatever is more suitable for infludb.

If you can structure the incoming MQTT message, set it up as a json object like

{"location": "porch", "temperature": 13.4"}

then use a 'template' node (not a 'ui_template' node) to build the insert like

insert {{msg.payload.location}},temperature={{msg.payload.temperature}}

Not sure if I understood you correctly, but if the string that comes from MQTT is "{"location": "porch", "temperature": 13.4"}" and I used template like adviced, I do not see expected output when debugging

[{"id":"7629d2d6.1873fc","type":"inject","z":"f646557d.7820f8","name":"","topic":"","payload":"{\"location\": \"porch\", \"temperature\": 13.4\"}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":800,"wires":[["f70fdd3e.766bc"]]},{"id":"ed8ed9de.1c4828","type":"debug","z":"f646557d.7820f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":520,"y":800,"wires":[]},{"id":"f70fdd3e.766bc","type":"template","z":"f646557d.7820f8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"insert {{msg.payload.location}},temperature={{msg.payload.temperature}}","output":"str","x":300,"y":800,"wires":[["ed8ed9de.1c4828"]]}]

If the message I get from MQTT is "13.5625,0x6f01d51b07000728" and then I could take this message and using function node :

var t1 = new Object();
t1.payload = {
    0x6f01d51b07000728: 13.4
}
return [ [t1] ];

get output object:

object

7998908725244987000: 13.4
then that is all I need. But I do not know how to complete that function node...

Ahh, I made a mistake and you made two. In the template node use {{payload.location}} not {{msg.payload.location}} which I said. (I always stumble over that :thinking:)
In the inject node you hade two issues (1) you sent the the contents as a string when you should have used {} (2) you had an extra double quote afrte the 13.4.

Try this:

[{"id":"d9bc2390.67b678","type":"inject","z":"d369a40c.12788","name":"","topic":"","payload":"{\"location\":\"porch\",\"temperature\":13.4}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":140,"wires":[["8c8997bc.918b88"]]},{"id":"306ba8b6.9d2578","type":"debug","z":"d369a40c.12788","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":520,"y":140,"wires":[]},{"id":"8c8997bc.918b88","type":"template","z":"d369a40c.12788","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"insert {{payload.location}},temperature={{payload.temperature}}","output":"str","x":300,"y":140,"wires":[["306ba8b6.9d2578"]]}]

Hi, I go get msg.payload "insert porch,temperature=13.4" as a result, how should I pass this data to influxdb? Using "influxdb in" node ? If yes, what should I enter as query (tried msg.payload, did not work)

Also, I see you suggest to get JSon message from MQTT. I get a string message at the moment, would need to find solution to make it to JSon, however if possible to work with string, that would be easier for me. I can use parse function to break string using "," as delimiter

decided to use tag for device. So by splitting message into two and creating new message, I was able to import data into influxdb:

var output = msg.payload.split(",");

var temp1 = parseFloat(output[0]);
var device = (output[1]);

msg.payload = [{
temperature:temp1
},
{
tag1:device
}];
return msg;

1 Like

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