Problem with a rule

Hi experts,

I have a huge problem with a rule.
The code is this:

import org.openhab.model.script.actions.Timer
var Timer timer_bad

rule “Heizung Bad ausschalten”
when
Item heizung_1 received update ON
then
timer_bad = null
timer_bad = createTimer(now.plusMinutes(30)) [|
sendCommand(heizung_1, OFF)
timer_bad = null
]
end

When heizung_1 receive an update my heaeter shall heat 30 minutes and then it shall turn off. But what happens is, it turns off after some minutes or it don’t turn off.
Do someone know what’s wrong?

Note that an Item can receive lots of updates to the same state. Since you do not check to see if timer_bad already has a Timer running you might be setting up multiple timers.

Setting timer_bad to null does not cancel the timer.

First thing to try is change your rule trigger to received command ON which will only trigger the rule when it is explicetly commanded. That will eliminate the first potential problem.

The second thing to try is to check whether you already have a running Timer and if so just reschedule that one, or cancel that one and create a new one.

NOTE: to properly format code put three backticks before and after the code

```
your code
```
    if(timer_bad != null && !timer_bad.hasTerminated){
        timer_bad.reschedule(now.plusMinutes(30))
    }
    else {
        timer_bad = createTimer(now.plusMinutes(30), [|
            sendCommand(heizung_1, OFF)
            timer_bad = null
        ]
    }

Hi Rich,

Thank you for this really fantastic support.
I used you code and up to now it runs.

unfortunately it’s still not running safe. When I make a check and include 5 minutes only (I do not want to wait always 30 minutes) in the code it runs. But when I use my heater in the evening with the down displayed 30 minutes it don’t turn off the heater. Can it be an other problem. Manual turn on and off runs 100%.

Here the present code:

var Timer timer_bad

rule "Heizung_Bad_ausschalten"
when
    Item heizung_1 received command ON 
then
    if(timer_bad != null && !timer_bad.hasTerminated){
        timer_bad.reschedule(now.plusMinutes(30))
    }
    else {
        timer_bad = createTimer(now.plusMinutes(30), [|
            sendCommand(heizung_1, OFF)
            timer_bad = null
        ]
        )
    }

end

The code looks correct. Is the 30 minutes passing midnight? That shouldn’t be a problem but it is the only thing I can think of right now.

There are some other alternative implementations that don’t use timers.

You can use Thread::sleep(minutes601000) where minutes is 5 or 30 or what ever the time to run is. Your rule would look like:

    Thread::sleep(30*60*1000)
    heizung_1.sendCommand(OFF)

This is less than ideal because if you send the ON command while it is sleeping that command will essentially be ignored (not really but the end result will look like it is ignored).

You could go down the path of turning on and off the heater based on some other events such as temperature or time relative to sunrise or sunset.

It’s really crazy. I had a look into the events.log and see that the item themselves run:

2016-07-10 00:57:41 - heizung_1 received command ON
2016-07-10 00:57:41 - heizung_1 state updated to ON

But there is no hint to the rule. The item get the update to ON but the rule seems not to start. But why? Is there a reason that prevent to start the rule?

By the way, if I use like proposed the sleep order, do openhab run furthermore or do it prevent all other rules etc.

Add logging statements to the rule so you can see it triggering in openhab.log.

Each rule executes in its own thread. Even if the same rule is triggered while one instance is sleeping both will run in parallel.