# MQTT wildcards, and how to read them?

**URL:** <https://discourse.nodered.org/t/mqtt-wildcards-and-how-to-read-them/2948>\
**Category:** General\
**Created:** [7 September 2018 15:25 UTC](https://discourse.nodered.org/t/mqtt-wildcards-and-how-to-read-them/2948 "2018-09-07T15:25:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Teunis](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@Teunis](https://discourse.nodered.org/u/Teunis)\
**Post date:** [7 September 2018 15:25 UTC](https://discourse.nodered.org/t/mqtt-wildcards-and-how-to-read-them/2948/1 "2018-09-07T15:25:32Z")

</div>

Hey all!

I'm trying the following...

MQTT input subscribed to /some/topic/+/state

...and I need to get the wildcard (+) name and the value of the topic. This name hold the name of a sensor and becouse I have loooots of sensors I want to keep it simple and parse them all the same way.

When I attach a debugger to the node, I can see the complete topic, but I just need the wildcard part.

```
/some/topic/13245/state : msg.payload : string[1]
"0"

```

So I need to get the "12345" part as a separate msg or variable.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 September 2018 15:34 UTC](https://discourse.nodered.org/t/mqtt-wildcards-and-how-to-read-them/2948/2 "2018-09-07T15:34:29Z")

</div>

You will need to extract the info from the string.

A change node with either a regex or JSONata will do the job. Split the string on "/" then from the resulting array, you need element 2.

---

<div class="post-metadata">

**Author:** ![Teunis](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@Teunis](https://discourse.nodered.org/u/Teunis)\
**Post date:** [7 September 2018 16:00 UTC](https://discourse.nodered.org/t/mqtt-wildcards-and-how-to-read-them/2948/3 "2018-09-07T16:00:34Z")

</div>

Thanks for the tip! Got as far as splitting some stuff up, but I can't seem to get the regex part right.  
If I use a split, I get many messages, and need a way to discard all but the Nth of them.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [7 September 2018 16:15 UTC](https://discourse.nodered.org/t/mqtt-wildcards-and-how-to-read-them/2948/4 "2018-09-07T16:15:05Z")

</div>

no don't use a split node.... you need a change node as Julian said.  
(or simple function node with `msg.payload = msg.topic.split("/")[3];` I think (as you have a leading /

---

<div class="post-metadata">

**Author:** ![Teunis](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@Teunis](https://discourse.nodered.org/u/Teunis)\
**Post date:** [7 September 2018 21:07 UTC](https://discourse.nodered.org/t/mqtt-wildcards-and-how-to-read-them/2948/5 "2018-09-07T21:07:09Z")

</div>

That seems to be the best solution, I guess it's also faster than using a regex?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 September 2018 07:22 UTC](https://discourse.nodered.org/t/mqtt-wildcards-and-how-to-read-them/2948/6 "2018-09-08T07:22:01Z")

</div>

A Change node will be more efficient than a simple function node, as function nodes have a significant inherent overhead. However it is most unlikely that you need to worry about efficiency unless you are trying to handle thousands of messages per second, so do whatever you feel most comfortable with.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 September 2018 16:20 UTC](https://discourse.nodered.org/t/mqtt-wildcards-and-how-to-read-them/2948/7 "2018-09-08T16:20:27Z")

</div>

> [@Teunis](#):
>
> I guess it's also faster than using a regex?

I theory but I very much doubt it is enough to spot.

It would be interesting if someone had time to do a test of the different ways - but sadly, not enough time for me to do it.
