Function to dynamically change the MQTT topic based on user input

I am using 2 switch nodes after combine to extract information from DBData[1] and DBdata[2] that I use for payload. I did not know any other way to do that. Could you show me what is a better way to do that? I have tried putting 2 extracts in series but doesnt seem to work as expected. I have mode some slight changes and added inject nodes. If you could have a look! :

[{"id":"1583f34e.c8146d","type":"change","z":"df177cb8.27882","name":"set flow DBdata[0] to device","rules":[{"t":"set","p":"DBdata[0]","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":3620,"wires":[["a7604536.d7d0d8"]]},{"id":"e93e64fd.8ac098","type":"change","z":"df177cb8.27882","name":"set flow DBdata[1] to item","rules":[{"t":"set","p":"DBdata[1]","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":3720,"wires":[["dfc90860.59c1d8"]]},{"id":"4c63fa.e2c7ec08","type":"change","z":"df177cb8.27882","name":"set flow DBdata[2] to serial","rules":[{"t":"set","p":"DBdata[2]","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":3800,"wires":[["3459c09a.ad099"]]},{"id":"68f987f0.232ad8","type":"change","z":"df177cb8.27882","name":"combine 3 flows to msg payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"DBdata","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":3980,"wires":[["425a981d.dc7638","71929940.285fb8","c7f6ee34.f936b","f1718379.7bfbd","3c686fd3.598cf"]]},{"id":"425a981d.dc7638","type":"function","z":"df177cb8.27882","name":"","func":"\ndevice = msg.payload[0]\nitem = msg.payload[1]\nserial = msg.payload[2]\n\nmsg.topic=`REPLACE INTO pack_to_light (Device,Item,Serial) VALUES ('${device}','${item}',${serial})`;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":4120,"wires":[["ac048f0d.a306e"]]},{"id":"ac048f0d.a306e","type":"mysql","z":"df177cb8.27882","mydb":"97be0e96.67231","name":"Add item to database","x":1040,"y":4120,"wires":[[]]},{"id":"71929940.285fb8","type":"change","z":"df177cb8.27882","name":"Extract item information","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[1]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":3940,"wires":[["ba469c18.8b198"]]},{"id":"c7f6ee34.f936b","type":"change","z":"df177cb8.27882","name":"Extract serial information","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[2]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":4000,"wires":[["ba469c18.8b198"]]},{"id":"ba469c18.8b198","type":"join","z":"df177cb8.27882","name":"join item and serial to build payload","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1140,"y":3980,"wires":[["f7818f14.6c06","f88fd6d5.f24538"]]},{"id":"f7818f14.6c06","type":"mqtt out","z":"df177cb8.27882","name":"","topic":"msg.mqtt_topic","qos":"1","retain":"true","broker":"2727c5a5.a4fb6a","x":1180,"y":3860,"wires":[]},{"id":"f1718379.7bfbd","type":"function","z":"df177cb8.27882","name":"function to build topic","func":"msg.mqtt_topic = msg.payload[0]+'/item_inside'\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":3860,"wires":[["3ddf0077.203fb","f7818f14.6c06"]]},{"id":"3ddf0077.203fb","type":"debug","z":"df177cb8.27882","name":"mqtt topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"mqtt_topic","targetType":"msg","statusVal":"","statusType":"auto","x":1080,"y":3780,"wires":[]},{"id":"f88fd6d5.f24538","type":"debug","z":"df177cb8.27882","name":"mqtt payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1410,"y":3980,"wires":[]},{"id":"3c686fd3.598cf","type":"change","z":"df177cb8.27882","name":"Extract item information","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[1]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":4200,"wires":[["5c36b94.52ecb48"]]},{"id":"5c36b94.52ecb48","type":"change","z":"df177cb8.27882","name":"Extract serial information","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[2]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":4200,"wires":[["e1594c88.a792f"]]},{"id":"e1594c88.a792f","type":"debug","z":"df177cb8.27882","name":"mqtt payload2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1080,"y":4200,"wires":[]},{"id":"71d05dfc.5df164","type":"inject","z":"df177cb8.27882","name":"inject item","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"item1","payloadType":"str","x":320,"y":3720,"wires":[["e93e64fd.8ac098"]]},{"id":"c7922bda.049bc8","type":"inject","z":"df177cb8.27882","name":"inject serial","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1234","payloadType":"str","x":320,"y":3800,"wires":[["4c63fa.e2c7ec08"]]},{"id":"c1ebd2ef.9213e","type":"inject","z":"df177cb8.27882","name":"inject device","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"device1","payloadType":"str","x":310,"y":3620,"wires":[["1583f34e.c8146d"]]},{"id":"a7604536.d7d0d8","type":"debug","z":"df177cb8.27882","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":3620,"wires":[]},{"id":"dfc90860.59c1d8","type":"debug","z":"df177cb8.27882","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":3720,"wires":[]},{"id":"3459c09a.ad099","type":"debug","z":"df177cb8.27882","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":3800,"wires":[]},{"id":"70050f26.e0f0e","type":"inject","z":"df177cb8.27882","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":3980,"wires":[["68f987f0.232ad8"]]},{"id":"97be0e96.67231","type":"MySQLdatabase","z":"","name":"","host":"localhost","port":"3306","db":"test","tz":""},{"id":"2727c5a5.a4fb6a","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"workshop/status","birthQos":"1","birthRetain":"true","birthPayload":"online","closeTopic":"workshop/status","closeQos":"1","closeRetain":"true","closePayload":"offline","willTopic":"workshop/status","willQos":"1","willRetain":"true","willPayload":"disconnected"}]

I have managed to build topic for mqtt as required, but not sure how to pass it to mqtt publish node

1 Like