Trying to make a if function work

i got a home center that sends out events:
and i only want the message to go trough when both id's are true.
but now if only 222 is true, the messga goes though.

if ( (msg.id == 222 && msg.payload == true) || ( msg.id == 220 && msg.payload == true) ){
return msg;
}

Hi @niceguy45 welcome to the forum!

Well assuming that both msg.id & msg.payload arrive at the same time, then your code works for me. See -

[{"id":"157eccae.ee3a93","type":"inject","z":"c53060.842a0fa","name":"","props":[{"p":"payload"},{"p":"id","v":"222","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":140,"y":1350,"wires":[["3b61ed35.e45e42"]]},{"id":"b490915a.67398","type":"debug","z":"c53060.842a0fa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":540,"y":1350,"wires":[]},{"id":"3b61ed35.e45e42","type":"function","z":"c53060.842a0fa","name":"","func":"msg.test=\"OK\";\nif ( (msg.id == 222 && msg.payload == true) || ( msg.id == 220 && msg.payload == true) ){\nreturn msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":1350,"wires":[["b490915a.67398"]]},{"id":"7b65e10f.83712","type":"inject","z":"c53060.842a0fa","name":"","props":[{"p":"payload"},{"p":"id","v":"222","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":140,"y":1400,"wires":[["3b61ed35.e45e42"]]}]

PS - if you share any code or flows, please see this post which describes how to post it (as the forum software changes it!)

the id's will not arive at the same time, like id 222 can be true for 5 minutes and then id 224 wil become true too.

In that case, you have 2 options (at least);

  1. use a join node to ensure that both messages are joined and passed through the flow at the exact same time.

  2. save one of the msg's to context, and then retrieve it when the other msg arrives, so again both msg's pass through the flow at the same time.

1 Like

so how must i make the second option?

Have you read the documentation about context on the link that I gave you?

yes, but i can't coded yet, i like note red to program as little as possible.

Use a change node and where it says msg, and change it depending on your needs you could use:
Node context
Flow context
Global context

As @Paul-Reed mentioned, reading the documentation is boring, but you will need to as it is your only help here to understand how these properties work, because this is some of the basic stuff, in mine projects i use hundreds of contexts...

Hi Lizzardguki,

I understand, but i use node-red in other way's, and it has helpt me in all projects. But i only have this problem, that is not working with my little knowledge of coding part.

It's essential that both msg's flow through the statement at the same time, so if you save 1 msg to context, then when the 2nd msg arrives, retrieve the first msg from context, and you will then have both msg's in the flow at the same time.
Using your code above as an example, something like;

let mypayload = flow.get("mypayload");
if ( (msg.id == 222 && mypayload == true) || ( msg.id == 220 && mypayload == true) ){
return msg;
}

So you would retrieve mypayload (true or false) which has been previously saved, and then use it in the if statement.

If you understand how this would work, you could considerably simplify your code.

However, you could always use a join node...

This code is not working. nothing goes through.

also getting the error use '===' to compare with 'true'

Why not post your flow so we can see what you have.
If you do, remember to format the flow as described in the link that I gave you above, otherwise we will not be able to use it.

It's not an error, it's advice - see JavaScript Comparison and Logical Operators
but yes, you should use ===

so why is the code not working? in the debug i see the event coming in. but the msg is not send.

As per my last post....

single commands if commands are working, but need 1 function that is waiting to get 2 id's that are true and then fire the next command.

As I have already said twice, please read the link that I gave you above to post flows in the forum!
The flow that you have posted cannot be used, so please use the pencil icon in your last post, and edit your post as per the link.

EDIT 11.30pm Sorry, bedtime in UK for me!

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