MQTT to InfluxDB Split Data Types (Convert Strings to Numeric and Booleans)
I needed something to parse the different data types before sending to influxDB to visualize in Grafana, or influxDB sends an error if you send floats then send booleans.
This flow Simply Parses the MQTT topic, converts from string (since MQTT sends msg.payload as strings no matter if it's a numeric or boolean data ) or subscribe to a subtopic and if data type is boolean data then convert to numeric 1 or a 0 to influxDB, if data is numeric, then convert to float, and send to influx DB.
All else, returns Null.
I'm sure I missed something and there is a better way to do this, but this was a quick and dirty way to get it working.
[{"id":"87b9861f.055a18","type":"influxdb out","z":"a2a53835.ea7788","influxdb":"b71bdadf.a27728","name":"","measurement":"","precision":"","retentionPolicy":"","x":770,"y":280,"wires":[]},{"id":"871ea8fa.caade8","type":"function","z":"a2a53835.ea7788","name":"Parse Floats/Booleans & Filter","func":" if (msg.payload === \"true\" || msg.payload === \"false\" ) {\n var tokens = msg.topic.split(\"/\");\n msg.measurement = tokens[0] + \".\" + tokens[1] + \".\" + tokens[2];\n \n if (msg.payload === \"true\"){\n \n i = 1;\n } else {\n \n i = 0;\n }\n msg.payload = [{\n value: i,\n \n },\n{\n entity_id: tokens[3] + \".\" + tokens[4],\n }];\n \n }\n \n else if (typeof parseFloat(msg.payload) === \"number\") { \n \n var tokens = msg.topic.split(\"/\");\n msg.measurement = tokens[0] + \".\" + tokens[1] + \".\" + tokens[2];\n msg.payload = [{\n value: parseFloat(msg.payload),\n },\n \n{\n entity_id: tokens[3] + \".\" + tokens[4],\n }];\n\n \n }\n \n else {\n \n return null;\n }\n \n return msg;\n","outputs":1,"noerr":0,"x":430,"y":280,"wires":[["87b9861f.055a18"]]},{"id":"823d912f.ed377","type":"mqtt in","z":"a2a53835.ea7788","name":"","topic":"jace/#","qos":"2","broker":"3c12b84b.cdc4d8","x":150,"y":280,"wires":[["871ea8fa.caade8"]]},{"id":"b71bdadf.a27728","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"jace_db","name":"","usetls":false,"tls":"c03ef566.d75e48"},{"id":"3c12b84b.cdc4d8","type":"mqtt-broker","z":"a2a53835.ea7788","broker":"localhost","port":"1883","clientid":"node-red","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":""},{"id":"c03ef566.d75e48","type":"tls-config","z":"","name":"local-tls","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"","verifyservercert":false}]