Sequenced "IF" clauses

Hi guys,

I´m trying to create a function node with a sequenced "IF" clause. The use case is as follows:

  • My hue sensor tracks motion
  • Msg.payload "true" (boolean) is sent to the function node
  • Depending on the part of the day AND the brightness in the room, different scenes should be activated

This is the flow

Thats the content of the debug node "Status Conditions"

And thats the content of my function node "Check Conditions":

msg.debug = {
    occupancy: msg.payload,
    occupancyType: typeof msg.payload,
    Lights_Off: flow.get("Lights_Off"),
    Lights_OffType: typeof flow.get("Lights_Off"),
    TagModus: global.get("TagModus"),
    TagModusType: typeof global.get("TagModus"),
    AbendModus: global.get("AbendModus"),
    AbendModusType: typeof global.get("AbendModus"),
    NachtModus: global.get("NachtModus"),
    NachtModusType: typeof global.get("NachtModus"),
    Brightness: flow.get("Brightness"),
    BrightnessType: typeof flow.get("Brightness")
}

if  (((flow.get("Lights_Off")===true))&&
    ((flow.get("Brightness")<25)))
    {
    if (global.get("TagModus") === true)
        {
        if ((flow.get("Brightness")>12))
            msg.payload = {"scene":"EStwiUv7q69qKtm"};
            return msg;
        }
        else
        {
            msg.payload = {"scene":"57KOzEvv0bVYuq9"};
            return msg;
        }
    if (global.get("AbendModus") === true)
    {
    msg.payload = {"scene":"zTM-LR-NfXhpzWs"};
    return msg;
    }
    else
    {
    msg.payload = null;
    return msg;
    }
}

Now what actually SHOULD happen under the circumstances given, is this:
msg.payload = {"scene":"zTM-LR-NfXhpzWs"};

However, what actually DOES happen is this:
msg.payload = {"scene":"57KOzEvv0bVYuq9"};

And now I need your brain power guys to point me where am I going down the wrong path :thinking:

Add node.warn() function calls at appropriate places to work out which test is failing an why. You can find examples of this in Writing Functions : Node-RED

It looks to me it is working as written. If I'm following the logic correctly given the settings provided you will get to the message payload you indicated then because you have a return msg it will kick you out of the logic and return your value . Thus it will never interrogate for the value of abendmous in your logic

1 Like

Thanks much @gerry - removed the "return msg" arguments in the if clauses and now it works :slight_smile:

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