# If condition with a function

**URL:** <https://discourse.nodered.org/t/if-condition-with-a-function/25389>\
**Category:** General\
**Created:** [24 April 2020 00:14 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389 "2020-04-24T00:14:22Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 22:07 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/21 "2020-04-24T22:07:10Z")

</div>

in a function it would be something like this and you would need to attach it to the output of the home/away presence node:

```auto
const isNight = global.get(“isNight”);
if(msg.payload === “home” && isNight === true){
    msg.payload = “got home and its night”;
    return msg;
} else {
    return null;
}

```

not tested

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 22:13 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/22 "2020-04-24T22:13:04Z")

</div>

Perfect!

I can manage do that with the switch node instead.

---

<div class="post-metadata">

**Author:** ![abhayssaini](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@abhayssaini](https://discourse.nodered.org/u/abhayssaini)\
**Post date:** [13 June 2020 07:37 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/23 "2020-06-13T07:37:37Z")

</div>

respected sir, i request to help me out..  
i am making pulse read sensor so for that i run python code and send data to node red. in that i had used (function node) with code

msg.payload=msg.payload.Pulse  
return msg;

reading is showing no problem but sir now i want to make another 2nd function node such that whenever the value exceeded 100 then condition is matched and give true message to (switch node) which is connected to 2nd function node.so sir what code should i wright in 2nd function code.  
path name of sanser value coming is payload.  
thankyou

---

<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:** [13 June 2020 08:39 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/24 "2020-06-13T08:39:11Z")

</div>

1 - how are you sending the data to NR?  
2 - is

```auto
msg.payload=msg.payload.Pulse
return msg;

```

the only code in the 'function' node? if so, you could use a `change` node instead:  
 ![Screen Shot 2020-06-13 at 4.36.25 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80ee577e1d74ad32315ffe6cf408f7791a905c6b.png)  
3 - you say

> now i want to make another 2nd function node such that whenever the value exceeded 100 then condition is matched  
> why not use a switch node for this?

---

<div class="post-metadata">

**Author:** ![abhayssaini](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@abhayssaini](https://discourse.nodered.org/u/abhayssaini)\
**Post date:** [13 June 2020 09:12 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/25 "2020-06-13T09:12:33Z")

</div>

1- sir through ibm cloud.  
2- yes sir  
msg.payload=msg.payload.Pulse  
return msg;  
these same code i am running in my 1st function node named (pulse rate)  
3- but sir before using switch node i thought comparison of pulse values must be there with the help of 1st and 2nd function node.Then 2nd function node will be connected with switch node which will connect the further node only when condition is fulfilled. for this i use 1:- value rules as (is true) in switch node..  
however i want what you think sir  
thankyou

---

<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:** [13 June 2020 09:33 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/26 "2020-06-13T09:33:31Z")

</div>

Ok, now you have confused me. Are you saying you have two (2) inputs and you need to compare their values?

Please provide a flow diagram explaining what you need to do.

---

<div class="post-metadata">

**Author:** ![abhayssaini](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@abhayssaini](https://discourse.nodered.org/u/abhayssaini)\
**Post date:** [30 June 2020 11:18 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/27 "2020-06-30T11:18:45Z")

</div>

Hi sir, its true that i'm very late to reply. i'm sorry for that. it's about the project that i had to do and due to less time i had submitted with another way.  
thank you 'Zenofmud' for helping us out!

---

<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:** [29 August 2020 11:18 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/28 "2020-08-29T11:18:46Z")

</div>

Closed on 29 August 2020 11:18 UTC

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

[Previous page](https://discourse.nodered.org/t/if-condition-with-a-function/25389.md?page=1)
