This rule works but I'm not so sure if I did it right

Hi all,
I’ve created this rule. It works but I’m not so sure if it’s the way it should be written. Please advice
rule “Heater Tuinschuur”

when

    Item SensorSauna_Temperature2 changed

then

if (SensorSauna_Temperature2.state < 9  | ℃ && HeaterTuinschuur_Switch.state == OFF)

{ HeaterTuinschuur_Switch.sendCommand(ON)

}

if (SensorSauna_Temperature2.state > 11  | ℃ && HeaterTuinschuur_Switch.state == ON )

{ HeaterTuinschuur_Switch.sendCommand(OFF)

}

end

I would like to ad another criteria when a door is open, the heater should not be switched on.
So I could ad another && maybe?

Thanks for you help

Koen

yes

There is nothing wrong with the rule as written and adding new criteria should be relatively straight forward. Just remember that the order of operations will be left to right so you may need parens to evaluate the first && first if that’s what you need.

But often when I start to see more than two expressions in a single if statement I try to find a way to split it up.

Consider this. The test for HeaterTuinschuur_Switch is so that you don’t send an ON command when it’s already ON or an OFF command when it’s already OFF. So let’s pull that out on it’s own so the rule has two parts: 1. determine whether the Switch should be ON or OFF and 2. send the command only if the Switch isn’t already set to what it’s supposed to be.

As your rule is currently written that could look something like:

// 1. Determine what state the heater should be in
var heaterState = "STAY" // do nothing if the temp is between 9 and 11 °C
if(SensorSauna_Temperature2.state < 9 | °C) {
    heaterState = "ON"
}
else if(SensorSauna_Temperatuer2.state > 11 | °C) {
    heaterState = "OFF"
}

// 2. Send the command if the switch isn't in the right state
if(heaterState != "STAY" && HeaterTuinschuur_Switch.state.toString != heaterState) {
    HeaterTuinschuur_Switch.sendCommand(heaterState)
}

Now, presumably you want the heater to be OFF no matter what the temperature is when the door is open. So you can add just one more if statement before or after checking the temps.

// 1. Determine what state the heater should be in
var heaterState = "STAY" // do nothing if the temp is between 9 and 11 °C
if(Door.state == OPEN) {
    heaterState = "OFF"
}
else if(SensorSauna_Temperature2.state < 9 | °C) {
    heaterState = "ON"
}
else if(SensorSauna_Temperatuer2.state > 11 | °C) {
    heaterState = "OFF"
}

// 2. Send the command if the switch isn't in the right state
if(heaterState != "STAY" && HeaterTuinschuur_Switch.state.toString != heaterState) {
    HeaterTuinschuur_Switch.sendCommand(heaterState)
}

Notice the order of the if statements and the use of else. The first one of those that evaluates to true will execute and the rest will not. So if the door is OPEN, that first if will run. Only if the door is not OPEN will the test for 9 °C run. Only if the door is not OPEN and the temp is >= 9 °C will the test for 11 °C run.

Running down the code and executing it in your head is easier and a lot less mental work when it’s split up like this than when you try to put everything all in one big if condition with || and && operators.

1 Like

Dear Rich,

Thank you so much for your detailed answer to my question.
I am looking in to it and still have a lot to learn dealing with rules, programming etc.
Really appreciate the time you took to give me this great solution.