Anyone know a way to do a dynamic (mqtt) specification subscriber topic specific?

Anyone know a way to do a dynamic (mqtt) specification subscriber topic specific? This not using # or + if the subscriber, at least I don't think so, I will xplain.

I want the subscriber to be specific, thus the flow to self-configurable on initial injection. You can do this with publisher topics, you can in a junction before the 'mqtt out' node, grab the host name, and create a static publisher that is host specific, for example, "//test" becames "/IamAhost/test" initial inject a flow initialization. This is because 'mqtt out' accepts an input. The flows I create, self configure, so I can import a flow, to any node-red instance, and avoid static assignments. So I don't have to use # or + qualifiers in subscribers, and parse the data received, unless that is exactly what is desired of course.

For 'mqtt in' states in its help notes that msg.topic if set will be honored, like in 'mqtt out', but I can't seem to get it to work. I think the documentation is wrong, or only really applies to 'mqtt out'.

Here is one way to self-configure a publisher...

[{"id":"b8757881.fcde68","type":"mqtt out","z":"1463b977.70bf97","name":"Pi4ModelB0 (/<Host>/revision","topic":"","qos":"","retain":"","broker":"98b1bc1f.465ac8","x":1390,"y":580,"wires":[]},{"id":"ddd8b49b.02c84","type":"function","z":"1463b977.70bf97","name":"Topic (/<Host>/revision)","func":"msg.topic = \"/\" + global.get('host') + \"/revision\";\nmsg.payload = msg.payload.toString().trim();\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1110,"y":580,"wires":[["b8757881.fcde68"]]},{"id":"98b1bc1f.465ac8","type":"mqtt-broker","z":"","name":"Pi4ModelB0","broker":"pi4modelb0.dachshund-digital.org","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

I want to do the same idea with a subscriber configuration...

Where does it say that? It hasn't got an input so how can you send it a topic?
I believe all you can do is to use wildcards and then a Switch node or similar to select the messages you want.

the default node does not allow dynamic subscriptons - (yet).
Meanwhile there is a contrib node - https://flows.nodered.org/node/node-red-contrib-mqtt-dynamictopic

Not yet... :slight_smile: Thanks for the pointer!

Ok, this is weird... I load the module from the palette manager, no errors. Restart node red, no issues. I bring up the editor, no new nodes. I look at the palette manager, it is there, the new module. Has an alert icon, which I suspect means one of the (new) nodes needs configuration, which makes sense. But I can't seem to get the new node types to appear in the editor. I do a npm list command... the new module package is there.

 $ npm list | grep dynamictopic
├─┬ node-red-contrib-mqtt-dynamictopic@1.0.2

But I cannot seem to use it, let alone find it, in the editor.

Update: Here is the log notification of why the load of new nodes is failing.

13 Jul 18:00:16 - [warn] Failed to enable node:
13 Jul 18:00:16 - [warn]  - mqtt-dynamic : Error: Cannot find module '../node_modules/mqtt'
Require stack:
- /home/pi/.node-red/node_modules/node-red-contrib-mqtt-dynamictopic/lib/mqtt.js
- /home/pi/.node-red/node_modules/node-red-contrib-mqtt-dynamictopic/lib/mqttConnectionPool.js
- /home/pi/.node-red/node_modules/node-red-contrib-mqtt-dynamictopic/mqtt-dynamic.js
- /usr/lib/node_modules/node-red/node_modules/@node-red/registry/lib/loader.js
- /usr/lib/node_modules/node-red/node_modules/@node-red/registry/lib/index.js
- /usr/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/index.js
- /usr/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/index.js
- /usr/lib/node_modules/node-red/lib/red.js
- /usr/lib/node_modules/node-red/red.js

Well the plot thickens...

$ npm list mqtt
node-red-project@0.0.1 /home/pi/.node-red
└─┬ node-red-contrib-mqtt-dynamictopic@1.0.2
  └── mqtt@0.3.13

Given the above module is DOA, I used the cheap route, Using '+' wildcard in the mqtt topic, I just have the flow check if the publisher is its-self, the do work, else ignore. This is not efficient, but gets the job done, such that I have one flow I can deploy across many devices and not have to hardcode the mqtt subscriber topic on each device.

I might try to figure out how to edit the flow file as a text/json file from a flow, to create the topic specification on initial node red start, but then if the flow ever changes over time, I could break something.

you can of course also use environment variables in a config field (like ${MY_ENV_VAR} ) which is only read at deploy/start time but does allow for different configs on different devices.

Yes, I saw that option as well. What I have works, and will be easy to replace once a more elegant option materializes. As I am typing this, I am thinking about the ENV variable option to avoid some unneeded or unwanted mqtt traffic.

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