# Help with logic filtering of message

**URL:** <https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076>\
**Category:** General\
**Created:** [17 March 2019 08:38 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076 "2019-03-17T08:38:42Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [17 March 2019 08:38 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/1 "2019-03-17T08:38:42Z")

</div>

Ok, rather than like my previous attempt (may be deleted now) here is the problem:

Two RPIs. They both ping the Gateway and another "real" IP address.  
They both report their results and publish them to MQTT.

The messages range from:  
OnLine, OffLine. (And if that RPI is powered down/turned off - in which case its messages are ignored.)

So.....

I am looking at "who's" message has higher weight than the other.  
If both machines are both sending **OnLine** messages, which is the one I show?  
If either sends **OffLine** then the other will get priority and it's message will be on-sent (that being OnLine.)

If **BOTH** are **OffLine** , then who's message is shown?

That's easy enough for me to determine, but please: Indulge me a bit more.

The above is (say) to the MODEM - say.  
Then there is the "uplink", as I call it.  
The above will need to be done to the message of the "uplink" status as well.

I'm stuck at how to do this: where both are the same.

Well, thinking about it: I think I don't really get the whole thing other than I concept of what it does. But no **real** understanding of how to do this.

Anyone - please?

---

<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:** [17 March 2019 09:07 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/2 "2019-03-17T09:07:29Z")

</div>

If you need to make decisions based on the current state of two variables then feed them both into a Join node set to Key/Value pair mode, identified by topic, wait for 2 messages and send on every new message after that, so you then get one message with both values in it each time either of them changes. Then you can perform whatever logic you want on the result.

---

<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:** [17 March 2019 09:13 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/3 "2019-03-17T09:13:06Z")

</div>

> [@Colin](#):
>
> feed them both into a Join node set to Key/Value pair mode

Thanks, but I can't find that setting in the `join` node.

All I can see is: Automatic, Manual and Reduce sequence.

---

<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:** [17 March 2019 09:37 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/4 "2019-03-17T09:37:31Z")

</div>

Having selected Manual, what other options do you see?

---

<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:** [17 March 2019 09:42 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/5 "2019-03-17T09:42:40Z")

</div>

Yeah, ok.......

But reading the info on the node it kind of says it is to be used with the `split` node.

It joins the two strings. I am missing how I can use this to assign weight to which message is shown.

Could you please expand on how this node is supposed to get two messages and "select" one of the two to on-send. Which one is not fixed. Either in message position or "who" sends it.

---

<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:** [17 March 2019 09:45 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/6 "2019-03-17T09:45:50Z")

</div>

Don't use string mode, use key/value mode (as I said, go back and read the original mail again).  
Once you have both of them you can look at them and send whatever you 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:** [17 March 2019 09:57 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/7 "2019-03-17T09:57:45Z")

</div>

I can't see:

> [@Colin](#):
>
> Join node set to Key/Value pair mode

a key/value object ?

> [@Colin](#):
>
> so you then get one message with both values in it each time either of them changes.  
> Then you can perform whatever logic you want on the result.

That's the part where I am stuck.

Preforming the logic.

---

<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:** [17 March 2019 09:59 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/8 "2019-03-17T09:59:59Z")

</div>

key/value object is one of the options.

Do you mean you don't even know _what_ you want to do, never mind _how_ to do it? Can't help much there I am afraid.

---

<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:** [17 March 2019 10:15 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/9 "2019-03-17T10:15:21Z")

</div>

Here is a basic example of what I am wanting to do:

Machine 1 sending: `Machine1 modem online`  
Machine 2 sending: `Machine2 modem online`

Great.  
For now, let's say I want Machine1's message to be weighted and sent.

Then the messages change to:  
`Machine1 modem offline`  
`Machine2 modem online`

Machine 2's message needs to be sent, as it is the online message.

But if it changes where machine1 is sending the online and machine2 is sending offline, machine 1's message is sent.

If both are offline, machine 1's message is sent.

So as best I can resolve it:

- If both online, machine 1 priority.
- If one of the machines shows offline (and the other online) the online is sent.
- If both are offline, machine 1 priority.

Does that help?

---

<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:** [17 March 2019 10:20 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/10 "2019-03-17T10:20:57Z")

</div>

Maybe you need something like [https://flows.nodered.org/node/node-red-contrib-nools](https://flows.nodered.org/node/node-red-contrib-nools) to establish some rules & priorities.

---

<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:** [17 March 2019 10:21 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/11 "2019-03-17T10:21:02Z")

</div>

Get the join working then you can easily do that in a function node.

---

<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:** [17 March 2019 10:37 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/12 "2019-03-17T10:37:09Z")

</div>

Colin,

Ok, with the `join` node working and set how you say I get alternate outputs of both machines with their messages.  
Though it (from my example above) I am getting:  
`Machine1 modem online`  
`Machine2 modem online`  
`Machine1 modem online`  
and so on.

(by edit)  
Ok, looked at it again a bit harder, but gee it is difficult to see the difference.  
(I am not getting it.)  
I can't see how that is useful.

Paul:  
Thanks.

That looks interesting.  
Seems I have to (maybe) learn about that language/syntax now too.

---

<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:** [17 March 2019 11:14 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/13 "2019-03-17T11:14:27Z")

</div>

You should see the payload from both messages identified by the topic you passed in with each. You did make sure they had different topics didn't you?

---

<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:** [17 March 2019 11:16 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/14 "2019-03-17T11:16:41Z")

</div>

Yes. That was the "oops" moment.

But I am not seeing much of an advantage.

I am (just now) setting contexts dependant on the messages.  
Now I am working on the logic on the context to what is then sent on as the message.

---

<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:** [17 March 2019 11:20 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/15 "2019-03-17T11:20:55Z")

</div>

So have you now got both messages in one? If so you can test them and send on what you 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:** [17 March 2019 11:24 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/16 "2019-03-17T11:24:39Z")

</div>

Colin, yes. But "testing them" is not something I understand.

This is what I have made in the mean time:

```auto
var BP = context.get('BedPi')||0; // Has data come in from BedPi?
var TP = context.get('TimePi')||0; // Has data come in from TimePi?

var new_message;
if (msg.topic == 'BedPi Modem')
{
    // BedPi message received.
    if (msg.state === true)
    {
        //
        context.set('BedPi',1);
        context.set('BedPi_message',msg.payload);
    } else
    {
        context.set('BedPi',0)
        context.set('BedPi_message',msg.payload);
    }
} else
if (msg.topic == 'TimePi Modem')
{
    // TimePi message received.
    if (msg.state === true)
    {
        //
        context.set('TimePi',1);
        context.set('TimePi_message',msg.payload);
    } else
    {
        context.set('TimePi',0)
        context.set('TimePi_message',msg.payload);
    }
}
// Now The contexts have been set, act depending on what is set.
if (TP == 1)
{
    //
    // Use TP status and display it.
    //
    msg.payload = context.get('TimePi_message');
    return msg;
} else
if (BP == 1)
{
    //
    // BedPi is online.
    //
    msg.payload = context.get('BedPi_message');
    return msg;
}

```

Is this looking ok?

---

<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:** [17 March 2019 11:30 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/17 "2019-03-17T11:30:21Z")

</div>

Why are you using context. Both inputs are available in the message.

---

<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:** [17 March 2019 11:34 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/18 "2019-03-17T11:34:04Z")

</div>

Because scanning one message for substrings is just too complicated for me.

I get the messages and save them as contexts, so it gets the messages, stores them and then acts on what the contexts are.

I also think I goofed with the line:

```auto
msg.payload = context.get('TimePi_message');

```

And it should be:

```auto
msg = context.get('TimePi_message');

```

Because the entire message is stored. Not just the message.

---

<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:** [17 March 2019 12:01 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/19 "2019-03-17T12:01:00Z")

</div>

OK, do it the way you want then.

---

<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:** [17 March 2019 12:06 UTC](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076/20 "2019-03-17T12:06:14Z")

</div>

I don't mind trying things, but sorry what you explained was too vague for what I understand.

[Next page](https://discourse.nodered.org/t/help-with-logic-filtering-of-message/9076.md?page=2)
