# Wildcard topic in function (If topic == /xxx/#)

**URL:** <https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218>\
**Category:** General\
**Created:** [5 January 2023 11:26 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218 "2023-01-05T11:26:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![myozone](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myozone/32/8904_2.png) [@myozone](https://discourse.nodered.org/u/myozone)\
**Post date:** [5 January 2023 11:26 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/1 "2023-01-05T11:26:44Z")

</div>

I'm trying to figure out how can I wildcard a topic, what I'm trying to do is filter off specific topics from rtl\_433 in a wildcard fashion.

RTL\_433 is a described here [GitHub - merbanan/rtl\_433: Program to decode radio transmissions from devices on the ISM bands (and other frequencies)](https://github.com/merbanan/rtl_433) and it outputs MQTT

My code I have is below, and the MQTT that feeds it is subscribed to rtl\_433/#

```auto
if (msg.topic == "rtl_433/Ford/#")
{
    msg.payload = "blink";
}
 else
{
   msg.payload = "off";
}
return msg; 

```

But it fails ...

I would like to do the filtering via a function rather than have a MQTT feed set to rtl\_433/Ford/# which is the obvious way of doing it

---

<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:** [5 January 2023 11:35 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/2 "2023-01-05T11:35:49Z")

</div>

I wrote a subflow for this type of thing...

[https://flows.nodered.org/flow/e38554543a8cdec6c44eacfc68a0c149](https://flows.nodered.org/flow/e38554543a8cdec6c44eacfc68a0c149)

However, if you are using MQTT why not simply subscribe to the right topic in the first place?

---

<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:** [5 January 2023 11:59 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/3 "2023-01-05T11:59:17Z")

</div>

You could use

```auto
if (msg.topic.startsWith("rtl_433/Ford/")) {
    msg.payload = "blink";
}

```

---

<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:** [5 January 2023 12:10 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/4 "2023-01-05T12:10:11Z")

</div>

> [@myozone](#):
>
> `if (msg.topic == "rtl_433/Ford/#")`

If your mqtt-in node is subscribed to `rtl_433/Ford/#` then your if statement isn't useful since everything would have a variation of that topic anyway. But if you were subscribed to `rtl_433/#` then jbudd's suggestion is the one to use.

Something that I often do as an alternative is to split the topic using `let splitTopic = msg.topic.split('/')` which then gives you easy access to each element of the topic for further processing. Then you could simply say `if ( splitTopic[1] === 'Ford') ... `

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [5 January 2023 20:48 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/5 "2023-01-05T20:48:27Z")

</div>

If you are using the standard rtl\_433 MQTT output ( `-F "mqtt://localhost:1883,user=USERNAME,pass=PASSWORD,retain=0,devices=rtl_433"` the MQTT input topic you are looking at is `rtl_433` with a payload something like

```auto
{"time":"2023-01-05 20:31:38","model":"Oregon-PCR800","id":89,"channel":0,"battery_ok":1,"rain_rate_in_h":0,"rain_in":0}

```

so for your selection of `rtl_433/Ford` you would need a change node to add `/model` to the topic. Looking for `rtl_433/Ford` would then work.

You would probably be better off also adding the `id` (`rtl_433/<id>/<model`) as otherwise if any of your neighbours have a similar device it will register in your flow.

If you want to use a function you would have to have something like;

```auto
if (msg.topic == "rtl_433" && msg.payload.model === 'Ford')
   
{
    msg.payload = "blink";
}
 else
{
   msg.payload = "off";
}
return msg; 

```

but to be honest, you would be much better off setting the topic up as above and using that to do any filtering using @TotallyInformation s suggestion.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 January 2023 10:19 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/6 "2023-01-06T10:19:46Z")

</div>

You could also add a `switch` node after the `mqtt-in` node and test msg.topic to see if it contained `rtl_433/Ford`. If it also contained an `otherwise` option, you could send the outputs to two change nodes to set msg.payload to 'blink' or 'off'.

---

<div class="post-metadata">

**Author:** ![myozone](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myozone/32/8904_2.png) [@myozone](https://discourse.nodered.org/u/myozone)\
**Post date:** [7 January 2023 10:29 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/7 "2023-01-07T10:29:24Z")

</div>

Hello, Thanks for all the replies very helpful !.

---

<div class="post-metadata">

**Author:** ![myozone](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myozone/32/8904_2.png) [@myozone](https://discourse.nodered.org/u/myozone)\
**Post date:** [11 January 2023 11:43 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/8 "2023-01-11T11:43:49Z")

</div>

Just thought I'd add

The RTL dongle is just a cheap clone, mine has a blue case, but any cheap RTL SDR dongle should work no need for anything expensive. Indeed, it opens a whole new world of monitoring neighbours weather stations, car tyre pressures etc,etc all in Node Red 🤓. The RTL SDR can of course, do MUCH more AIS ships, ADSB planes etc. I use it in Node-Red in Docker/Portainer with the command -C si -F mqtt://192.168.2.40:1883,events=rtl\_433[/model][/id] which puts ALL the received data into MQTT

I graph some of my neighbours weather station data in Grafana and Node red, probably better than their own displays 😉

---

<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:** [11 January 2023 11:57 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/9 "2023-01-11T11:57:31Z")

</div>

I keep thinking I should find out about software defined radios and raspberry pies.

It would be interesting to see your system described in Share Your Projects.

---

<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:** [11 January 2023 22:01 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/10 "2023-01-11T22:01:50Z")

</div>

If my RFXtrx433e ever fails, perhaps this will be my next device. Though I have to say that that device has been rock solid for the best part of a decade now. 🙂

---

<div class="post-metadata">

**Author:** ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)\
**Post date:** [12 January 2023 06:54 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/11 "2023-01-12T06:54:56Z")

</div>

Mine too. But it looks like there are new devices out there (I am particularly interested in Oregon thermos/hygros) which are not supported.

---

<div class="post-metadata">

**Author:** ![myozone](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myozone/32/8904_2.png) [@myozone](https://discourse.nodered.org/u/myozone)\
**Post date:** [12 January 2023 16:16 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/12 "2023-01-12T16:16:51Z")

</div>

It's actually running on Proxmox \> Debian \> Docker on a tiny 1L PC, my Pi2's are too loaded for much more and I'm slowly moving some things over to the new box. I've had the RTL SDR dongle for ages plugging it in my laptop for a bit of SDR radio ADSB and SDR#. But a while ago I came across RTL\_433 but never really played with until a few months ago. It really is easy to set up the difficult bit is finding a place for it with it's antenna !

Currently it hides behind the sofa and the dongle hangs of the door frame with the USB cable down to the PC

---

<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:** [26 January 2023 16:16 UTC](https://discourse.nodered.org/t/wildcard-topic-in-function-if-topic-xxx/73218/13 "2023-01-26T16:16:57Z")

</div>

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