Multiple topics in one template

Hi I have the following node and Im thinking of how can I make it more simple. Because no I have 17 templates and there should be a way to just make it simpler.. Its hard to explain maybe if you see my node you will understand what I mean...

[{"id":"fe5cbd20.7d67a8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"afaacc1d.ab2918","type":"template","z":"fe5cbd20.7d67a8","name":"Office - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/Office/set/state","output":"str","x":1290,"y":240,"wires":[["cffd3bfc.e19ec"]]},{"id":"26015b8c.3b0d64","type":"json","z":"fe5cbd20.7d67a8","name":"","property":"payload","action":"","pretty":false,"x":350,"y":560,"wires":[["6f592e5.2d66ad"]]},{"id":"8206c876.72a7b","type":"mqtt in","z":"fe5cbd20.7d67a8","name":"Schalter1 Taste 6","topic":"zigbee2mqtt/Schalter1","qos":"2","datatype":"auto","broker":"5d017373.c59dac","x":160,"y":560,"wires":[["26015b8c.3b0d64"]]},{"id":"6f592e5.2d66ad","type":"switch","z":"fe5cbd20.7d67a8","name":"","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"button_6_single","vt":"str"},{"t":"eq","v":"button_6_double","vt":"str"},{"t":"eq","v":"button_6_triple","vt":"str"},{"t":"eq","v":"button_6_hold","vt":"str"},{"t":"eq","v":"button_6_release","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":490,"y":560,"wires":[["c49d748b.ce0ff"],[],[],[],[]]},{"id":"c49d748b.ce0ff","type":"function","z":"fe5cbd20.7d67a8","name":"","func":"\n var newMsg = \"toggle\"\n msg.payload = newMsg;\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":560,"wires":[["afaacc1d.ab2918","49cfc081.120028","5a2aa3c3.417864","6509746f.813eec","27ceba87.7b127e","8ac7c18b.8e6e48","9e9fd043.be181","d5d2a7ea.16cd8","b42f6075.c22f3","9d50264e.cabeb8","f1926037.a34498","39e18e88.303042","37b5294b.1e0d8e","85e9a81.1363158","36093f9c.7d412","b0de2c2b.c58b2","8c5ed665.2dd3a"]]},{"id":"49cfc081.120028","type":"template","z":"fe5cbd20.7d67a8","name":"FloorOffice - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/FloorOffice/set/state","output":"str","x":1310,"y":280,"wires":[["cffd3bfc.e19ec"]]},{"id":"5a2aa3c3.417864","type":"template","z":"fe5cbd20.7d67a8","name":"StepsOG - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/StepsOG/set/state","output":"str","x":1300,"y":320,"wires":[["cffd3bfc.e19ec"]]},{"id":"6509746f.813eec","type":"template","z":"fe5cbd20.7d67a8","name":"Floor2Play - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/Floor2Play/set/state","output":"str","x":1310,"y":360,"wires":[["cffd3bfc.e19ec"]]},{"id":"27ceba87.7b127e","type":"template","z":"fe5cbd20.7d67a8","name":"Floor2Bed - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/Floor2Bed/set/state","output":"str","x":1310,"y":400,"wires":[["cffd3bfc.e19ec"]]},{"id":"8ac7c18b.8e6e48","type":"template","z":"fe5cbd20.7d67a8","name":"KidsBed - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/KidsBed/set/state","output":"str","x":1300,"y":440,"wires":[["cffd3bfc.e19ec"]]},{"id":"9e9fd043.be181","type":"template","z":"fe5cbd20.7d67a8","name":"KidsBedBath - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/KidsBedBath/set/state","output":"str","x":1320,"y":480,"wires":[["cffd3bfc.e19ec"]]},{"id":"d5d2a7ea.16cd8","type":"template","z":"fe5cbd20.7d67a8","name":"KidsPlay - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/KidsPlay/set/state","output":"str","x":1300,"y":520,"wires":[["cffd3bfc.e19ec"]]},{"id":"b42f6075.c22f3","type":"template","z":"fe5cbd20.7d67a8","name":"KidsPlayBath - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/KidsPlayBath/set/state","output":"str","x":1320,"y":560,"wires":[["cffd3bfc.e19ec"]]},{"id":"9d50264e.cabeb8","type":"template","z":"fe5cbd20.7d67a8","name":"FloorEgUp - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/FloorEgUp/set/state","output":"str","x":1310,"y":600,"wires":[["cffd3bfc.e19ec"]]},{"id":"f1926037.a34498","type":"template","z":"fe5cbd20.7d67a8","name":"FloorEgDown - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/FloorEgDown/set/state","output":"str","x":1320,"y":640,"wires":[["cffd3bfc.e19ec"]]},{"id":"39e18e88.303042","type":"template","z":"fe5cbd20.7d67a8","name":"DiningTable - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/DiningTable/set/state","output":"str","x":1310,"y":680,"wires":[["cffd3bfc.e19ec"]]},{"id":"37b5294b.1e0d8e","type":"template","z":"fe5cbd20.7d67a8","name":"Livingroom - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/Livingroom/set/state","output":"str","x":1310,"y":720,"wires":[["cffd3bfc.e19ec"]]},{"id":"85e9a81.1363158","type":"template","z":"fe5cbd20.7d67a8","name":"FloorMaidsroom - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/FloorMaidsroom/set/state","output":"str","x":1330,"y":760,"wires":[["cffd3bfc.e19ec"]]},{"id":"36093f9c.7d412","type":"template","z":"fe5cbd20.7d67a8","name":"0xb0ce1814030e6413 - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/0xb0ce1814030e6413/set/state","output":"str","x":1340,"y":800,"wires":[["cffd3bfc.e19ec"]]},{"id":"b0de2c2b.c58b2","type":"template","z":"fe5cbd20.7d67a8","name":"0xb0ce1814030e6128 - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/0xb0ce1814030e6128/set/state","output":"str","x":1340,"y":840,"wires":[["cffd3bfc.e19ec"]]},{"id":"8c5ed665.2dd3a","type":"template","z":"fe5cbd20.7d67a8","name":"0xb0ce1814030e5c1a - state","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"zigbee2mqtt/0xb0ce1814030e5c1a/set/state","output":"str","x":1340,"y":880,"wires":[["cffd3bfc.e19ec"]]},{"id":"cffd3bfc.e19ec","type":"mqtt out","z":"fe5cbd20.7d67a8","name":"an MQTT senden","topic":"","qos":"","retain":"","broker":"5d017373.c59dac","x":1870,"y":520,"wires":[]},{"id":"5d017373.c59dac","type":"mqtt-broker","name":"MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"domoticz/bridge/state","birthQos":"0","birthRetain":"false","birthPayload":"online","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]

Do you realise you are sending the msf from the function node to all seventeen template nodes each time? If that is the case, why not build a table in the function node and loop thru it creating the complete msg and sending one msg for each table. That way you would get rid of all the template nodes and to add a new device would just mean updating the table in the function node.

1 Like

Sounds great and thats what I'm looking for but how to do it? Should I just create an array and loop it. Could your give me an example with 3 devices. That would help me a lot.

Many thanks in advance...

Here is an example

[{"id":"5825e73b.2df618","type":"mqtt in","z":"d7cd8d09.985378","name":"Schalter1 Taste 6","topic":"zigbee2mqtt/Schalter1","qos":"2","datatype":"json","broker":"a7d8322.c47985","x":580,"y":560,"wires":[["370ce238.ae7396"]]},{"id":"370ce238.ae7396","type":"function","z":"d7cd8d09.985378","name":"","func":"//set rooms / devices\nlet devices =[\"StepsOG\",\"FlorOffice\",\"Office\"];\n//set actions\nlet actions = {\"button_6_single\":\"toggle\",\"button_6_double\":\"another_action\"};\n // set payload to action\nmsg.payload = actions[msg.payload.action]\n// check msg.payload has been set\nif (!msg.payload){\n    node.warn(\"ation not set\");\n    return null;\n}\n// loop throgh rooms to create and send topics\ndevices.forEach((val) => {\n    //set topic\n    msg.topic = \"zigbee2mqtt/\"+val+\"/set/state\";\n    // send message\n    node.send(msg)\n});\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":560,"wires":[["4fe2a13e.7d37a","d5558525.5a225"]]},{"id":"4fe2a13e.7d37a","type":"debug","z":"d7cd8d09.985378","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":940,"y":500,"wires":[]},{"id":"fe6779c8.7ca1d","type":"inject","z":"d7cd8d09.985378","name":"test","props":[{"p":"payload.action","v":"button_6_single","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":620,"y":500,"wires":[["370ce238.ae7396"]]},{"id":"d5558525.5a225","type":"mqtt out","z":"d7cd8d09.985378","name":"an MQTT senden","topic":"","qos":"","retain":"","broker":"a7d8322.c47985","x":1040,"y":560,"wires":[]},{"id":"a7d8322.c47985","type":"mqtt-broker","name":"MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"domoticz/bridge/state","birthQos":"0","birthRetain":"false","birthPayload":"online","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]

[edit] removed switch node as is not need this will work for each command just add actions and rooms in function node.
Added check that msg.payload has been set

1 Like