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

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) and it outputs MQTT

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

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

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

https://flows.nodered.org/flow/e38554543a8cdec6c44eacfc68a0c149

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

2 Likes

You could use

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

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') ...

1 Like

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

{"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;

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.

1 Like

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'.

1 Like

Hello, Thanks for all the replies very helpful !.

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 :nerd_face:. 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 :wink:

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.

2 Likes

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. :slight_smile:

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

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

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