# Switch node - MQTT like wildcards for switching

**URL:** <https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337>\
**Category:** Feature Requests\
**Created:** [27 May 2021 11:54 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337 "2021-05-27T11:54:50Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [27 May 2021 11:54 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/1 "2021-05-27T11:54:50Z")

</div>

Hi all, I am canvasing opinion on switch node switching with MQTT-like wildcards

@bartoszek made an interesting point...

> [@Message structure convention](https://discourse.nodered.org/t/message-structure-convention/46324/1):
>
> And now... I'm confused because if this is a Nore-RED way of sending messages, why there is no builtin mechanism of routing messages by topic like for example in MQTT? (a switch node which outputs the message on all fitting topics, not only one)

> [@Message structure convention](https://discourse.nodered.org/t/message-structure-convention/46324/4):
>
> I know that there are some mechanisms in switch node itself to check the structure of a topic, but still they are not so straightforward as simple #+ wildcards

Obviously we could use JSONata or regex to simulate this functionality but I think his point is valid. If we are using topics and have MQTT wildcard features for MQTT, it would seem like the obvious thing to add _ **MQTT-like** _ wildcard filtering to the switch node?

Something like...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f6e0fa4cd1c7091e925bb1ab7cfb7df44f559bb.png)

What are peoples thoughts on this?

What would the dropdown entry label be?

Is it worth it?

---

<div class="post-metadata">

**Author:** ![Hypnos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hypnos/32/7566_2.png) [@Hypnos](https://discourse.nodered.org/u/Hypnos)\
**Post date:** [27 May 2021 12:05 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/2 "2021-05-27T12:05:21Z")

</div>

With `wildcard` everyone also imagines something different. For example, I think of wildcard more about have a `*` as wildcard which stands for multiple characters and not the MQTT-like `#`.

I only think that this option should then be named a little more specifically.

On the other hand, I have been using the `contains` in the switch node for this type of filtering in the past.

---

<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:** [27 May 2021 12:08 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/3 "2021-05-27T12:08:57Z")

</div>

> [@Hypnos](#):
>
> On the other hand, I have been using the `contains` in the switch node for this type of filtering in the past.

Yes, me too, but `contains` doesnt do `startsWith` and cant handle `+` or `#`

> [@Hypnos](#):
>
> With `wildcard` everyone also imagines something different. For example, I think of wildcard more about have a `*` as wildcard which stands for multiple characters and not the MQTT-like `#` .

For me, `?` is 1 char, `*` is none or more of any char, '#' is a number etc etc - but you are completely right, eveeryone has there own idea of wildcards 😕

> [@Hypnos](#):
>
> I only think that this option should then be named a little more specifically.

Agreed. So perhaps the dropdown name would be something like `+# wildcard` or `MQTT filter` or `????`

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [27 May 2021 12:42 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/4 "2021-05-27T12:42:30Z")

</div>

Not sure what would be the benefit compared to `matches regex`. Am I missing something?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 May 2021 13:10 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/5 "2021-05-27T13:10:32Z")

</div>

> [@greengolfer](#):
>
> Not sure what would be the benefit compared to `matches regex`

It would be much easier to use.  
Regex is not that easy to format, and it's reputation puts off a lot of users.

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [27 May 2021 13:19 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/6 "2021-05-27T13:19:19Z")

</div>

That's true. But having multiple options to do almost the same thing might also be confusing when to stop using the "simple" one when to start using the "complex" one. I am no regex specialist, but [regex101.com](http://regex101.com) is my friend 🙂  
Putting a link to a simple regex tool could be an alternative to me.

---

<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:** [27 May 2021 14:00 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/7 "2021-05-27T14:00:57Z")

</div>

Hi @greengolfer

I feel the benefit is it matches MQTT topic filtering so anyone doing a primarily MQTT based solution has a sense of consistency.

I know regex fairly well but it is not for beginners. Nor is JSONata.

---

<div class="post-metadata">

**Author:** ![Hypnos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hypnos/32/7566_2.png) [@Hypnos](https://discourse.nodered.org/u/Hypnos)\
**Post date:** [27 May 2021 14:14 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/8 "2021-05-27T14:14:11Z")

</div>

> [@Steve-Mcl](#):
>
> Agreed. So perhaps the dropdown name would be something like `+# wildcard` or `MQTT filter` or `????`

`MQTT filter` or `Topic filter` doesn't sound bad

---

<div class="post-metadata">

**Author:** ![bartoszek](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@bartoszek](https://discourse.nodered.org/u/bartoszek)\
**Post date:** [27 May 2021 14:14 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/9 "2021-05-27T14:14:27Z")

</div>

@greengolfer please remember that An unescaped delimiter must be escaped with a backslash () so a simple query would look like `home\/.+\/temperature\/status` compared to `home/+/temperature/status`

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 May 2021 14:23 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/10 "2021-05-27T14:23:42Z")

</div>

`Topic filter` could be confusing, but `MQTT filter` or `MQTT topic` would be clear (using the word 'MQTT' makes it's intended use clear, and leaves the regex option open for more advanced users).

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 May 2021 14:31 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/11 "2021-05-27T14:31:56Z")

</div>

Topic or MQTT filter is confusing as it implies it's for topic payloads or, has something to do with MQTT as it could just be used for path filtering, without MQTT. Maybe PATH Filter. Also escaping the / is also pron to errors and confusion.  
[edit The ability to pass on the # or + match would be neat to]

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [27 May 2021 14:33 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/12 "2021-05-27T14:33:33Z")

</div>

Perhaps _ **"matches pattern"** _ , and such a pattern can contain wildcards. Similar to the "matches regex" option... Although a regex is also a pattern of course...

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [27 May 2021 15:05 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/13 "2021-05-27T15:05:49Z")

</div>

My thoughts is - on the useful feature request scale of 1-10 is it anymore than a 1?

Do you have an actual use case for it?

99.9% of time - contains "xxx/yyy/" will handle xxx/yyy/# so only needed for xxx/+/yyy

PS but @BartButenaers suggestion of "matches pattern" seems good as a name for it

---

<div class="post-metadata">

**Author:** ![bartoszek](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@bartoszek](https://discourse.nodered.org/u/bartoszek)\
**Post date:** [27 May 2021 15:21 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/14 "2021-05-27T15:21:10Z")

</div>

IMHO this feature can be used in most use cases instead of regexp. For me personally, lack of this function was the sole reason why I dropped the use of topics in my project altogether, and probably all people that come from "embedded c" world would also try to avoid regexp  
The fact that it would be inline with MQTT is also worth noticing  
I think thit this should have lot higher grade then 1/10, maybe 3/10 😉

---

<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:** [27 May 2021 15:40 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/15 "2021-05-27T15:40:53Z")

</div>

To me - either it is an MQTT wildcard type filter or it isn't. If it is then it may as well be called an MQTT filter and accept just + and # as the wildcards correctly (eg foo/+/bar is ok - but foo/ab+c/bar is not) .

If not then it is something else and why wouldn't contains or regex etc do, as otherwise it's yet another syntax to learn. (and of course if someone wants to add a regex helper/tester then that could also be an option instead)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 May 2021 17:53 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/16 "2021-05-27T17:53:31Z")

</div>

> [@dceejay](#):
>
> To me - either it is an MQTT wildcard type filter or it isn't. If it is then it may as well be called an MQTT filter and accept just + and # as the wildcards correctly (eg foo/+/bar is ok - but foo/ab+c/bar is not)

Unless I've misread, that is exactly what has been suggested.  
It would be a useful addition, because it would widen the scope of using MQTT topics & wildcards, which I would suggest are not currently used to their full potential by most 'non-technical' users (me included) 👍

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [27 May 2021 18:19 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/17 "2021-05-27T18:19:25Z")

</div>

> [@Paul-Reed](#):
>
> by most 'non-technical' users

I must admit that I have never used mqtt filters. So when I should see an "mqtt filter" option, I assume I would never use it. I don't mean that I don't like the concept of the mqtt filter examples above, but I would not expect that it would offer me pattern matching via wildcards on any (non-mqtt) messages...

---

<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:** [28 May 2021 20:15 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/18 "2021-05-28T20:15:26Z")

</div>

> [@bartoszek](#):
>
> @greengolfer please remember that An unescaped delimiter must be escaped with a backslash () so a simple query would look like `home\/.+\/temperature\/status` compared to `home/+/temperature/status`

I think if you are going to do this, you need to get rid of the escapes. Otherwise you might as well use regex. Its only real purpose is ease of use and ease of learning. So it should work exactly like an MQTT topic filter as stated.

> [@cymplecy](#):
>
> on the useful feature request scale of 1-10 is it anymore than a 1?

I think I'd put it at a 5 myself. There are certainly times when I'd be happy to use it and it would certainly sometimes replace single, more complex, function nodes.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [28 May 2021 20:32 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/19 "2021-05-28T20:32:55Z")

</div>

> [@TotallyInformation](#):
>
> it would certainly sometimes replace single, more complex, function nodes.

Do you have a concrete example of when you've had to program a function node to switch on wildcard topics?

---

<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:** [28 May 2021 20:33 UTC](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/20 "2021-05-28T20:33:29Z")

</div>

Oh yes. Let me see if I can grab something.

[Next page](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337.md?page=2)
