# Trying to make a if function work

**URL:** <https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691>\
**Category:** General\
**Created:** [22 October 2020 19:17 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691 "2020-10-22T19:17:05Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![niceguy45](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@niceguy45](https://discourse.nodered.org/u/niceguy45)\
**Post date:** [22 October 2020 19:17 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/1 "2020-10-22T19:17:06Z")

</div>

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;   
}

---

<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:** [22 October 2020 19:30 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/2 "2020-10-22T19:30:33Z")

</div>

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 -

```auto
[{"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](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) which describes how to post it (as the forum software changes it!)

---

<div class="post-metadata">

**Author:** ![niceguy45](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@niceguy45](https://discourse.nodered.org/u/niceguy45)\
**Post date:** [22 October 2020 19:35 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/3 "2020-10-22T19:35:32Z")

</div>

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.

---

<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:** [22 October 2020 19:39 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/4 "2020-10-22T19:39:37Z")

</div>

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](https://nodered.org/docs/user-guide/context), and then retrieve it when the other msg arrives, so again both msg's pass through the flow at the same time.

---

<div class="post-metadata">

**Author:** ![niceguy45](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@niceguy45](https://discourse.nodered.org/u/niceguy45)\
**Post date:** [22 October 2020 19:40 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/5 "2020-10-22T19:40:43Z")

</div>

so how must i make the second option?

---

<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:** [22 October 2020 19:41 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/6 "2020-10-22T19:41:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![niceguy45](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@niceguy45](https://discourse.nodered.org/u/niceguy45)\
**Post date:** [22 October 2020 20:26 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/7 "2020-10-22T20:26:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [22 October 2020 20:44 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/8 "2020-10-22T20:44:02Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![niceguy45](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@niceguy45](https://discourse.nodered.org/u/niceguy45)\
**Post date:** [22 October 2020 20:48 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/9 "2020-10-22T20:48:54Z")

</div>

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.

---

<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:** [22 October 2020 20:55 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/10 "2020-10-22T20:55:24Z")

</div>

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;

```auto
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...

---

<div class="post-metadata">

**Author:** ![niceguy45](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@niceguy45](https://discourse.nodered.org/u/niceguy45)\
**Post date:** [22 October 2020 21:31 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/11 "2020-10-22T21:31:54Z")

</div>

This code is not working. nothing goes through.

---

<div class="post-metadata">

**Author:** ![niceguy45](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@niceguy45](https://discourse.nodered.org/u/niceguy45)\
**Post date:** [22 October 2020 21:35 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/12 "2020-10-22T21:35:07Z")

</div>

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

---

<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:** [22 October 2020 21:55 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/13 "2020-10-22T21:55:43Z")

</div>

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.

> [@niceguy45](#):
>
> getting the error use '===' to compare with 'true'

It's not an error, it's advice - see [JavaScript Comparison and Logical Operators](https://www.w3schools.com/js/js_comparisons.asp)  
but yes, you should use `===`

---

<div class="post-metadata">

**Author:** ![niceguy45](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@niceguy45](https://discourse.nodered.org/u/niceguy45)\
**Post date:** [22 October 2020 22:09 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/14 "2020-10-22T22:09:49Z")

</div>

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

---

<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:** [22 October 2020 22:11 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/15 "2020-10-22T22:11:09Z")

</div>

As per my last post....

> [@Paul-Reed](#):
>
> Why not post your flow so we can see what you have.

---

<div class="post-metadata">

**Author:** ![niceguy45](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@niceguy45](https://discourse.nodered.org/u/niceguy45)\
**Post date:** [22 October 2020 22:24 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/17 "2020-10-22T22:24:54Z")

</div>

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.

---

<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:** [22 October 2020 22:26 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/18 "2020-10-22T22:26:27Z")

</div>

> [@Paul-Reed](#):
>
> PS - if you share any code or flows, [please see this post](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) which describes how to post it

> [@Paul-Reed](#):
>
> 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.

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!

---

<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:** [21 December 2020 22:27 UTC](https://discourse.nodered.org/t/trying-to-make-a-if-function-work/34691/20 "2020-12-21T22:27:19Z")

</div>

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