How to compare two topics with different payload messages

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

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.

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

And also here is how my flow looks like.

You can do the comparison with Switch nodes something like this

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.

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

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.

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

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 .

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

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.

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.

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)

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

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