# Switch to Filter Messages followed by Join

**URL:** https://discourse.nodered.org/t/switch-to-filter-messages-followed-by-join/58465
**Category:** General
**Created:** [16 February 2022 13:36 UTC](https://discourse.nodered.org/t/switch-to-filter-messages-followed-by-join/58465 "2022-02-16T13:36:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lorccan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lorccan](https://discourse.nodered.org/u/lorccan)
#### Post date: [16 February 2022 13:36 UTC](https://discourse.nodered.org/t/switch-to-filter-messages-followed-by-join/58465/1 "2022-02-16T13:36:59Z")

</div>

Two nodes "A" and "B" emit payload.MotionDetected with a value of either 1 or 0 and a Function node adds a timestamp, payload.Time, with Date.now() and a Change node applies a Topic (to differentiate A from B).

A Switch then filters these messages, allowing through only MotionDetected values of 1. These are then Joined to create a key/value object using Topic as the key.

The goal is to compare the timestamps to see which is greater (happened most recently).

The comparison in a Function node is trivial, but I am unsure how I can ensure I only act when I have a pair of messages from BOTH - rather than a pair of messages from ONE.

I'm thinking of discarding a message from A that is not accompanied by one from B within a certain timeout OR if the next message was also from A to discard the first (and continue waiting for one from B) and vice-versa. I think I need something like a reverse deadband RBE node somewhere, but I can't get my head around this (whereas the act of drafting a question usually clarifies it for me)

Any ideas/help gratefully acceppted!

---

<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: [16 February 2022 14:04 UTC](https://discourse.nodered.org/t/switch-to-filter-messages-followed-by-join/58465/2 "2022-02-16T14:04:19Z")

</div>

Can you show a screenshot of how you have configured the Join node please? You can paste an image here.

---

<div class="post-metadata">

### Author: ![lorccan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lorccan](https://discourse.nodered.org/u/lorccan)
#### Post date: [16 February 2022 14:16 UTC](https://discourse.nodered.org/t/switch-to-filter-messages-followed-by-join/58465/3 "2022-02-16T14:16:47Z")

</div>

Here it is.  
 ![Screenshot 2022-02-16 at 14.15.37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e9af91261158fcdd80de2df2968fbec602fd28c.png)

---

<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: [16 February 2022 14:42 UTC](https://discourse.nodered.org/t/switch-to-filter-messages-followed-by-join/58465/4 "2022-02-16T14:42:41Z")

</div>

> [@lorccan](#):
>
> rather than a pair of messages from ONE

You will never get that with the configuration you have, as you have said it has to receive messages with two different topics.  
However, if useful, you could also specify a timeout time then if two messages arrive within that time then they will be passed on, but if only one arrives in that time then it will be passed on anyway, and you can check in your function which one(s) are present and take appropriate action.

---

<div class="post-metadata">

### Author: ![lorccan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lorccan](https://discourse.nodered.org/u/lorccan)
#### Post date: [16 February 2022 14:46 UTC](https://discourse.nodered.org/t/switch-to-filter-messages-followed-by-join/58465/5 "2022-02-16T14:46:31Z")

</div>

Ah! So as the key has to be unique it dictates that the topics need to be different.

I'll try the timeout suggestion and see how far I get. Thank you.

---

<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: [17 April 2022 14:46 UTC](https://discourse.nodered.org/t/switch-to-filter-messages-followed-by-join/58465/6 "2022-04-17T14:46:44Z")

</div>

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