Using flow variables in an mqtt topic

Hi all,

I have a number of mqtt topic subscriptions in my flow that I push to a dashboard.

Now, I want to replace ${QWKID} and ${BBSHOST} with flow variables.

I setup a function to configure two flow variables, QWKID and BBSHOST
image

Now the question I have is, how do I connect in the function or get the variables to be assigned and then how do I use them in the mqtt topic subscription?

Thanks,

If you are running the latest node-red you can use dynamic subscriptions in the mqtt in node. there is a dropdown in the node config to select this option. There is help text in the sidebar that describes the properties you need to send it.
Have a go and feel free to ask if you get any issues.

That's nice, but I'm using mqtt in, not mqtt out.

I see that mqtt in also has dynamic subscriptions but I'm not sure how that really helps me. It doesn't explain much in the help.

That was a typo i meant in.
It explains it very clearly in the help you need to send msg.action of "subscribe" or "unsubscribe" and the msg.topic containing the topic you wish to subscribe or unsubscribe to.
An example

[{"id":"8f199f8fc9b6248b","type":"inject","z":"da8a6ef0b3c9a5c8","name":"subscription1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":240,"wires":[["c09da18ead2e148b"]]},{"id":"c09da18ead2e148b","type":"trigger","z":"da8a6ef0b3c9a5c8","name":"","op1":"unsubscribe","op2":"subscribe","op1type":"str","op2type":"str","duration":"250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":2,"x":500,"y":240,"wires":[["c19f6a358c4f21f8"],["89fcea1eae433bdc"]]},{"id":"c19f6a358c4f21f8","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"subscription1","tot":"flow"},{"t":"move","p":"payload","pt":"msg","to":"action","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":220,"wires":[["3632416ab31f0abd"]]},{"id":"89fcea1eae433bdc","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"text1","pt":"msg","to":"QWKID","tot":"flow"},{"t":"set","p":"text2","pt":"msg","to":"BBSHOST","tot":"flow"},{"t":"set","p":"topic","pt":"msg","to":"\"start/\" & $$.text1 & \"/\" & $$.text2 & \"/end\"","tot":"jsonata"},{"t":"set","p":"subscription1","pt":"flow","to":"topic","tot":"msg"},{"t":"move","p":"payload","pt":"msg","to":"action","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":260,"wires":[["f0489606dab85a12"]]},{"id":"3632416ab31f0abd","type":"switch","z":"da8a6ef0b3c9a5c8","name":"","property":"topic","propertyType":"msg","rules":[{"t":"istype","v":"string","vt":"string"}],"checkall":"true","repair":false,"outputs":1,"x":870,"y":220,"wires":[["f0489606dab85a12"]]},{"id":"f0489606dab85a12","type":"mqtt in","z":"da8a6ef0b3c9a5c8","name":"","topic":"","qos":"2","datatype":"auto-detect","broker":"e8ba3ef5.22f4a8","nl":false,"rap":true,"rh":0,"inputs":1,"x":1030,"y":240,"wires":[["e5e60a2f2d3ff4cc"]]},{"id":"e5e60a2f2d3ff4cc","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 114","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":180,"wires":[]},{"id":"8b227618062354e4","type":"inject","z":"da8a6ef0b3c9a5c8","name":"test subscription1","props":[{"p":"payload"},{"p":"text1","v":"QWKID","vt":"flow"},{"p":"text2","v":"BBSHOST","vt":"flow"},{"p":"topic","v":"\"start/\" & $$.text1 & \"/\" & $$.text2 & \"/end\"","vt":"jsonata"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":320,"wires":[["c758eda20c3b6f6b"]]},{"id":"c758eda20c3b6f6b","type":"mqtt out","z":"da8a6ef0b3c9a5c8","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"e8ba3ef5.22f4a8","x":510,"y":320,"wires":[]},{"id":"ef9f06be11be1595","type":"inject","z":"da8a6ef0b3c9a5c8","name":"set flow vars suscription1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":370,"y":140,"wires":[["9442164bb3990b3f"]]},{"id":"9442164bb3990b3f","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"QWKID","pt":"flow","to":"another1","tot":"str"},{"t":"set","p":"BBSHOST","pt":"flow","to":"another2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":140,"wires":[[]]},{"id":"e8ba3ef5.22f4a8","type":"mqtt-broker","name":"testb","broker":"192.168.1.25","port":"1883","clientid":"node-red-test","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

I just went with the lazy and highly portable method of using sbbs/+/+/whatever...

Your method does look like complete overkill for what I need.

It does seem to highlight that a better way (easy) was on using flow variables in a topic could be devised though.

The problem with using flow variables is: what does the MQTT node do when you change the flow variable? does it unsubscribe the old topic and subscribe the new topic?

Firstly there is currently no means of know when a flow variable was changed by some other node.
Secondly, even if we could, we would need to know the old value to unsubscribe.
Lastly, this would promote the use of flow/global context which is (in pure programming terms) not a good thing. It is akin to using global variables in a C/C++ program where a value changes "somewhere" and affects operations "elsewhere" - difficult to bug fix.

You options are - wild card or dynamic subscriptions.

  • wild card: inefficient since you might use 1% of the data incoming.
  • dynamic: efficient but is slightly harder to do.

I have no way of knowing your needs. I answered your question

I know of no other way to use flow vars in a mqtt subscription.
Glad you have a solution to your needs though.

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