Hi,
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?
Example:
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"
when
Item mySwitch changed to ON // was switched on
then
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
end
do it the other way:
rule "auto off"
when
Item myItem changed
then
if((newState as Number) > 150) // while > 150
return; // stop the rule
// now myItem.state is <= 150
mySwitch.sendCommand(OFF) // switch it off
end
It will do the same job, but won’t need the Thread.sleep() and the rule won’t consume any processor time.
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.
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.