Function node: How to "OR" operate

Hi guys,
For one of my Alexa based flows, I´m trying to define the room in which the echo device is located that one is speaking to. Unfortunately, the corresponding "Alexa Event" node does not deliver the information about the group which the device is assigned to. So this again means DIY :grinning_face_with_smiling_eyes:

In order to do so I´m trying to create a function node, that sets a flow variable for the certain room based on the device the event was initiated from. And that´s how it looks like:

if ((msg.payload.data.domain === "Routines"))
{
flow.set("ActiveEcho",msg.payload.deviceSerialNumber)
}

if ((msg.payload.name === "Büro Echo"||"Büro Echo Show")){
    flow.set("ActiveRoom","Büro")
    
}else if (msg.payload.name === "Bad Echo") {
    flow.set("ActiveRoom","Badezimmer")
    
}else if (msg.payload.name === "Kinderzimmer Echo"){
    flow.set("ActiveRoom","Kinderzimmer")
    
}else if ((msg.payload.name === "Küche Echo"||"Küche Echo Show")){
    flow.set("ActiveRoom","Küche")
    
}else if ((msg.payload.name === "Schlafzimmer Echo links"||"Schlafzimmer Echo rechts")){
    flow.set("ActiveRoom","Schlafzimmer")

}else if ((msg.payload.name === "Couch links"||"Couch rechts"||"Wohnzimmer Echo Show"||"Wohnzimmer Echo Studio")){
    flow.set("ActiveRoom","Wohnzimmer")
    
}else{
    flow.set("ActiveRoom","undefined")
}

So far, so good. However, no matter which room I initiate the alexa routine from, the "ActiveRoom" variable is always set to "Büro". Interesting wise, the "ActiveEcho" flow variable is set correctly. Now what am I doing wrong here?

If I write this line in plain english, it says...

If msg.payload.name equals "Büro Echo" OR "Büro Echo Show" is something

To explain a little further, If the LEFT HAND side of an || is false, the RIGHT HAND SIDE is evaluated. "Büro Echo Show" evaluates to true in a boolean test.

change it to...

if (msg.payload.name === "Büro Echo" || msg.payload.name ===  "Büro Echo Show") {
   //...
}

(And all the other places you have used ||)

You might be better off with a switch statement TBH..

if ((msg.payload.data.domain === "Routines")) {
    flow.set("ActiveEcho",msg.payload.deviceSerialNumber)
}

switch (msg.payload.name) {
    case "Büro Echo":
    case "Büro Echo Show":
        flow.set("ActiveRoom", "Büro")
        break;
    case "Bad Echo":
        flow.set("ActiveRoom", "Badezimmer")
        break;
    case "Kinderzimmer Echo":
        flow.set("ActiveRoom", "Kinderzimmer")
        break;
    case "Küche Echo":
    case "Küche Echo Show":
        flow.set("ActiveRoom", "Küche")
        break;
    case "Schlafzimmer Echo links":
    case "Schlafzimmer Echo rechts":
        flow.set("ActiveRoom", "Schlafzimmer")
        break;
    case "Couch links":
    case "Couch rechts":
    case "Wohnzimmer Echo Show":
    case "Wohnzimmer Echo Studio":
        flow.set("ActiveRoom", "Wohnzimmer")
        break;
    default:
        flow.set("ActiveRoom", "undefined")
}

3 Likes

Works like a charm - Thanks much @Steve-Mcl ! :+1:

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