Writing a template node

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?

Could you achieve all that with a flow something like this? The result is an object and needs to be made into an array.

[{"id":"b7eae8f8a3174085","type":"change","z":"947cea7bd1d96252","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"fields","tot":"msg"},{"t":"set","p":"measurement","pt":"msg","to":"sensors","tot":"str"},{"t":"set","p":"tags","pt":"msg","to":"{\"label\": \"home\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":380,"wires":[["5b775212b47ffca4"]]},{"id":"7b17b93592329ed5","type":"mqtt in","z":"947cea7bd1d96252","name":"","topic":"home/+/currenttemperature","qos":"0","datatype":"auto-detect","broker":"42a2154b.3dfb04","nl":false,"rap":true,"rh":0,"inputs":0,"x":150,"y":380,"wires":[["2bcc34526ca066df"]]},{"id":"2bcc34526ca066df","type":"function","z":"947cea7bd1d96252","name":"Translate topic","func":"msg.topic = \"temperature_\" + msg.topic.split(\"/\")[1];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":380,"wires":[["1b0c14bef2b03a50"]]},{"id":"1b0c14bef2b03a50","type":"join","z":"947cea7bd1d96252","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":550,"y":380,"wires":[["b7eae8f8a3174085"]]},{"id":"5b775212b47ffca4","type":"debug","z":"947cea7bd1d96252","name":"debug 88","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":900,"y":380,"wires":[]},{"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":""}]

Thanks. That looks like an elegant solution, and it's definitely a step in the right direction. I added some other sensor data, and the object now looks like this:
screenshot

I'm still struggling on how to make an array out of this. I tried Googling, but apparently I did not use the right keywords ...

Edit: Never mind. I added one function node which did the trick:

msg.payload = [msg.fields, msg.tags];
return msg;

Thanks again!

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