# Multiple "If clauses" in function node

**URL:** https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788
**Category:** General
**Created:** [6 June 2021 22:11 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788 "2021-06-06T22:11:14Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)
#### Post date: [6 June 2021 22:11 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/1 "2021-06-06T22:11:14Z")

</div>

Hi guys,  
I just started my very few steps using functional nodes as I´m not a programmer so please bear with me if this request is rather simple, stupid. Here is the use case I´m trying to accomplish:

When entering my office, I like the motion sensor to turn on the light (quite simple). This is what I would like to achieve:

- IF motion is detected (msg.payload.occupancy===true) AND no lights are on (("BueroLicht\_AN")===false) THEN the light should be turned an
- When turning on the light, different scenes for day- and nighttime should be chosen

That´s how the flow looks like:

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

And this is the code within the function node:

```auto
if ((msg.payload.occupancy===true)&&((flow.get("BueroLicht_AN")===false))) {
    if (global.get("TagModus") === true)
    {
    msg.payload = {"scene":"zMs2ou4CxSgWBd8"}
    }
    else
    {
    msg.payload = {"scene":"UpeL9VFrraH7QlU"}
    }
}
return msg;

```

The problem now is, even if all criteria to activate a certain scene are met, the original incoming payload is being returned 🙄

I´m quite sure this is a piece of cake for people with solid coding skills... Looking forward to some help 🙂

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [6 June 2021 22:39 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/2 "2021-06-06T22:39:47Z")

</div>

> [@RonMa](#):
>
> `return msg;`

That is outside the if statements and so will always run.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [6 June 2021 22:53 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/3 "2021-06-06T22:53:23Z")

</div>

To be sure that your values are correct add this line to the beginning of your function node. It will output the values to debug so you can see them

```auto
node.warn(`occupancy = ${msg.payload.occupancy} 
Buero = ${flow.get("BueroLicht_AN")}  
Tag = ${global.get("TagModus")}`);

```

---

<div class="post-metadata">

### Author: ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)
#### Post date: [7 June 2021 07:18 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/4 "2021-06-07T07:18:54Z")

</div>

You're using "strict equality" (===) rather than "equality" (==). This is good, but it means if the types don't match, they will not be equal. For example, `msg.payload.occupancy` could contain the string "true", rather than the binary value true, and `"true" === true` is false, because they are different types.

If you use a debug node to show the payload, you will be able to tell. It will either have `occupancy: "true"` for a string, or `occupancy: true` for a binary value. Your comparison should match. If the types already match, it could be your flow variable that differs. You can check this in the Context Data tab.

---

<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: [7 June 2021 08:52 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/5 "2021-06-07T08:52:40Z")

</div>

As @TotallyInformation points out the problem is that you do not have an `else` clause on the outer `if`. Therefore, if the first test fails you are returning the original message unchanged. A simple solution is the replace the last two lines with

```auto
} else {
  msg = null
}
return msg

```

This works because returning null tells it not to pass on any message.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [7 June 2021 09:27 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/6 "2021-06-07T09:27:53Z")

</div>

> [@RonMa](#):
>
> The problem now is, even if all criteria to activate a certain scene are met, the original incoming payload is being returned

The OP said this, so if occupancy is true and the global is true and the flow is false, then payload should be changed to scene. That makes me think that the conditional values have not been met. Sure if the flow and occupancy values are not met the payload will not be changed, but that's not what the OP has said. he needs to confirm his conditional values

---

<div class="post-metadata">

### Author: ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)
#### Post date: [7 June 2021 20:41 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/7 "2021-06-07T20:41:05Z")

</div>

Hi everyone,  
thank you so much for all the feedback you gave - That is amazing! 👏

I tried to incorporate as much as I could and also tried to simplify the function in order to first get it going before adding more complexity to it. Now this is my new flow:

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

And here comes my updated function node:

```auto
if ((msg.payload.occupancy===true)&& 
    (flow.get("BueroLicht_AN")===false)&&
    (global.get("homee.nodes[21].attributes[16].current_value")<90))
{
     msg.payload = {"scene":"zMs2ou4CxSgWBd8"};
     return msg;
}else{
    msg.payload = "Lights off";
    return msg;
   }

```

As you can see in the picture, all conditions for turning on the light are actually met (I ignore the day- and nighttime distinction for now):

- Notion is detected from the sensor  
`msg.payload.occupancy===true`

- All lights in the office are off  
`(flow.get("BueroLicht_AN")===false)`

- The brightness of my second sensor is below the defined trash hold `((global.get("homee.nodes[21].attributes[16].current_value")<90))`

Still, no matter what I do, the function output is always "Lights off" 😒

Anybody has another guess what's wrong in the equation?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [7 June 2021 21:01 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/8 "2021-06-07T21:01:01Z")

</div>

Change your function to this...

```auto
msg.debug = {
    occupancy: msg.payload.occupancy,
    occupancyType: typeof msg.payload.occupancy,
    BueroLicht_AN: flow.get("BueroLicht_AN"),
    BueroLicht_ANType: typeof flow.get("BueroLicht_AN"),
    homee_nodes_21_attribute_16_current_value: global.get("homee.nodes[21].attributes[16].current_value"),
    homee_nodes_21_attribute_16_current_valueType: typeof global.get("homee.nodes[21].attributes[16].current_value")
}

if ((msg.payload.occupancy===true)&& 
    (flow.get("BueroLicht_AN")===false)&&
    (global.get("homee.nodes[21].attributes[16].current_value")<90))
{
     msg.payload = {"scene":"zMs2ou4CxSgWBd8"};
     return msg;
}else{
    msg.payload = "Lights off";
    return msg;
   }

```

then attach a debug node to the output set to show complete msg  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a8c3577c72acdb35db5ac3a9cf49965e38882db.png)

then expand msg.debug & you should see what is wrong

---

<div class="post-metadata">

### Author: ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)
#### Post date: [8 June 2021 19:39 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/9 "2021-06-08T19:39:21Z")

</div>

That helped me a lot to find the problem. Thanks much @Steve-Mcl ! The issue was that `msg.payload.occupancy` was not defined as boolean 🤦‍♂️

---

<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: [22 June 2021 19:39 UTC](https://discourse.nodered.org/t/multiple-if-clauses-in-function-node/46788/10 "2021-06-22T19:39:41Z")

</div>

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