Rule: Do a check every 5 minutes and if true then TurnOff myItem

Hello,

I am trying to setup a rule with no success.

The idea is the following:
When I turn on the heater, I would like to check if the Target Water temperature has been reached. If true then Turn Off the heater.

var Timer timer = null // AT THE TOP OF THE RULE FILE

rule "Turn off when target temp reached"
when
    Item DaikinAltherma_Power changed from OFF to ON
then
    if (DaikinAltherma_Power.state == ON) {
        if (timer === null) {
            timer = createTimer(now.plusSeconds(10), [ |
                if (DaikinAltherma_Power.state == ON) { // Item state is still 0 after 10s
                    // do something!
                    var tankTemp = DaikinAltherma_TankTemperature.state as Number
                    var targetTemp = DaikinAltherma_TargetTemperature.state as Number
                    if (tankTemp >= targetTemp) {
                      DaikinAltherma_Power.sendCommand(OFF)
                    }
                }
            ])
        } else {
            if(timer!=null) {
                timer.cancel
                timer = null
       }
    }
end

You don’t need to do this on a schedule.

This is what you actually want to do, so use it as your trigger:

Item DaikinAltherma_TankTemperature changed

Your rule will run every time the sensor changes values. If the power is on and the target temperature is reached, turn it off. If the power is off, exit without doing anything.

Thank you very much.
Much easier approach!
Solved.

No problem. It’s easy to get stuck on the idea of “check every X minutes”, but that’s rarely the best way to automate. You want to have your rules react to incoming data, whether that’s a switch, a sensor, the time of day, a weather report, etc.

Also, note that a looping timer won’t survive your system restarting unless the rule is triggered again. So if OH went down and came back up, your water heater would just stay on permanently in your original rule. You can get around that by using the System started trigger, but personally I prefer to avoid that scenario.