The example rules page on the OH1 wiki generally show rules created using {} curly brackets, but i’ve seen in comments on here that people think curly brackets are not required.
I have a rule that is playing up a bit, and wanted to know what role curly brackets play, if any, please.
ok - so are curly brackets now used correctly in this rule?
rule "SUNSET we are home minus 30 mins - lights on"
// This will turn on all ground floor lamps 30 mins before sunset if we are in
when
Item NightTime received command ON
then
if
(Powermax_zone7_status2.state == CLOSED)
{
sendCommand(Light_GF_Lounge_All, ON)
sendCommand(Light_GF_Dining_All, ON)
sendCommand(Light_GF_Kitchen_All, ON)
sendCommand(Light_GF_Hall_Lamp, ON)
sendCommand(Light_GF_Hall_Lamp_2, ON)
sendCommand(Light_GF_Hall_Porch_New, ON)
}
if (Powermax_zone7_status2.state == OPEN)
{
sendMail("abc@xyz.com", "something happened to the house", "Backdoor open, lights not on")
sendMail("def@xyz.com", "something happened to the house", "Backdoor open, lights not on")
sendCommand(Waiting_For_Backdoor_To_Close, ON)
}
end
Yes. But I would write it this way for personal optical reasons. And added an else for performance reasons.
rule "SUNSET we are home minus 30 mins - lights on"
when
Item NightTime received command ON
then
// This will turn on all ground floor lamps 30 mins before sunset if we are in
if (Powermax_zone7_status2.state == CLOSED) {
sendCommand(Light_GF_Lounge_All, ON)
sendCommand(Light_GF_Dining_All, ON)
sendCommand(Light_GF_Kitchen_All, ON)
sendCommand(Light_GF_Hall_Lamp, ON)
sendCommand(Light_GF_Hall_Lamp_2, ON)
sendCommand(Light_GF_Hall_Porch_New, ON)
} else if (Powermax_zone7_status2.state == OPEN) {
sendMail("abc@xyz.com", "something happened to the house", "Backdoor open, lights not on")
sendMail("def@xyz.com", "something happened to the house", "Backdoor open, lights not on")
sendCommand(Waiting_For_Backdoor_To_Close, ON)
}
end
No. The only brackets not required are the ones between when/then or then/end. The if statement needs a context. Can’t explain it any better.
To clarify, this works also:
rule "SUNSET we are home minus 30 mins - lights on"
when
Item NightTime received command ON
then {
// This will turn on all ground floor lamps 30 mins before sunset if we are in
if (Powermax_zone7_status2.state == CLOSED) {
sendCommand(Light_GF_Lounge_All, ON)
sendCommand(Light_GF_Dining_All, ON)
sendCommand(Light_GF_Kitchen_All, ON)
sendCommand(Light_GF_Hall_Lamp, ON)
sendCommand(Light_GF_Hall_Lamp_2, ON)
sendCommand(Light_GF_Hall_Porch_New, ON)
} else if (Powermax_zone7_status2.state == OPEN) {
sendMail("abc@xyz.com", "something happened to the house", "Backdoor open, lights not on")
sendMail("def@xyz.com", "something happened to the house", "Backdoor open, lights not on")
sendCommand(Waiting_For_Backdoor_To_Close, ON)
}
}
end
In general, if there is a dependence, like an if(), and if there is more than one command depending on it, you will have to set it in curly brackets, try out this:
rule "test rule"
when
Item MyItem changed
then
if (MyItem.state == ON)
logInfo ("test","state is ON")
logInfo ("test","this is always printed")
if (MyItem.state == OFF) {
logInfo ("test","state is OFF")
logInfo ("test","this is only printed when MyItem state is OFF")
}
end