Is there an easy way to enumerate MQTT publishing devices

Thanks a lot Steve.
I am an embedded developer, not at all familiar with javascript
I changed it to "null" as you recommend (versus "NULL")
Actually (don't ask me why) both versions give something functional, but definitely I keep yours.

ok, then while I am at it...

const topic = global.get("DeviceId");
if (msg.topic === topic) {
    return msg;
}
return null;

is more "correct" :wink:

Well done for getting it working but you really don't need to use global, or any other context variable.

Try using @Steve-Mcl's function, without any context variables, like this

[{"id":"1c6f86d369486d13","type":"mqtt in","z":"9372feccca70ce91","name":"","topic":"","qos":"2","datatype":"auto-detect","broker":"53e05e1941cf7e1e","nl":false,"rap":true,"rh":0,"inputs":1,"x":470,"y":260,"wires":[["60c7e8f51e4f0663"]]},{"id":"c0a6a8a4469646d7","type":"ui_dropdown","z":"9372feccca70ce91","name":"","label":"Device","tooltip":"","place":"Select option","group":"3c297a489db1e930","order":2,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"Input 1","value":"sensor/input1","type":"str"},{"label":"Input 2","value":"sensor/input2","type":"str"}],"payload":"","topic":"topic","topicType":"msg","className":"","x":90,"y":280,"wires":[["86a8c2a5ba8960c9"]]},{"id":"60c7e8f51e4f0663","type":"ui_text","z":"9372feccca70ce91","group":"3c297a489db1e930","order":3,"width":0,"height":0,"name":"","label":"MQTT Data ","format":"{{msg.payload}}","layout":"row-spread","className":"","x":610,"y":260,"wires":[]},{"id":"86a8c2a5ba8960c9","type":"function","z":"9372feccca70ce91","name":"Unsubscribe, Subscribe","func":"node.send({ topic: true, action: \"unsubscribe\" });\n\nmsg.topic = msg.payload\nnode.warn(\"About to subscribe to \" + msg.topic)\nmsg.payload = \"\"\nmsg.action = \"subscribe\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":280,"wires":[["1c6f86d369486d13","e50bd21e93e64769"]]},{"id":"e50bd21e93e64769","type":"ui_text","z":"9372feccca70ce91","group":"3c297a489db1e930","order":3,"width":0,"height":0,"name":"","label":"Subscribed topic","format":"{{msg.topic}}","layout":"row-center","className":"","x":620,"y":300,"wires":[]},{"id":"53e05e1941cf7e1e","type":"mqtt-broker","name":"GlassPi","broker":"192.168.1.11","port":"1883","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":{},"userProps":"","sessionExpiry":""},{"id":"3c297a489db1e930","type":"ui_group","name":"Demo","tab":"45d37c7fa1edec02","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"45d37c7fa1edec02","type":"ui_tab","name":"Demo","icon":"dashboard","disabled":false,"hidden":false}]

I used a dropdown to select the device ID because a) you said you already know all the values, so no need to type it in every time and b) a dropdown can show for example "Thing No 7" but return the complete topic "sensors/PG0600642"

1 Like

OK, many thanks for your advices :wink:

Hello jbudd,

I am facing an issue where the mqtt node connects, then disconnect right after.
If I don't set the "dynamic subscription", connection keeps OK

I have to leave that apart for now, I have to focus on the embedded software; so even if that is ugly, I will keep the globals so far.
But yes your implementation is much much nicer, so I will try to have it working later on.

Thanks for your help.

That can happen if you subscribe to a bad topic or have "disconnect" in the action property.

It really isnt that hard! Just use debug nodes (set to show the complete message) to inspect what is going INTO the MQTT-In node.

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