# Is there an easy way to enumerate MQTT publishing devices

**URL:** <https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444>\
**Category:** Dashboard\
**Tags:** node-red-dashboard\
**Created:** [1 October 2022 09:51 UTC](https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444 "2022-10-01T09:51:21Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![mike1369](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mike1369](https://discourse.nodered.org/u/mike1369)\
**Post date:** [4 October 2022 09:01 UTC](https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444/21 "2022-10-04T09:01:54Z")

</div>

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.

---

<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:** [4 October 2022 09:05 UTC](https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444/22 "2022-10-04T09:05:40Z")

</div>

> [@mike1369](#):
>
> I am an embedded developer, not at all familiar with javascript

ok, then while I am at it...

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

```

is _more_ "correct" 😉

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 October 2022 09:58 UTC](https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444/23 "2022-10-04T09:58:23Z")

</div>

> [@mike1369](#):
>
> Finally got something working using global settings (I hate globals ...

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

 ![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cd94f9c1a06c53925f552e7d9b563a9ab8aeae1f.jpeg)

```auto
[{"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"

---

<div class="post-metadata">

**Author:** ![mike1369](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mike1369](https://discourse.nodered.org/u/mike1369)\
**Post date:** [4 October 2022 10:13 UTC](https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444/24 "2022-10-04T10:13:52Z")

</div>

OK, many thanks for your advices 😉

---

<div class="post-metadata">

**Author:** ![mike1369](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mike1369](https://discourse.nodered.org/u/mike1369)\
**Post date:** [4 October 2022 13:29 UTC](https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444/25 "2022-10-04T13:29:02Z")

</div>

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.

---

<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:** [4 October 2022 13:49 UTC](https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444/26 "2022-10-04T13:49:40Z")

</div>

> [@mike1369](#):
>
> 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

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.

---

<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:** [3 November 2022 13:49 UTC](https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444/27 "2022-11-03T13:49:51Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/is-there-an-easy-way-to-enumerate-mqtt-publishing-devices/68444.md?page=1)
