Hi all, In my project I receive sensor data via MQTT. I'm trying to send some measurements to InfluxDB, and to do that I need to write a template. So far, I've come up with this:
[{"id":"710a3f50523457d9","type":"mqtt in","z":"2541553a8ad1942c","name":"","topic":"home/office/currenttemperature","qos":"0","datatype":"auto-detect","broker":"42a2154b.3dfb04","nl":false,"rap":true,"rh":0,"inputs":0,"x":170,"y":500,"wires":[["4b28a08dd6749e2c"]]},{"id":"44cdf9bece054463","type":"mqtt in","z":"2541553a8ad1942c","name":"","topic":"home/bedroom/currenttemperature","qos":"0","datatype":"auto-detect","broker":"42a2154b.3dfb04","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":660,"wires":[["4b28a08dd6749e2c"]]},{"id":"cce4270a0341af45","type":"mqtt in","z":"2541553a8ad1942c","name":"","topic":"home/hall/currenttemperature","qos":"0","datatype":"auto-detect","broker":"42a2154b.3dfb04","nl":false,"rap":true,"rh":0,"inputs":0,"x":160,"y":460,"wires":[["4b28a08dd6749e2c"]]},{"id":"d4747e53e031565f","type":"mqtt in","z":"2541553a8ad1942c","name":"","topic":"home/livingroom/currenttemperature","qos":"0","datatype":"auto-detect","broker":"42a2154b.3dfb04","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":540,"wires":[["4b28a08dd6749e2c"]]},{"id":"2e64b797ed7e11c4","type":"mqtt in","z":"2541553a8ad1942c","name":"","topic":"home/guestroom/currenttemperature","qos":"0","datatype":"auto-detect","broker":"42a2154b.3dfb04","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":580,"wires":[["4b28a08dd6749e2c"]]},{"id":"344a0f68f37c9745","type":"mqtt in","z":"2541553a8ad1942c","name":"","topic":"home/bathroom/currenttemperature","qos":"0","datatype":"auto-detect","broker":"42a2154b.3dfb04","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":620,"wires":[["4b28a08dd6749e2c"]]},{"id":"4b28a08dd6749e2c","type":"function","z":"2541553a8ad1942c","name":"Translate topic","func":"msg.topic = \"temperature_\" + msg.topic.split(\"/\")[1];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":460,"wires":[["c0bf7ba543d93027","145f92c1fc0467cf"]]},{"id":"145f92c1fc0467cf","type":"join","z":"2541553a8ad1942c","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"6","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":690,"y":460,"wires":[["58f9ad98bccb7b54"]]},{"id":"914a03032b03001f","type":"debug","z":"2541553a8ad1942c","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1120,"y":460,"wires":[]},{"id":"58f9ad98bccb7b54","type":"template","z":"2541553a8ad1942c","name":"home measurement","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"[{\n \"measurement\" : \"sensors\",\n \"tags\": \n {\n \"label\" : \"home\"\n },\n \"fields\": \n {\n \"temperature_bedroom\" : {{payload.temperature_bedroom}},\n \"temperature_hall\" : {{payload.temperature_hall}},\n \"temperature_livingroom\" : {{payload.temperature_livingroom}},\n \"temperature_office\" : {{payload.temperature_office}},\n \"temperature_guestroom\" : {{payload.temperature_guestroom}},\n \"temperature_bathroom\" : {{payload.temperature_bathroom}}\n }\n}]\n","output":"json","x":900,"y":460,"wires":[["914a03032b03001f"]]},{"id":"42a2154b.3dfb04","type":"mqtt-broker","name":"NUC7 MQTT broker","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]
It works, but I wonder whether it could be written without manually specifying every data point. Any thoughts?