Get rules killed by openhab?

I just read in another thread that openhab kill rules after a certain amount of time.
When does this case occur?
I’m working with rulesDSL and I’m considering running a rule with “while”.
Will this rule be killed at some point if it doesn’t come to an end at some point?
While{ item > 150
Sleep x
Turn off


Well, it’s an extremly bad Idea to work explicitly against openHAB (which is, to to start a rule which will run until something happens), but no, the rule won’t be killed.

When using openHAB1 oder openHAB2, there are 5 + 2 threads for all rules, which leads to problems when more than 5 rules run at the same time. (2 threads are for scheduled tasks, Time cron or createTimer).
So a sixth rule won’t start until one of the five running rules will end.
In openHAB3 every rule gets its own thread, so no problem, but not smart.

Instead of something like this:

rule "auto off"
    Item mySwitch changed to ON              // was switched on
    while((myItem.state as Number) > 150)    // while > 150
        Thread.sleep(10000)                  // sleep 10 seconds

                                             // now myItem.state is <= 150
    mySwitch.sendCommand(OFF)                // switch it off

do it the other way:

rule "auto off"
    Item myItem changed
    if((newState as Number) > 150) // while > 150
        return;                    // stop the rule

                                   // now myItem.state is <= 150
    mySwitch.sendCommand(OFF)      // switch it off

It will do the same job, but won’t need the Thread.sleep() and the rule won’t consume any processor time.

IIRC five minutes after a rule starts running if it doesn’t exit normally OH will kill the rule.

That’s what Timers are for. Timers are a far better approach. You can even set up a looping Timer.

val loopingtimer = createTimer(now, [ |
  if(item > 150) loopingtimer.reschedule(now.plusSeconds(1))
  else MyItem.sendCommand(OFF)

That doesn’t block the rule thread, is basically the same number of lines of code, and if you need to manage the timer sometime later you can do that.

Yes, but the looping timer one wont. There is no time limit for how long into the future a timer can be scheduled nor how many times a timer can be rescheduled. And it takes mere milliseconds to create the Timer and run the timer’s function every second so OH will never have to kill it to time it out.

@Udo_Hartmann’s approach is yet another excellent approach.

There’s a rule template on the marketplace that could do this too (Threshold Alert).

There are many ways to achieve this that do not involve sleeps. Truth be told, were it up to me I would eliminate support for sleeps entirely. It’s almost never used in a safe manner, and there is nothing that can be done with a sleep that cannot be done with a Timer.

1 Like

Thanks for the explanations.
The loop timer method is great.

I did not know that

Me neither… :slight_smile:

Pretty sure though, that this was introduced with openHAB3 or at least somewhere after the last openHAB1 version…

It’s hard to tell. In OH 2 there was a completely separate rule engine for Rules DSL vs everything else and there wasn’t a time limit for Rules DSL rules (though there were other problems with long running rules). I would not be surprised if the timeout was added in what was called the Experimental Rule Engine in OH 2 which is now just the rule engine on OH 3.

1 Like