Help needed with MQTT to influx

After reading the documentation of InfluxDB, I've come a bit further.
I;ve formatted the input for the Influx as following:

var tokens = msg.topic.split("/");
var Signal = {payload: msg.payload.event.data.networkStatus.signalStrength};
msg.measurement = tokens[1];
var payload = [
{
measurement: tokens[1],
fields: {
"sensor": tokens[2],
"Signalstrength": Number(Signal.payload),
},
tags: {
connector: msg.payload.event.data.networkStatus.Connectors[0].id,
transmissionMode: msg.payload.event.data.networkStatus.transmissionMode
},
//timestamp: new Date("2020-03-31T20:30:01Z").getTime() *1000 *1000
timestamp: ( Date.now() / 1000 )
}
];
//node.send({payload});
msg.payload = payload;
msg.measurement = msg.payload.measurement;
return msg;

However somehow, the measurement isn't passed on.
Influx out is missing the "msg.measurement"

So, i've added an extra function, just to add: msg.measurement.

[{"id":"fce711bc.b02a5","type":"mqtt in","z":"1b060b76.b3bb65","name":"","topic":"sensors/networkStatus/#","qos":"2","datatype":"json","broker":"5902000a.e4b36","x":350,"y":180,"wires":[["7847fa6.d1ca004","dcd10c16.83caa"]]},{"id":"dcd10c16.83caa","type":"function","z":"1b060b76.b3bb65","name":"Formatting","func":"var tokens = msg.topic.split(\"/\");\nvar Signal = {payload: msg.payload.event.data.networkStatus.signalStrength};\nmsg.measurement = tokens[1];\nvar payload = [\n    {\n        measurement: tokens[1],\n        fields: {\n            \"sensor\": tokens[2],\n            \"Signalstrength\": Number(Signal.payload),\n        },\n        tags: {\n            connector: msg.payload.event.data.networkStatus.connectors[0].id,\n            transmissionMode: msg.payload.event.data.networkStatus.transmissionMode\n        },\n        //timestamp: new Date(\"2020-03-31T20:30:01Z\").getTime() *1000 *1000\n        timestamp:  ( Date.now() / 1000 ) \n    }\n    \n];\n//node.send({payload});\nmsg.payload = payload;\nmsg.measurement = msg.payload.measurement;\nreturn msg;\n","outputs":1,"noerr":0,"x":590,"y":160,"wires":[["853fac2.f633f5","cae5283f.db57e8"]]},{"id":"cae5283f.db57e8","type":"function","z":"1b060b76.b3bb65","name":"measurement toevoegen","func":"msg.measurement = \"networkStatus\";\nreturn msg;","outputs":1,"noerr":0,"x":830,"y":160,"wires":[["9b9be73.f22c118","abd135e7.d0f098","5e3c7e07.df6a3"]]},{"id":"5e3c7e07.df6a3","type":"influxdb out","z":"1b060b76.b3bb65","influxdb":"5a8ec4d6.f465dc","name":"InfluxDB","measurement":"","precision":"","retentionPolicy":"","x":1080,"y":160,"wires":[]},{"id":"5902000a.e4b36","type":"mqtt-broker","z":"","name":"localhost","broker":"127.0.0.1","port":"1883","clientid":"node-red","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"5a8ec4d6.f465dc","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"ooksensors","name":"influxione.....","usetls":false,"tls":"6f4cf6b0.5e2ea8"},{"id":"6f4cf6b0.5e2ea8","type":"tls-config","z":"","name":"local-tls","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"","verifyservercert":false}]

the debug show me the following:

{"topic":"sensors/networkStatus/bivdmqv95ss000eukf5g","payload":[{"measurement":"networkStatus","fields":{"sensor":"bivdmqv95ss000eukf5g","Signalstrength":21},"tags":{"connector":"bhr8gf4c00013k4nvl20","transmissionMode":"LOW_POWER_STANDARD_MODE"},"timestamp":1587922440.567}],"qos":0,"retain":false,"_topic":"sensors/networkStatus/bivdmqv95ss000eukf5g","_msgid":"4b829402.283c2c","measurement":"networkStatus"}

If i connect to the "influxdb out" it will result into the following error.

Error: A 400 Bad Request error occurred: {"error":"unable to parse 'networkStatus fields=[object Object],measurement="networkStatus",tags=[object Object],timestamp=1587922375.42': invalid boolean"}

What am i missing or what am i doing wrong?
Any help would be highly appreciated.

In your fuction node (made it a bit more readable)

var m = msg.payload
var n = m.event.data.networkStatus

var t = msg.topic.split("/");
var signal =  Number(n.signalStrength);
var measurement = t[1];
var sensor = t[2]
var connector = n.connector[0].id
var transmission = n.transmissionMode
var timestamp = new Date()

var payload = [
    {
        measurement: measurement,
        fields: {
            sensor: sensor,
            Signalstrength: signal,
        },
        tags: {
            connector: connector,
            transmissionMode: transmission
        },
       
        timestamp: timestamp
    }
    
];

return {payload:payload};

Example flow:

[{"id":"4f2cd05e.18533","type":"function","z":"7eb6f8da.c3a35","name":"influx object array","func":"var m = msg.payload\nvar n = m.event.data.networkStatus\n\nvar t = msg.topic.split(\"/\");\nvar signal =  Number(n.signalStrength);\nvar measurement = t[1];\nvar sensor = t[2]\nvar connector = n.connector[0].id\nvar transmission = n.transmissionMode\nvar timestamp = new Date()\n\nvar payload = [\n    {\n        measurement: measurement,\n        fields: {\n            sensor: sensor,\n            Signalstrength: signal,\n        },\n        tags: {\n            connector: connector,\n            transmissionMode: transmission\n        },\n       \n        timestamp: timestamp\n    }\n    \n];\n\n\nreturn {payload:payload};\n","outputs":1,"noerr":0,"x":450,"y":528,"wires":[["8de8f527.e64fb8"]]},{"id":"701789a0.4650c8","type":"inject","z":"7eb6f8da.c3a35","name":"","topic":"sensors/networkStatus/bivdmqv95ss000eukf5g","payload":"{   \"event\": {     \"eventId\": \"bqheousrto76o8dikp3g\",     \"targetName\": \"projects/location/devices/mysensor\",     \"eventType\": \"networkStatus\",     \"data\": {       \"networkStatus\": {         \"signalStrength\": 78,         \"updateTime\": \"2020-04-24T13:41:15.670000Z\",         \"connector\": [           {             \"id\": \"bjr27rkc0000n66nmetg\",             \"signalStrength\": 78           }         ],         \"transmissionMode\": \"LOW_POWER_STANDARD_MODE\"       }     },     \"timestamp\": \"2020-04-24T13:41:15.670000Z\"   },   \"labels\": {} }","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":266,"y":528,"wires":[["4f2cd05e.18533"]]},{"id":"8de8f527.e64fb8","type":"debug","z":"7eb6f8da.c3a35","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":626,"y":528,"wires":[]}]

Note 1: Connect the function node to a influx batch node
Note 2: In your example mqtt output in the first post, it states: connector while in your function node later on you use 'connectors' - are the inputs different ? I used your first post as input (as we need the input to see what is going on).

1 Like

Hi @bakman2 Note1 helped me solve my puzzle and make me understand it more. Thank you.
Note2 must have been a copy paste letter fallen of the clipboard.

Something so simple, yet effective is to shorten such long paths. I will keep using it in the future to simplify my life.

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