Dynamic MQTT node's "actions->connect" guidance needed

Hi there,
I am a little bit stuck and have some question regarding the "dynamic support" for MQTT nodes (both in and out).
To illustrate my issue: here's my flow:

[{"id":"8afdb9e0485c9da7","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"a7f43be0a40d080b","type":"mqtt out","z":"8afdb9e0485c9da7","name":"mqtt out","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"4a46acfce798c2ed","x":1440,"y":460,"wires":[]},{"id":"6e4017154a5ae6a8","type":"function","z":"8afdb9e0485c9da7","name":"connect","func":"// var new_msg = ([{\n//   \"action\": \"connect\",\n//   \"broker\":{\n//     \"broker\": global.get(\"cba\"),\n//     \"port\": global.get(\"cbp\")\n//   },\n//   \"topic\": \"test\",\n//   \"payload\": \"test\"\n// }]);\n\n\nvar new_msg = {\n  \"action\": \"connect\",\n  \"broker\":{\n    \"force\": \"127.0.0.1\",\n    \"port\": 1883\n  },\n};\n\nreturn new_msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1020,"y":320,"wires":[["98a8e44583093459","c1caabe0b876a47b"]]},{"id":"5cb325b85eb53b39","type":"function","z":"8afdb9e0485c9da7","name":"disconnect","func":"var new_msg = {\n    \"action\": \"disconnect\"\n}\n\nreturn new_msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1010,"y":360,"wires":[["98a8e44583093459","c1caabe0b876a47b"]]},{"id":"98a8e44583093459","type":"debug","z":"8afdb9e0485c9da7","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1010,"y":420,"wires":[]},{"id":"0a81d269773b76f1","type":"inject","z":"8afdb9e0485c9da7","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":790,"y":320,"wires":[["6e4017154a5ae6a8"]]},{"id":"b4128a6651cdb84e","type":"inject","z":"8afdb9e0485c9da7","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":790,"y":360,"wires":[["5cb325b85eb53b39"]]},{"id":"c1caabe0b876a47b","type":"mqtt in","z":"8afdb9e0485c9da7","name":"mqtt in","topic":"","qos":"2","datatype":"json","broker":"4a46acfce798c2ed","nl":false,"rap":true,"rh":0,"inputs":1,"x":1430,"y":320,"wires":[[]]},{"id":"4a46acfce798c2ed","type":"mqtt-broker","name":"","broker":"192.168.132.1","port":"1560","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

Let's say I have two or more MQTT servers.
How the data structure with action -> connect should be created to over write the broker fields in both types of MQTT nodes? It's a bit unclear!

The dynamic connection does not overwrite fields. It simply connects to the broker specified in the msg you send.

You just add a broker (with any settings you like) and set it to NOT auto connect
image

then you send the appropriate msg to set up and connect.

Here is a working demo...

mqtt-dyn

[{"id":"a7f43be0a40d080b","type":"mqtt out","z":"8afdb9e0485c9da7","name":"mqtt out","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"4a46acfce798c2ed","x":1600,"y":440,"wires":[]},{"id":"0a81d269773b76f1","type":"inject","z":"8afdb9e0485c9da7","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":975,"y":300,"wires":[["1dbb8b9de866f1bb"]],"l":false},{"id":"b4128a6651cdb84e","type":"inject","z":"8afdb9e0485c9da7","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":975,"y":360,"wires":[["0804d23a71784f60"]],"l":false},{"id":"c1caabe0b876a47b","type":"mqtt in","z":"8afdb9e0485c9da7","name":"mqtt in","topic":"","qos":"2","datatype":"json","broker":"4a46acfce798c2ed","nl":false,"rap":true,"rh":0,"inputs":1,"x":1430,"y":300,"wires":[["72c6e0bd15bcab64"]]},{"id":"0804d23a71784f60","type":"change","z":"8afdb9e0485c9da7","name":"disconnect action","rules":[{"t":"set","p":"action","pt":"msg","to":"disconnect","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":360,"wires":[["c1caabe0b876a47b"]]},{"id":"1dbb8b9de866f1bb","type":"change","z":"8afdb9e0485c9da7","name":"connect to test.mosquitto.org V5","rules":[{"t":"set","p":"action","pt":"msg","to":"connect","tot":"str"},{"t":"set","p":"broker","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"broker.broker","pt":"msg","to":"test.mosquitto.org","tot":"str"},{"t":"set","p":"broker.port","pt":"msg","to":"1883","tot":"num"},{"t":"set","p":"broker.protocolVersion","pt":"msg","to":"5","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1150,"y":300,"wires":[["c1caabe0b876a47b"]]},{"id":"c64541f54dc69623","type":"change","z":"8afdb9e0485c9da7","name":"connect to localhost","rules":[{"t":"set","p":"action","pt":"msg","to":"connect","tot":"str"},{"t":"set","p":"broker","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"broker.broker","pt":"msg","to":"127.0.0.1","tot":"str"},{"t":"set","p":"broker.port","pt":"msg","to":"1883","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":260,"wires":[["c1caabe0b876a47b"]]},{"id":"8dfce72561555840","type":"inject","z":"8afdb9e0485c9da7","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":975,"y":260,"wires":[["c64541f54dc69623"]],"l":false},{"id":"1101ba965699e6e9","type":"inject","z":"8afdb9e0485c9da7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":975,"y":100,"wires":[["e3867dd8f6cc41d0"]],"l":false},{"id":"e3867dd8f6cc41d0","type":"change","z":"8afdb9e0485c9da7","name":"sub to testing_dyn_con/test1","rules":[{"t":"set","p":"action","pt":"msg","to":"subscribe","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"testing_dyn_con/test1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1140,"y":100,"wires":[["c1caabe0b876a47b"]]},{"id":"39ba9280b77d972d","type":"change","z":"8afdb9e0485c9da7","name":"sub to testing_dyn_con/test2","rules":[{"t":"set","p":"action","pt":"msg","to":"subscribe","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"testing_dyn_con/test2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1140,"y":140,"wires":[["c1caabe0b876a47b"]]},{"id":"2e59da988d7114a0","type":"change","z":"8afdb9e0485c9da7","name":"unsub ALL","rules":[{"t":"set","p":"action","pt":"msg","to":"unsubscribe","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":200,"wires":[["c1caabe0b876a47b"]]},{"id":"3335e4a872bfeaf4","type":"inject","z":"8afdb9e0485c9da7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":975,"y":140,"wires":[["39ba9280b77d972d"]],"l":false},{"id":"a581260ca57a2540","type":"inject","z":"8afdb9e0485c9da7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":975,"y":200,"wires":[["2e59da988d7114a0"]],"l":false},{"id":"d4cbc8ec38a6314a","type":"inject","z":"8afdb9e0485c9da7","name":"pub to testing_dyn_con/test1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"testing_dyn_con/test1","payload":"","payloadType":"date","x":1360,"y":440,"wires":[["a7f43be0a40d080b"]]},{"id":"1a58a113687667b9","type":"inject","z":"8afdb9e0485c9da7","name":"pub to testing_dyn_con/test2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"testing_dyn_con/test2","payload":"","payloadType":"date","x":1360,"y":480,"wires":[["a7f43be0a40d080b"]]},{"id":"c65b1239c3340bc0","type":"mqtt in","z":"8afdb9e0485c9da7","name":"","topic":"testing_dyn_con/#","qos":"2","datatype":"auto","broker":"4a46acfce798c2ed","nl":false,"rap":true,"rh":0,"inputs":0,"x":1410,"y":360,"wires":[["b4e9a4f71cf52c7d"]]},{"id":"72c6e0bd15bcab64","type":"debug","z":"8afdb9e0485c9da7","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1610,"y":300,"wires":[]},{"id":"b4e9a4f71cf52c7d","type":"debug","z":"8afdb9e0485c9da7","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1610,"y":360,"wires":[]},{"id":"4a46acfce798c2ed","type":"mqtt-broker","name":"dynamic broker1","broker":"0.0.0.0","port":"0","clientid":"","autoConnect":false,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
7 Likes

Thanks. That helped a lot

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