# Filter incoming MQTT messages with same topic

**URL:** <https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932>\
**Category:** General\
**Tags:** function-node, mqtt\
**Created:** [30 December 2022 08:44 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932 "2022-12-30T08:44:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![elmo2002](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@elmo2002](https://discourse.nodered.org/u/elmo2002)\
**Post date:** [30 December 2022 08:44 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/1 "2022-12-30T08:44:24Z")

</div>

Hi, I am trying to filter incoming MQTT messages that all have the same topic, however different payload objects.

I have a rtl\_433 that i am using to pick up the signal from my outdoor weather station. I then wish to take this data and display it.

The problem that i am having is that i am also receiving data from a solar controller so am unable to just set msg.payload to msg.payload.temperature for example, as both the solar controller and the weather station has this in the object.

My thought has been to only pass payloads that contain the payload object "model" : "Bressier".  
All i seem to be able to do is pass only the model data and not the rest of the objects in that payload. However this is where my knowledge has ran out and i am struggling and would like some guidance.

Sorry if i have not explained myself well.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 December 2022 08:47 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/2 "2022-12-30T08:47:52Z")

</div>

Hi and welcome to the forum.

Quick question/observation:  
the `topic` should be all you need to _filter_ the messages.

The rtl\_433 should be sending on a specific channel.

So the `MQTT IN` node should have that `topic` set so ONLY those messages from that device are received.

So really: no _filtering_ should be needed.

How ever: Of those OTHER devices are sending messages on the same `topic`, that's where your problem starts/is.

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [30 December 2022 08:49 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/3 "2022-12-30T08:49:05Z")

</div>

Could you send a sample of data from a Debug node so that a complete MQTT message can be seen?

It would help people understand what you are trying to explain.

---

<div class="post-metadata">

**Author:** ![dh3jhz](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dh3jhz](https://discourse.nodered.org/u/dh3jhz)\
**Post date:** [30 December 2022 08:51 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/4 "2022-12-30T08:51:18Z")

</div>

Hi!  
Just simply use a "Switch" Node and test if the key is existing!

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/427f8cd990f878b192b8d0246419f7b3e8ef113d.png)

---

<div class="post-metadata">

**Author:** ![elmo2002](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@elmo2002](https://discourse.nodered.org/u/elmo2002)\
**Post date:** [30 December 2022 08:53 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/5 "2022-12-30T08:53:05Z")

</div>

Hi, Thank you and thank you for your quick reply.

I understand what you mean as everywhere i have looked, read and watched all the MQTT topics seem to be unique to the device for example rtl\_433/Marlec-solar or rtl\_433/Honda-Remote. But if you look at the attached screen shot that i have just taken that is not the case. The unique identifier seem to be in the payload object "Model"

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/4/e483063ea5ea169f518a79938ac500f6039c4585.png)

---

<div class="post-metadata">

**Author:** ![elmo2002](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@elmo2002](https://discourse.nodered.org/u/elmo2002)\
**Post date:** [30 December 2022 08:55 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/6 "2022-12-30T08:55:39Z")

</div>

Hi, I have included a sample above which should help explain. Unfortunately the sample does not contain the weather station data.

---

<div class="post-metadata">

**Author:** ![dh3jhz](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dh3jhz](https://discourse.nodered.org/u/dh3jhz)\
**Post date:** [30 December 2022 08:57 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/7 "2022-12-30T08:57:32Z")

</div>

As I wrote before, just use the "switch" node to add a filter that only let the payload pass that have an explicit key! Wait until you have a payload in the debug from your wheather station and find an unique key. Then filter it!

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [30 December 2022 09:02 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/8 "2022-12-30T09:02:34Z")

</div>

eg. `msg.payload.model` are unique in the above messages.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 December 2022 09:04 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/9 "2022-12-30T09:04:56Z")

</div>

But the `topic` should also be unique to THAT device.

So any/ALL of the messages it sends will have that unique topic.

So again: why do you need to filter them?

Can you (@elmo2002 ) also post a message you do NOT want?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 December 2022 09:07 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/10 "2022-12-30T09:07:35Z")

</div>

> [@elmo2002](#):
>
> Hi, I have included a sample above which should help explain. Unfortunately the sample does not contain the weather station data.

So that raises a question:

Shouldn't it have weather data in it?

How do you know that is a message that comes from the weather station?  
Just you are saying you are getting incorrect messages that are NOT weather messages.

So how do you know that is a valid message?  
It could be one of those unwanted ones.

---

<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:** [30 December 2022 09:08 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/11 "2022-12-30T09:08:04Z")

</div>

Andrew the rtl device is a radio receiver. It doesn’t know what things are sending what messages apart from what is in the received payload. So we need to identify something unique in the payload first before we can assign that to the topic.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 December 2022 09:08 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/12 "2022-12-30T09:08:42Z")

</div>

Oh, sorry. My mistake.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 December 2022 09:12 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/13 "2022-12-30T09:12:06Z")

</div>

So - for people like me:

You have WiFi devices that all talk back to the `rtl` device - which is the WiFi receiver.  
Then ALL packets get sent to a MQTT message.

Then after being received by NR (and MQTT) you need to route the messages depending on something in their message.

Yes?

---

<div class="post-metadata">

**Author:** ![elmo2002](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@elmo2002](https://discourse.nodered.org/u/elmo2002)\
**Post date:** [30 December 2022 09:12 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/14 "2022-12-30T09:12:32Z")

</div>

The `topic` does not seem to be unique for each device. If you look in the above screen shot then both `topic`'s are rtl\_433.

> Can you (@elmo2002 ) also post a message you do NOT want?

If you take the above screen shot as an example i would like to pass all payload objects for the "Marlec-Solar" but nothing for the "Honda-CarRemote"

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 December 2022 09:12 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/15 "2022-12-30T09:12:54Z")

</div>

You can use a switch node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82b6181c22c27d3ece157c871e88b68e7cdc3fa2.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 December 2022 09:14 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/16 "2022-12-30T09:14:48Z")

</div>

@elmo2002

Could you post a message from both the solar device and a message from the weather device?

---

<div class="post-metadata">

**Author:** ![elmo2002](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@elmo2002](https://discourse.nodered.org/u/elmo2002)\
**Post date:** [30 December 2022 09:19 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/17 "2022-12-30T09:19:17Z")

</div>

The `rtl` device is a radio receiver that listens to all transmissions on a certain frequency and then converts the received data into a usable format like my screenshot above.

> Then after being received by NR (and MQTT) you need to route the messages depending on something in their message.

That is correct, When i receive a message where the "Model" : "Bresser-5in1" is found in the payload, I wish to forward the entire contents of that payload on

Sorry i haven't included a screen shot of the debug for the weather station, it seems to be a little temperamental in sending data at the moment.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 December 2022 09:21 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/18 "2022-12-30T09:21:27Z")

</div>

No problem.

I think there are enough people helping you here now.

Yes, it seems you need to use the `switch` node to _filter_ the messages.  
I don't want to start a _pissing contest_ to who is the better one to help.  
I got it wrong when I replied and didn't understand the problem.

---

<div class="post-metadata">

**Author:** ![Daniel8535](https://avatars.discourse-cdn.com/v4/letter/d/5fc32e/32.png) [@Daniel8535](https://discourse.nodered.org/u/Daniel8535)\
**Post date:** [30 December 2022 09:49 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/19 "2022-12-30T09:49:57Z")

</div>

An MQTT Topic is created as a UTF-8 string with one or more topic levels.Each Topic levels are seperated by a forward slash.

---

<div class="post-metadata">

**Author:** ![elmo2002](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@elmo2002](https://discourse.nodered.org/u/elmo2002)\
**Post date:** [30 December 2022 09:57 UTC](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932/20 "2022-12-30T09:57:34Z")

</div>

I had thought of this but thought that it would only pass the model part of the payload and not all of the objects.

However in trials with using the "Honda-CarRemote" it passes all of objects so think i have a solution.

Many thanks to all that replied and so quickly. Its much appreciated and i have defiantly learnt from this.

[Next page](https://discourse.nodered.org/t/filter-incoming-mqtt-messages-with-same-topic/72932.md?page=2)
