whats the correct syntax for this if statement (compare every light hsbtype in one room with a given HSBType)?
var HSBType weiss = new HSBType(new DecimalType(0),new PercentType(0),new PercentType(100))
if((Yeelightc_6.state as HSBType).equals(weiss) && (Yeelightc_6.state as HSBType).equals(weiss) && (XGateway_c.state as HSBType).equals(weiss) == true)
Try this, but check the first two items as they are the same, or is that a typo?
if(Yeelightc_6.state as HSBType == weiss && Yeelightc_6.state as HSBType == weiss && XGateway_c.state as HSBType == weiss) {
// if the above statement is true then do whatever goes here
}
end
You might need the extra ( )'s, not really sure, so if the first way fails try like below.
if(Yeelightc_6.state as HSBType == weiss) && (Yeelightc_6.state as HSBType == weiss) && (XGateway_c.state as HSBType == weiss) {
// if the above statement is true then do whatever goes here
}
end
but lets hold on for the syntax:
so basically an if statement returns true if all of its boolean querys return true
if (truestatement && truestatement) → true
right?
So I can get a bunch of querys returning true without saying that they HAVE to be true, to get my if statement to true
Sorry, this was a bit indistinctly.
What I meant:
you don’t have to tell the compiler which state all querys should return.
for example:
if(true.statement && true.statement == true)
but this is enough:
if(true.statement && true.statement )
right?
The if statement checks if everything is true. If all is true then the part between the { } of the rule will execute. If the statement is not true it will not execute and you can use else to execute something different when the if statement is false.
Sometimes a picture is worth a thousand words, see the comments // in the rule below. Hope this helps.
rule "Detect Motion when Garage is Open"
when
Item Esp_Easy_Motion changed from OFF
then
if(Proxy_Motion.state == ON && stopMotionTimer === null && ESP_Easy_Door.state == OFF){ // When everything in the statement is TRUE it will execute
Echo_Plus_TTS.sendCommand('Alert, garage movement!') // This happens when the if statement is true
stopMotionTimer = createTimer(now.plusSeconds(15)) [| // Just a timer so it doesn't happen every second while movement is detected.
stopMotionTimer = null
]
}
end
See the link for how statments work. I keep this book marked on my PC.
(((Yeelightc_5.state as HSBType).equals(weiss)) && ((Yeelightc_6.state as HSBType).equals(weiss)) && ((XGateway_c.state as HSBType).equals(weiss)))
Look like what you had at first was mostly correct, just needed to remove the ==true b/c that’s what the if statement is doing, and add the extra ( )'s
If you would please mark the post solved by clicking the square box on the post that provided the solution. Also edit the title to start with [Solved] to help others with a similar issue fine a quick solution.
Thanks, I’m sure aware how a if statement works lol, I’m just sometimes confused how XBase is interpreting things.
I’m Sometimes having strange behaviour with my VS Code extension when writing a if statement.
XBase is just an unusual language and I’m not aware of anything yet
Syntax highlighting where everything should be fine, most of the time it’s a comparison between values which should return a boolean type.
Anyway, it’s working just fine now.
thanks!