# Dynamic subscribe MQTT

**URL:** <https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656>\
**Category:** General\
**Created:** [14 May 2021 14:25 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656 "2021-05-14T14:25:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 May 2021 14:25 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/1 "2021-05-14T14:25:09Z")

</div>

Hi @knolleary @dceejay - is this contribution still wanted?

> [@I need MQTT node that have input and output](https://discourse.nodered.org/t/i-need-mqtt-node-that-have-input-and-output/28451/22):
>
> Where there _is_ a need for another MQTT node is to be able to dynamically subscribe to topics at runtime - where you can't hard code the topic into the node.
> 
> We do have a design for such a node and it is sat in the backlog until someone is interested enough to contribute to its implementation.

I had a bit play time today and generated a new node, based fully on the current node-red mqtt nodes (in fact I did it on my node-red fork under branch `mqtt-dyn`, based off current `dev` branch today)

#### Foreword...

To keep things simple, the node does not permit dynamic/runtime choice of broker (ie this new node must have a broker selected in its config). This made implementation relatively easy and very similar in structure to the existing built in MQTT nodes.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/1/d163428a5b0d3faa3e18e92808f1d1dd673ea3cd.png)

For want of a better name, I called this node "mqtt sub"

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b80fb941b3cc60ca39529948f1241ddaeac6b2d1.png)

### Implementation

#### Inputs

The node accepts a `msg` with 3 topic options...

- **"subscribe"** - subscribe the item(s) specified in msg.payload
- **"unsubscribe"** - unsubscribe the item(s) specified in msg.payload
- **"list"** - this returns the current list of subscriptions

The payload can be either...

| info | payload | notes |
| --- | --- | --- |
| BASIC/SINGLE | string | The payload is the topic to subscribe to |
| ADVANCED/SINGLE | object | payload object should contain `.topic`. Can also include `.qos` and other MQTT V5 options |
| BASIC/MULTIPLE | array of strings | an array of topics to permit multiple subs in one go |
| ADVANCED/MULTIPLE | array of objects | An array of objects to permit multiple subs (with options), in one go |

#### Outputs

The outputs for mqtt subscriptions and status/control messages are kept separate. The reasoning behind having 2 outputs was 2 fold...

1. Sending "list" to list out the topics is all good and well until you actually have a subscription to the topic "list" - you wouldnt know which `msg` is the sub and which `msg` is the response to a `list` command
2. Removes the need for a switch node after the "mqtt sub" node (all outputs from output 1 are subscription messages, all outputs from output 2 are control/status messages)

#### DEMO...

![VrmPrPL6j4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a0de8daedac5a73e0ff85b9176af46fbb4916bf.gif)

NOTE: I checked the [whiteboard](https://trello.com/b/R0O3CSrI/node-red-whiteboard) and the issues but could not see the backlog entry for this feature - so my apologies if this is nothing like you wanted.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [14 May 2021 14:42 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/2 "2021-05-14T14:42:50Z")

</div>

Hi @Steve-Mcl

as I mentioned in that old post, we have had a design for such a node for a long time. Had you asked, I'd have happily shared the link before you started implementing something. You can find the details here: [designs/dynamic-mqtt-node.md at master · node-red/designs · GitHub](https://github.com/node-red/designs/blob/master/designs/dynamic-mqtt-node.md)

Have a read of that and let me know what you think. Keep in mind it was written prior to v5 so I suspect there's more thought needed in some of the options.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 May 2021 14:44 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/3 "2021-05-14T14:44:52Z")

</div>

I knew i had seen it somewhere - doh.

I'll have a look over and feedback Nick.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 May 2021 18:06 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/4 "2021-05-16T18:06:31Z")

</div>

Hi Nick.

Fortunately what I wrote was not a million miles off and I have now implemented the remaining features `connect` / `disconnect` / `publish` features and the `auto connect` flag for the broker.

I have also renamed it `MQTT control`

QUESTION:

- How stuck are you on using `msg.mqttControl`? It feels a bit superfluous since every "action" is a string - it feels more suited to `topic`? As it stands, i use `topic` as the action and `payload` is the parameters for the action. I have no issue changing this but just wanted to ask first.

NOTES:

- in order to support `publish` action, I had to refactor the MQTT Out node so as to avoid duplication of code
- regarding _"If the broker is already connected, the action will be ignored. ( **Q** : or should it cause a disconnect and reconnect?)"_ : currently, if the node is connected, I send a `node.error` to tell the user to disconnect first. Obviously this can be changed.
- regarding _"It will also pass through messages it receives when the action is complete. **TODO** : identify how to distinguish these from subscription messages"_ - I handled this by sending control responses to 2nd output That means only subscription messages come out of output 1 (meaning no need to distinguish control messages from subscription messages)

TESTED:

- LWT `birth` and `close` sent on actions `connect` and `disconnect` - ok
- LWT `will` sent on broker shutdown - ok
- connect without options + initial good config - connects ok
- connect without options + initial bad config : fails to connect & shows "connecting" - ok
- connect with options + initial bad config - new settings are applied and connects ok
- disconnect - ensure disconnection possible from connected AND connecting state
- subscribe 1 - ok
- subscribe multiple - ok
- subscribe 1/multiple with v5 properties - ok
- unsubscribe 1 - ok
- unsubscribe multiple - ok
- broker retained messages arrive when a connection is made (even when changing broker)

Let me know if you want me to proceed and any thoughts you have.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 May 2021 18:28 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/5 "2021-05-16T18:28:39Z")

</div>

Thanks Steve.

I haven't even glanced at this design in any detail since writing it. All of the details are up for discussion, but as it stands, I'll need to remind myself what it says before digesting everything you've written.

> [@Steve-Mcl](#):
>
> How stuck are you on using `msg.mqttControl` ? It feels a bit superfluous since every "action" is a string - it feels more suited to `topic` ?

Given topic has meaning with mqtt, I do not want to overload it as a command property.

Raise a PR in dev and we can take a look. It may well be once we have an implementation to play with, we decide the design needs changing.

I just can't promise to look at it immediately as my plate is full getting 2.0 beta ready this week.

It would be good to get into 2.0, just may not be the first beta.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 May 2021 18:33 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/6 "2021-05-16T18:33:27Z")

</div>

Yeah I was hoping it would make it into 2.0.0 betas as there is a fair bit refactoring.

I understand your point about topic being an mqtt thing and to avoid confusion so I'll spend a bit more time mulling it over and raise a PR tomorrow if that's ok?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 May 2021 18:37 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/7 "2021-05-16T18:37:17Z")

</div>

No rush. I'm not likely to look at it in any detail for a few days.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 May 2021 08:14 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/8 "2021-05-17T08:14:51Z")

</div>

Morning Nick.

I know you are super busy but would appreciate 2 mins to quickly comment on the following proposal...

In order to provide visual cues to the user and simplify setting up the `action` (while avoiding overloading `topic`) would using a drop down like the HTTP Request node be a good compromise? I propose the user would be offered a list of actions to chose from and a `- set by msg.action -` option. Using a separate msg variable string like `msg.action` simplifies things for the user to a simple change node (as opposed to having to create `msg.mqttControl` object for something simple like a publish or disconnect action)

#### mock up

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/16363d19e068717d2143bf6cbb47a686b1e2a005.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05a00c3b147b6c03c90b9db6b02ab72009f31ae2.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c7eb2f5efb5c2c5f1150a7dc9f0e825ad35c03ab.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35d3b78bc83d043b959873f25995ff687d26f6b5.png)

#### notes

I dont want to repeat the whole UI of the connection node / publish node / subscribe node (as I think we should consider this node an advanced user node) but we could provide a tip to direct the user to figuring it out for themselves?

Is this a suitable direction?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 May 2021 20:15 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/9 "2021-05-18T20:15:29Z")

</div>

PR Raised 🤞

> <https://github.com/node-red/node-red/pull/2989>
>
> \- \[\] Bugfix (non-breaking change which fixes an issue)
> \- \[x\] New feature (no…n-breaking change which adds functionality)
> 
> 
> \## Proposed changes
> Add proposed mqtt-control as specified \[here\](https://github.com/node-red/designs/blob/master/designs/dynamic-mqtt-node.md) and discussed \[here\](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656)
> 
> \## Implementation notes
> I will keep this light as I know your time is limited...
> 
> There is one major difference from the spec - that is using \`msg.action\` (string) to simplify the \`action\` (as proposed in the forum thread). That means the associated properties are now in a separate object. This permits us to provide a visual cue on what the "actions" are. Additionally, there is a dynamic tip to which property is required when the user selects an action. Lastly, the built in help details what property is used.
> 
> \## Checklist
> 
> \- \[x\] I have read the \[contribution guidelines\](https://github.com/node-red/node-red/blob/master/CONTRIBUTING.md)
> \- \[x\] For non-bugfix PRs, I have discussed this change on the forum/slack team.
> \- \[x\] I have run \`grunt\` to verify the unit tests pass
> \- \[\] I have added suitable unit tests to cover the new/changed functionality
> 
> \## Demo flow to help test the functionality...
> \`\`\`
> \[{"id":"8c8b272.301f1d8","type":"inject","z":"4b3f21a3.ba434","name":"sub multiple \[{test/test3},{test/test4},\\"test/test5\\"\]","props":\[{"p":"action","v":"subscribe","vt":"str"},{"p":"subscription","v":"\[{\\"topic\\":\\"test/test3\\",\\"datatype\\":\\"json\\",\\"qos\\":0},{\\"topic\\":\\"test/test4\\",\\"datatype\\":\\"utf8\\",\\"qos\\":1},\\"test/test5\\"\]","vt":"json"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1300,"y":1380,"wires":\[\["d835cbd0.207b28"\]\]},{"id":"18724d21.34e9a3","type":"debug","z":"4b3f21a3.ba434","name":"MQTT messages","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1570,"y":2020,"wires":\[\]},{"id":"15399895.933047","type":"inject","z":"4b3f21a3.ba434","name":"test/test1 (datatype auto)","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test1","payload":"{\\"isJSON\\":true}","payloadType":"json","x":730,"y":1800,"wires":\[\["2bb55579.21c2aa"\]\]},{"id":"e24875ef.314bd8","type":"inject","z":"4b3f21a3.ba434","name":"test/test2 (datatype json)","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test2","payload":"{\\"key\\": \\"value\\"}","payloadType":"json","x":730,"y":1840,"wires":\[\["2bb55579.21c2aa"\]\]},{"id":"2bb55579.21c2aa","type":"mqtt out","z":"4b3f21a3.ba434","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"7bab12a9.b04bcc","x":990,"y":1880,"wires":\[\]},{"id":"ef265819.c24d98","type":"inject","z":"4b3f21a3.ba434","name":"sub single string \\"test/test1\\"","props":\[{"p":"action","v":"subscribe","vt":"str"},{"p":"subscription","v":"test/test1","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1240,"y":1300,"wires":\[\["d835cbd0.207b28"\]\]},{"id":"105e4283.278b6d","type":"inject","z":"4b3f21a3.ba434","name":"sub single object {test/test2}","props":\[{"p":"action","v":"subscribe","vt":"str"},{"p":"subscription","v":"{\\"topic\\":\\"test/test2\\",\\"datatype\\":\\"json\\",\\"qos\\":0}","vt":"json"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1240,"y":1340,"wires":\[\["d835cbd0.207b28"\]\]},{"id":"8900a759.0712a8","type":"inject","z":"4b3f21a3.ba434","name":"","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test3","payload":"{\\"key\\": \\"value\\"}","payloadType":"json","x":730,"y":1880,"wires":\[\["2bb55579.21c2aa"\]\]},{"id":"ca90115a.593aa","type":"inject","z":"4b3f21a3.ba434","name":"","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test4","payload":"{\\"key\\": \\"value\\"}","payloadType":"json","x":730,"y":1920,"wires":\[\["2bb55579.21c2aa"\]\]},{"id":"400e95b5.50d65c","type":"inject","z":"4b3f21a3.ba434","name":"test/test5 (with v5 props)","props":\[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"userProperties","v":"{\\"fred\\":\\"123\\"}","vt":"json"},{"p":"responseTopic","v":"test/reply","vt":"str"},{"p":"contentType","v":"JSON","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test5","payload":"{\\"key\\": \\"value\\"}","payloadType":"json","x":730,"y":1960,"wires":\[\["2bb55579.21c2aa"\]\]},{"id":"e7e02690.7991b8","type":"inject","z":"4b3f21a3.ba434","name":"unsub single test/test1","props":\[{"p":"action","v":"unsubscribe","vt":"str"},{"p":"subscription","v":"{ \\"topic\\": \\"test/test5\\", \\"datatype\\": \\"json\\", \\"qos\\": 0 }","vt":"json"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1220,"y":1480,"wires":\[\["1e612121.aa6c4f"\]\]},{"id":"42888285.314b4c","type":"inject","z":"4b3f21a3.ba434","name":"list subscriptions","props":\[{"p":"action","v":"list","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":700,"y":1460,"wires":\[\["47c8c347.17c61c"\]\]},{"id":"3e957fe6.3a1e8","type":"inject","z":"4b3f21a3.ba434","name":"unsub all \[\\"test/test1\\", ... , \\"test/test5\\"\]","props":\[{"p":"action","v":"unsubscribe","vt":"str"},{"p":"subscription","v":"\[\\"test/test1\\",\\"test/test2\\",\\"test/test3\\",\\"test/test4\\",\\"test/test5\\"\]","vt":"json"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1270,"y":1520,"wires":\[\["1e612121.aa6c4f"\]\]},{"id":"b7bf8c92.303c3","type":"debug","z":"4b3f21a3.ba434","name":"control messages","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1570,"y":2060,"wires":\[\]},{"id":"139f8b7c.4fbf15","type":"comment","z":"4b3f21a3.ba434","name":"publish to regular mqtt out","info":"","x":730,"y":1760,"wires":\[\]},{"id":"357dd410.c98afc","type":"comment","z":"4b3f21a3.ba434","name":"Unsubscribe topics","info":"","x":1210,"y":1440,"wires":\[\]},{"id":"ff246de5.2a665","type":"comment","z":"4b3f21a3.ba434","name":"Subscribe topics","info":"","x":1200,"y":1260,"wires":\[\]},{"id":"e12462e5.a3056","type":"inject","z":"4b3f21a3.ba434","name":"connect","props":\[{"p":"action","v":"connect","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":670,"y":1340,"wires":\[\["47c8c347.17c61c"\]\]},{"id":"558d476b.0ced78","type":"inject","z":"4b3f21a3.ba434","name":"disconnect","props":\[{"p":"action","v":"disconnect","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":680,"y":1300,"wires":\[\["47c8c347.17c61c"\]\]},{"id":"7fe8eb8f.031624","type":"inject","z":"4b3f21a3.ba434","name":"connect with opts V1","props":\[{"p":"options","v":"{\\"url\\":\\"mqtt://192.168.1.50:1883\\",\\"birth\\":{\\"topic\\":\\"test/lwt/birth-V1\\",\\"qos\\":0,\\"retain\\":false,\\"payload\\":\\"birth-V1\\",\\"properties\\":{\\"correlationData\\":\\"hello birth-V1\\",\\"userProperties\\":{\\"prop1\\":\\"birth-V1\\"},\\"contentType\\":\\"application/text\\"}},\\"close\\":{\\"topic\\":\\"test/lwt/close-V1\\",\\"qos\\":0,\\"retain\\":false,\\"payload\\":\\"close-V1\\",\\"properties\\":{\\"correlationData\\":\\"hello close-V1\\",\\"userProperties\\":{\\"prop1\\":\\"close-V1\\"},\\"contentType\\":\\"application/text\\"}},\\"will\\":{\\"topic\\":\\"test/lwt/will-V1\\",\\"qos\\":0,\\"retain\\":false,\\"payload\\":\\"will-V1\\",\\"properties\\":{\\"correlationData\\":\\"hello will-V1\\",\\"userProperties\\":{\\"prop1\\":\\"will-V1\\"},\\"contentType\\":\\"application/text\\"}}}","vt":"json"},{"p":"action","v":"connect","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":710,"y":1380,"wires":\[\["47c8c347.17c61c"\]\]},{"id":"c5bb1c01.fc48f","type":"inject","z":"4b3f21a3.ba434","name":"test/test1 (datatype auto)","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test1","payload":"a string payload","payloadType":"str","x":730,"y":1540,"wires":\[\["ff4abd8b.d5d8c"\]\]},{"id":"1fbb6fb0.2e21c","type":"inject","z":"4b3f21a3.ba434","name":"test/test2 (datatype json)","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test2","payload":"{\\"bool\\":true,\\"string\\":\\"hello\\"}","payloadType":"json","x":730,"y":1580,"wires":\[\["ff4abd8b.d5d8c"\]\]},{"id":"13e8f594.bded7a","type":"inject","z":"4b3f21a3.ba434","name":"test/test3 (v5 props)","props":\[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"responseTopic","v":"test/reply","vt":"str"},{"p":"userProperties","v":"{\\"prop1\\":\\"val1\\",\\"prop2\\":\\"val2\\"}","vt":"json"},{"p":"correlationData","v":"this\_is\_correl\_data","vt":"str"},{"p":"contentType","v":"application/json","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test3","payload":"{\\"bool\\":true,\\"string\\":\\"hello\\"}","payloadType":"json","x":710,"y":1620,"wires":\[\["ff4abd8b.d5d8c"\]\]},{"id":"94ae6596.a55898","type":"link out","z":"4b3f21a3.ba434","name":"publish-\>mqtt control","links":\["17bbde58.6bd362"\],"x":1260,"y":1620,"wires":\[\],"l":true},{"id":"17bbde58.6bd362","type":"link in","z":"4b3f21a3.ba434","name":"mqtt control","links":\["47c8c347.17c61c","94ae6596.a55898","d835cbd0.207b28","1e612121.aa6c4f","e38fb27c.31965","9d777fc1.0cfec"\],"x":1170,"y":2040,"wires":\[\["1ed4d33.7151c2d"\]\],"l":true},{"id":"47c8c347.17c61c","type":"link out","z":"4b3f21a3.ba434","name":"actions-\>mqtt control","links":\["17bbde58.6bd362"\],"x":940,"y":1340,"wires":\[\],"l":true},{"id":"d835cbd0.207b28","type":"link out","z":"4b3f21a3.ba434","name":"subscribe-\>mqtt control","links":\["17bbde58.6bd362"\],"x":1590,"y":1320,"wires":\[\],"l":true},{"id":"1e612121.aa6c4f","type":"link out","z":"4b3f21a3.ba434","name":"unsubscribe-\>mqtt control","links":\["17bbde58.6bd362"\],"x":1590,"y":1500,"wires":\[\],"l":true},{"id":"9d1fa392.f2af3","type":"comment","z":"4b3f21a3.ba434","name":"publish via mqtt control","info":"","x":720,"y":1500,"wires":\[\]},{"id":"c36ac379.0b0c2","type":"comment","z":"4b3f21a3.ba434","name":"connect / disconnect / other","info":"","x":720,"y":1260,"wires":\[\]},{"id":"1ed4d33.7151c2d","type":"mqtt control","z":"4b3f21a3.ba434","name":"","broker":"7bab12a9.b04bcc","action":"setby","x":1350,"y":2040,"wires":\[\["18724d21.34e9a3"\],\["b7bf8c92.303c3"\]\]},{"id":"43229d30.74ea64","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"test/lwt/#","qos":"2","datatype":"auto","broker":"7bab12a9.b04bcc","nl":false,"rap":true,"rh":0,"x":1340,"y":2100,"wires":\[\["de1ac78e.3de038"\]\]},{"id":"de1ac78e.3de038","type":"debug","z":"4b3f21a3.ba434","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1530,"y":2100,"wires":\[\]},{"id":"e6b741ea.22857","type":"inject","z":"4b3f21a3.ba434","name":"publish no topic","props":\[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"action","v":"publish","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1200,"y":1800,"wires":\[\["9d777fc1.0cfec"\]\]},{"id":"c53e04c6.f38828","type":"inject","z":"4b3f21a3.ba434","name":"subscribe no msg.subscription","props":\[{"p":"action","v":"subscribe","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1240,"y":1880,"wires":\[\["9d777fc1.0cfec"\]\]},{"id":"5856b83d.3c2938","type":"comment","z":"4b3f21a3.ba434","name":"bad tests","info":"","x":1180,"y":1720,"wires":\[\]},{"id":"ff4abd8b.d5d8c","type":"change","z":"4b3f21a3.ba434","name":"","rules":\[{"t":"set","p":"action","pt":"msg","to":"publish","tot":"str"}\],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":1620,"wires":\[\["94ae6596.a55898"\]\]},{"id":"1dc959da.08f5c6","type":"inject","z":"4b3f21a3.ba434","name":"subscribe bad msg.subscription","props":\[{"p":"action","v":"subscribe","vt":"str"},{"p":"subscription","v":"{\\"TOPIC\\":\\"test/test1\\"}","vt":"json"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1250,"y":1920,"wires":\[\["9d777fc1.0cfec"\]\]},{"id":"95157fea.89116","type":"inject","z":"4b3f21a3.ba434","name":"publish bad topic","props":\[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"action","v":"publish","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"#/#/+/#","payload":"","payloadType":"date","x":1200,"y":1840,"wires":\[\["9d777fc1.0cfec"\]\]},{"id":"b9f15f39.e3d4","type":"inject","z":"4b3f21a3.ba434","name":"bad action","props":\[{"p":"action","v":"deliberate\_error","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1180,"y":1760,"wires":\[\["9d777fc1.0cfec"\]\]},{"id":"ce2c4a8f.bb5998","type":"inject","z":"4b3f21a3.ba434","name":"subscribe bad topic in msg.subscription","props":\[{"p":"action","v":"subscribe","vt":"str"},{"p":"subscription","v":"#/#/+/#","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1270,"y":1960,"wires":\[\["9d777fc1.0cfec"\]\]},{"id":"e16b3361.47361","type":"complete","z":"4b3f21a3.ba434","name":"","scope":\["1ed4d33.7151c2d"\],"uncaught":false,"x":1350,"y":2160,"wires":\[\["f1f605ad.680d88"\]\]},{"id":"f1f605ad.680d88","type":"debug","z":"4b3f21a3.ba434","name":"mqtt control complete monitor","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1610,"y":2160,"wires":\[\]},{"id":"9d777fc1.0cfec","type":"link out","z":"4b3f21a3.ba434","name":"bad test-\>mqtt control","links":\["17bbde58.6bd362"\],"x":1540,"y":1840,"wires":\[\],"l":true},{"id":"2f9a4f56.30938","type":"inject","z":"4b3f21a3.ba434","name":"connect with opts V2","props":\[{"p":"options","v":"{\\"broker\\":\\"192.168.1.50\\",\\"port\\":1883,\\"cleansession\\":false,\\"birth\\":{\\"topic\\":\\"test/lwt/birth-V2\\",\\"qos\\":0,\\"retain\\":false,\\"payload\\":\\"birth-V2\\",\\"properties\\":{\\"correlationData\\":\\"hello birth-V2\\",\\"userProperties\\":{\\"prop1\\":\\"birth-V2\\"},\\"contentType\\":\\"application/text\\"}},\\"close\\":{\\"topic\\":\\"test/lwt/close-V2\\",\\"qos\\":0,\\"retain\\":false,\\"payload\\":\\"close-V2\\",\\"properties\\":{\\"correlationData\\":\\"hello close-V2\\",\\"userProperties\\":{\\"prop1\\":\\"close-V2\\"},\\"contentType\\":\\"application/text\\"}},\\"will\\":{\\"topic\\":\\"test/lwt/will-V2\\",\\"qos\\":0,\\"retain\\":false,\\"payload\\":\\"will-V2\\",\\"properties\\":{\\"correlationData\\":\\"hello will-V2\\",\\"userProperties\\":{\\"prop1\\":\\"will-V2\\"},\\"contentType\\":\\"application/text\\"}}}","vt":"json"},{"p":"action","v":"connect","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":710,"y":1420,"wires":\[\["47c8c347.17c61c"\]\]},{"id":"7f40d44f.03b97c","type":"inject","z":"4b3f21a3.ba434","name":"","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test4","payload":"{\\"key\\": \\"value\\"}","payloadType":"json","x":730,"y":1660,"wires":\[\["ff4abd8b.d5d8c"\]\]},{"id":"269b950a.5744aa","type":"inject","z":"4b3f21a3.ba434","name":"test/test5 (with v5 props)","props":\[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"userProperties","v":"{\\"fred\\":\\"123\\"}","vt":"json"},{"p":"responseTopic","v":"test/reply","vt":"str"},{"p":"contentType","v":"JSON","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test5","payload":"{\\"key\\": \\"value\\"}","payloadType":"json","x":730,"y":1700,"wires":\[\["ff4abd8b.d5d8c"\]\]},{"id":"7bab12a9.b04bcc","type":"mqtt-broker","name":"PI","broker":"xxxx","port":"1883","clientid":"","autoConnect":false,"usetls":false,"compatmode":false,"protocolVersion":"5","keepalive":"60","cleansession":true,"birthTopic":"test/lwt/birth","birthQos":"0","birthPayload":"payload of test/lwt/birth","birthMsg":{"contentType":"application/json","userProps":"{\\"prop\\":\\"birth-prop\\"}","respTopic":"test/lwt/birthResponse","correl":"test/lwt/birth:Correl","expiry":"60"},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}\]
> \`\`\`

---

<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:** [17 July 2021 20:16 UTC](https://discourse.nodered.org/t/dynamic-subscribe-mqtt/45656/10 "2021-07-17T20:16:08Z")

</div>

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