I have a flow where user inputs data through dropdown and form menus. I save this data in flow variable as such:
flow.DBdata[0] = Device
flow.DBdata[1] = Item
flow.DBdata[2] = Serial
What I need to do is to join DBdata[1] and DBdata[2] to a single message and then send mqtt message to whatever Device described in DBdata[0]
So the MQTT topic should look like:
Device/topic_name
MQTT payload:
Item,Serial
I know how to build a single message out of 2 individual messages, but I am not too sure how can I change MQTT topic since I have to manually input topic when I am using MQTT publish node.
flow:
[{"id":"95b7e688.b0a538","type":"change","z":"df177cb8.27882","name":"set flow DBdata.device","rules":[{"t":"set","p":"DBdata[0]","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2700,"wires":[[]]},{"id":"32a52bea.d3a564","type":"change","z":"df177cb8.27882","name":"set flow DBdata.item","rules":[{"t":"set","p":"DBdata[1]","pt":"flow","to":"payload.Item","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2800,"wires":[[]]},{"id":"715089ad.12de08","type":"change","z":"df177cb8.27882","name":"set flow DBdata.serial","rules":[{"t":"set","p":"DBdata[2]","pt":"flow","to":"payload.Serial","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2880,"wires":[[]]},{"id":"9188896.d0fdd78","type":"ui_dropdown","z":"df177cb8.27882","name":"","label":"Select device test","tooltip":"","place":"Select option","group":"3521c1e4.63bd6e","order":3,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"Device1","value":"Device1","type":"str"},{"label":"Device2","value":"Device2","type":"str"},{"label":"Device3","value":"Device3","type":"str"}],"payload":"","topic":"","x":250,"y":2700,"wires":[["95b7e688.b0a538"]]},{"id":"a133b7b3.55e198","type":"ui_form","z":"df177cb8.27882","name":"","label":"Add data to mysql table test","group":"3521c1e4.63bd6e","order":4,"width":0,"height":0,"options":[{"label":"Item","value":"Item","type":"text","required":true,"rows":null},{"label":"Serial","value":"Serial","type":"text","required":true,"rows":null}],"formValue":{"Item":"","Serial":""},"payload":"","submit":"submit","cancel":"cancel","topic":"","x":220,"y":2840,"wires":[["32a52bea.d3a564","715089ad.12de08"]]},{"id":"c8fc5ac1.e56758","type":"ui_button","z":"df177cb8.27882","name":"","group":"3521c1e4.63bd6e","order":5,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":190,"y":2980,"wires":[["8641e264.f6037"]]},{"id":"8641e264.f6037","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":490,"y":2980,"wires":[["41d59152.53a51","922fd2dc.d6f3e","f34040a3.1fb81"]]},{"id":"41d59152.53a51","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":720,"y":2980,"wires":[["4ecc06d0.50b9b8"]]},{"id":"4ecc06d0.50b9b8","type":"mysql","z":"df177cb8.27882","mydb":"97be0e96.67231","name":"Add item to database","x":920,"y":2980,"wires":[[]]},{"id":"922fd2dc.d6f3e","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":770,"y":2880,"wires":[["98551b72.f10e88"]]},{"id":"f34040a3.1fb81","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":770,"y":3060,"wires":[["98551b72.f10e88"]]},{"id":"98551b72.f10e88","type":"join","z":"df177cb8.27882","name":"","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":1170,"y":2980,"wires":[["6fecb1dd.d6a7d"]]},{"id":"6fecb1dd.d6a7d","type":"mqtt out","z":"df177cb8.27882","name":"","topic":"device3/item_inside","qos":"1","retain":"true","broker":"2727c5a5.a4fb6a","x":1350,"y":2980,"wires":[]},{"id":"3521c1e4.63bd6e","type":"ui_group","z":"","name":"MYSQL","tab":"cc84c991.418318","order":4,"disp":true,"width":"6","collapse":false},{"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"},{"id":"cc84c991.418318","type":"ui_tab","z":"","name":"Control","icon":"dashboard","disabled":false,"hidden":false}]
As you can see, my MQTT topic is device3/item_inside and the payload is joined message from DB_data[1] and DB_data[2].
How can I make the MQTT topic change based on user input DB_data[0].
If user inputs device20 I want my topic to automatiaclly change to device20/item_inside