# Help with nested if-then-else please

**URL:** <https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733>\
**Category:** General\
**Tags:** function-node, home-assistant\
**Created:** [21 November 2025 14:03 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733 "2025-11-21T14:03:18Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveK](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@DaveK](https://discourse.nodered.org/u/DaveK)\
**Post date:** [21 November 2025 14:03 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/1 "2025-11-21T14:03:18Z")

</div>

Hi again,

I'm slowly learning Node Red and its workings, trying to get a function node working with nested if-then-else statements but despite messing for some hours I just can't grasp it. Could someone kindly have a look at my code below and maybe mould it into a working function.

The idea is that if the msg contains an "on" command **and** the requested RGB colours are as listed it will set the msg object as shown.

If the "on" command is there **and** the RGB colours requested **do not** match the figures shown then set the msg as shown.

If the command is "off" then just do that bit.

Sounds so simple but my coding education stopped at visual basic so I'm way behind and getting on in age now sadly.

Thanks for any guidance

```auto
var CT = global.get('homeassistant.homeAssistant.states["input_number.rgb_white_tone"].state');
var BR = global.get('homeassistant.homeAssistant.states["input_number.dr_rgb_default"].state');
CT = parseInt(CT);
BR = parseInt(BR);

if (msg.on === true)
{
    if (msg.rgb[0] == 192 && msg.rgb[1] == 192 && msg.rgb[2] == 192){
        msg.payload =
        "action": "light.turn_on",
        data:
        {"color_temp": CT,
        "brightness": BR}
    } else {
        msg.payload =
        "action": "light.turn_on",
        data:
        {"rgb_color": msg.rgb, "brightness": msg.bri}
    };
};
if (msg.on === false) msg.payload = {
    "action": "light.turn_off"
};
return msg;

```

---

<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:** [21 November 2025 14:28 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/2 "2025-11-21T14:28:20Z")

</div>

The editor should show that you have problems with your brace matching. The statements assigning an object to msg.payload should be

```auto
        msg.payload = {
            "action": "light.turn_on",
            data:
            {
                "color_temp": CT,
                "brightness": BR
            }
        }

```

And similarly for the second one.

You can further simplify it with something like

```auto
var CT = global.get('homeassistant.homeAssistant.states["input_number.rgb_white_tone"].state');
var BR = global.get('homeassistant.homeAssistant.states["input_number.dr_rgb_default"].state');
CT = parseInt(CT);
BR = parseInt(BR);

if (msg.on) {
    if (msg.rgb[0] == 192 && msg.rgb[1] == 192 && msg.rgb[2] == 192) {
        msg.payload = {
            "action": "light.turn_on",
            data:
            {
                "color_temp": CT,
                "brightness": BR
            }
        }
    } else {
        msg.payload = {
            "action": "light.turn_on",
            data:
                { "rgb_color": msg.rgb, "brightness": msg.bri }
        }
    };
} else {
    msg.payload = {
        "action": "light.turn_off"
    }
}
return msg;

```

You can get rid of all the trailing semi-colons too.  
I advise against code like

```auto
if (msg.on === false) msg.payload = {
    "action": "light.turn_off"
};

```

Put in the extra braces so that it becomes

```auto
if (msg.on === false) {
     msg.payload = {
        "action": "light.turn_off"
    }
};

```

It makes it clearer.

---

<div class="post-metadata">

**Author:** ![DaveK](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@DaveK](https://discourse.nodered.org/u/DaveK)\
**Post date:** [21 November 2025 14:28 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/3 "2025-11-21T14:28:36Z")

</div>

A cup of coffee helped me out, I was over-complicating things it seems, the working version I now have is below. I guess the lesson learnt is to KISS (keep it simple, stupid) 🙂

```auto
var CT = global.get('homeassistant.homeAssistant.states["input_number.rgb_white_tone"].state');
var BR = global.get('homeassistant.homeAssistant.states["input_number.dr_rgb_default"].state');
CT = parseInt(CT);
BR = parseInt(BR);

if (msg.on === true && msg.rgb[0] == 192 && msg.rgb[1] == 192 && msg.rgb[2] == 192)
msg.payload =
{
    "action": "light.turn_on",
    data:
    {"color_temp": CT, "brightness": BR}
}
else if (msg.on === true)
msg.payload =
{
    "action": "light.turn_on",
    data:
    {"rgb_color": msg.rgb, "brightness": msg.bri}
};

if (msg.on === false) 
msg.payload = 
{
    "action": "light.turn_off"
};
return msg;

```

---

<div class="post-metadata">

**Author:** ![DaveK](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@DaveK](https://discourse.nodered.org/u/DaveK)\
**Post date:** [21 November 2025 14:29 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/4 "2025-11-21T14:29:39Z")

</div>

Many thanks, I was typing my update when you replied 🙂

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [21 November 2025 14:37 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/5 "2025-11-21T14:37:42Z")

</div>

Here's another offering for you...  
**Note:** Untested as I do not use HA.

```auto
var CT = global.get('homeassistant.homeAssistant.states["input_number.rgb_white_tone"].state');
var BR = global.get('homeassistant.homeAssistant.states["input_number.dr_rgb_default"].state');

CT = parseInt(CT);
BR = parseInt(BR);

// Default payload
msg.payload = {};

if (msg.on === true) {

    // Check for your "white tone" RGB pattern
    if (msg.rgb && msg.rgb[0] === 192 && msg.rgb[1] === 192 && msg.rgb[2] === 192) {

        msg.payload = {
            action: "light.turn_on",
            data: {
                color_temp: CT,
                brightness: BR
            }
        };

    } else {

        msg.payload = {
            action: "light.turn_on",
            data: {
                rgb_color: msg.rgb,
                brightness: msg.bri
            }
        };

    }

} else if (msg.on === false) {

    msg.payload = {
        action: "light.turn_off"
    };

}

return msg;

```

---

<div class="post-metadata">

**Author:** ![DaveK](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@DaveK](https://discourse.nodered.org/u/DaveK)\
**Post date:** [21 November 2025 14:42 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/6 "2025-11-21T14:42:32Z")

</div>

Nice, thanks,

I've cleaned it to this point, I think it's a little easier for me to read....

```auto
var CT = global.get('homeassistant.homeAssistant.states["input_number.rgb_white_tone"].state')
var BR = global.get('homeassistant.homeAssistant.states["input_number.dr_rgb_default"].state')
CT = parseInt(CT)
BR = parseInt(BR)

if (msg.on === true && msg.rgb[0] == 192 && msg.rgb[1] == 192 && msg.rgb[2] == 192) {
    msg.payload = { "action": "light.turn_on", data: { "color_temp": CT, "brightness": BR }}
}
else if (msg.on === true) {
    msg.payload = { "action": "light.turn_on", data: { "rgb_color": msg.rgb, "brightness": msg.bri }}
}

if (msg.on === false) {
    msg.payload = { "action": "light.turn_off" }
}
return msg

```

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [21 November 2025 14:45 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/7 "2025-11-21T14:45:48Z")

</div>

If you want to send the settings into an action node, there is a specific format to use if you want to override the node.

> **[Action | node-red-contrib-home-assistant-websocket](https://zachowj.github.io/node-red-contrib-home-assistant-websocket/node/action.html#input)**
>
> A suite of nodes that seamlessly integrates Home Assistant with Node-RED

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [21 November 2025 14:45 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/8 "2025-11-21T14:45:52Z")

</div>

IMHO  
If you retain the identation in the formatting - then it is much easier to spot a missing { or }.

---

<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:** [21 November 2025 16:15 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/9 "2025-11-21T16:15:42Z")

</div>

It is best not to use `var`. Use `let` or `const` instead. Google if you don't know why.

In case you did not realise, instead of `if (msg.on === true)` you can just use `if (msg.on)`. If you know that msg.on is true or false then the two statements are identical. If msg.on is, for example, 1 or "1" or in fact any string or any non-zero number, then the test will ~~fail~~ pass in the first test, but ~~pass~~ fail in the second.

Edit: corrected above. Non zero numbers and all strings (including "false" and "0") are considered truthy in javascript. Number 0 is considered falsey.

---

<div class="post-metadata">

**Author:** ![DaveK](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@DaveK](https://discourse.nodered.org/u/DaveK)\
**Post date:** [21 November 2025 16:57 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/10 "2025-11-21T16:57:08Z")

</div>

> [@Colin](#):
>
> It is best not to use `var`. Use `let` or `const` instead. Google if you don't know why.

Thanks, something else learnt and applied.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [22 November 2025 11:09 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/11 "2025-11-22T11:09:00Z")

</div>

Another option to simplify nested if/else structures is to break outside, to save annoying bracket counting. For example:

```auto
msg.status = 0;

if (condition1)
   return msg.

if (condition2) {
   if (condition3) {
      msg.status = 1;
      return msg.
   }

   if (condition4) {
     msg.status = calcStatus();
     return msg;
   }
   msg.status = 7;
   return msg.
}
msg.status = 99;
return msg;

```

---

<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:** [22 November 2025 13:59 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/12 "2025-11-22T13:59:43Z")

</div>

Personally I would not do that. I have seen, many times, programmers updating code that has early return statements add code not noticing that a return had been made further up, meaning that the added code does not get run.

---

<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:** [20 February 2026 14:00 UTC](https://discourse.nodered.org/t/help-with-nested-if-then-else-please/99733/13 "2026-02-20T14:00:18Z")

</div>

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