# How to compare two topics with different payload messages

**URL:** <https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289>\
**Category:** General\
**Created:** [20 October 2022 01:11 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289 "2022-10-20T01:11:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dtrexx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dtrexx/32/69366_2.png) [@Dtrexx](https://discourse.nodered.org/u/Dtrexx)\
**Post date:** [20 October 2022 01:11 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/1 "2022-10-20T01:11:09Z")

</div>

I am trying to use Node-Red to switch on pump by comparing the two output from two topics.

topic1 = soc;  
topic2 = pvt;

I want to send 0 or 1 to switch node if any of this condition is meet.

1. if soc \>= 50 && pvt \>= 400 send 1
2. if soc \>= 50 && pvt \<= 400 send 1
3. if soc \<= 50 && pvt \>= 400 send 0
4. if soc \<= 50 && pvt \<= 400 send 0

---

<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:** [20 October 2022 02:43 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/2 "2022-10-20T02:43:19Z")

</div>

Are you sure about your conditions? both 1 and 2 test src \>=50 and the second part will always be rtrue for one of the two cases. sSo it doesn't matter what value pvt contains.

The same thing holds true for 3 and 4

If you are receiving two msgs then you need to use a `join` to join the two msgs so the data can be tested at the same time.

---

<div class="post-metadata">

**Author:** ![Dtrexx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dtrexx/32/69366_2.png) [@Dtrexx](https://discourse.nodered.org/u/Dtrexx)\
**Post date:** [21 October 2022 09:01 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/3 "2022-10-21T09:01:08Z")

</div>

Thanks for the response, yes I want to compare soc against pvt, both value could be low or high ( soc: 0 -100 and pvt: 0 - 1500)

I have use a join and below is the debug output from the join.

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

And also here is how my flow looks like.

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

---

<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:** [21 October 2022 09:21 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/4 "2022-10-21T09:21:58Z")

</div>

You can do the comparison with Switch nodes something like this

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a2494e2ee90f909ae75de783d15c022a8c779e63.jpeg)

But you do need to pay some attention to your proposed conditions.  
Consider if soc == 50 then both (1 or 2) and (3 or 4) will apply.  
As Zenofmud says, the output 1 or 0 is not dependent on the value of pvt.

I expect you have the conditions clear in your mind but you need to be able to define a precise algorithm for the computer.

---

<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:** [21 October 2022 09:35 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/5 "2022-10-21T09:35:44Z")

</div>

What are the SOC and PVT nodes in the flow? Are you splitting a message up and then joining it back together again?

---

<div class="post-metadata">

**Author:** ![Dtrexx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dtrexx/32/69366_2.png) [@Dtrexx](https://discourse.nodered.org/u/Dtrexx)\
**Post date:** [21 October 2022 11:42 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/6 "2022-10-21T11:42:10Z")

</div>

Both SOC an PVT is just to filter out selected topics and I then used a changed node to reduce the topic name to 3 character.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/888628b8ac7a32c7694402d25376ac703508f949.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:** [21 October 2022 12:40 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/7 "2022-10-21T12:40:09Z")

</div>

OK, I would have done that with two MQTT In nodes subscribing to the exact topics, but what you have should work.

---

<div class="post-metadata">

**Author:** ![Dtrexx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dtrexx/32/69366_2.png) [@Dtrexx](https://discourse.nodered.org/u/Dtrexx)\
**Post date:** [21 October 2022 15:32 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/8 "2022-10-21T15:32:50Z")

</div>

Zenofmud , you are correct about the condition .  
for 1 and 2 the out put will be true and for 3 and 4 the output will always be false.  
I have tested with this function but sometimes it work and sometime all the condition are not meet .

```auto
const msg1 = msg.payload.soc;
const msg2 = msg.payload.pvt;

if (msg1 >= 60 && msg2 >= 400)
    {msg.payload = true}
else if (msg1 >= 60 && msg2 <= 400) 
    {msg.payload = true}
else if (msg1 <= 60 && msg2 <= 400) 
    {msg.payload = false}
else
    {msg.payload = 'unknown'}
return msg

```

---

<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:** [21 October 2022 15:56 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/9 "2022-10-21T15:56:15Z")

</div>

Don't call them `msg1` and 2 as they are not messages. Give them meaningful names.

Perhaps sometimes soc is less than 60 and pvt is greater than 400.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [21 October 2022 15:57 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/10 "2022-10-21T15:57:29Z")

</div>

Just taken a very quick look at your conditions and it would seem (to me) that...

The first two tests where `msg1 >= 60`, means that `msg2` is redundant as it doesn't affect the condition.

So your piece of JavaScript is only doing two tests.

```auto
msg1 >= 60
msg1 <= 60 && msg2 <= 400

```

and of course the default when neither is true.

If msg1 was 'equal to 60' then test 1 or two would be satisfied.  
I don't think this "logic" is what you are after.

Please note you have used the condition` >=` and `<=` which means if you had a value of 60 or 400 then both tests are satisfied (as that value appears in BOTH tests).

If for example you wanted 60 as the threshold, you could write two tests...  
One for `<= 60` // This will check for values for 60 and beow  
And another for ` > 60` // This will check for values **above** 60 (61 or maybe 60.0000001)

---

<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:** [21 October 2022 15:59 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/11 "2022-10-21T15:59:58Z")

</div>

Besides your tests apparently still making no sense, you should also consider what happens if either input is null or NaN (not a number)

---

<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:** [20 December 2022 16:00 UTC](https://discourse.nodered.org/t/how-to-compare-two-topics-with-different-payload-messages/69289/12 "2022-12-20T16:00:51Z")

</div>

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