Multiple "If clauses" in function node

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:

And this is the code within the function node:

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 :roll_eyes:

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

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

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

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

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.

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

} else {
  msg = null
}
return msg

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

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

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

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:

And here comes my updated function node:

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" :unamused:

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

Change your function to this...

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

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

1 Like

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 :man_facepalming:

1 Like

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