# Writing a template node

**URL:** https://discourse.nodered.org/t/writing-a-template-node/72788
**Category:** General
**Created:** [27 December 2022 19:59 UTC](https://discourse.nodered.org/t/writing-a-template-node/72788 "2022-12-27T19:59:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jw\_te\_r](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jw_te_r/32/69700_2.png) [@jw\_te\_r](https://discourse.nodered.org/u/jw_te_r)
#### Post date: [27 December 2022 19:59 UTC](https://discourse.nodered.org/t/writing-a-template-node/72788/1 "2022-12-27T19:59:10Z")

</div>

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:

```auto
[{"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?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [27 December 2022 20:41 UTC](https://discourse.nodered.org/t/writing-a-template-node/72788/2 "2022-12-27T20:41:50Z")

</div>

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

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/117104ac27f4817b5152972588360613ddb7d05e.jpeg)

```auto
[{"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":""}]

```

---

<div class="post-metadata">

### Author: ![jw\_te\_r](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jw_te_r/32/69700_2.png) [@jw\_te\_r](https://discourse.nodered.org/u/jw_te_r)
#### Post date: [28 December 2022 11:44 UTC](https://discourse.nodered.org/t/writing-a-template-node/72788/3 "2022-12-28T11:44:29Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/066c5915a33a008b34bc7f0b0aa41a47de8af074.jpeg)

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:

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

```

Thanks again!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [11 January 2023 11:44 UTC](https://discourse.nodered.org/t/writing-a-template-node/72788/4 "2023-01-11T11:44:42Z")

</div>

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