Edited:
Reading this edit is definitely easier and faster than reading the accepted solution, so for a quick answers:
- Yes, rules are put in queue;
- The “no more than 5 rules” limit means no more than 5 rules at a time, the others are queued and executed afterwards;
- The solution is don’t use thread:sleep, but use Timers instead.
Original post:
My setup is all zwave on Raspberry Pi4 with UZB Stick, Openhab 2.5
I have a rule to turn on the light on opening the door.
It was working well (even though there is a thread::sleep in it) and I’ve never had any issues. Until today, when I was playing around with the door (I had to adjust it a little bit and that involved multiple opening and closing). When I have finished the light continued to go on and off again for quite a while. I checked in the log and the door was closed for 20 minutes or so, but the rule kept getting triggered.
Here is the rule (I put a lot of logging in it after editing due to some hardware changes and upgrade to OH2.5):
rule "door light"
when
Item door_contact changed to OPEN
then
logInfo("Test", "door light triggered!")
logInfo("Test","door state(start): " + door_contact.state.toString)
logInfo("Test","motion(start): " + door_motion.state.toString)
sendCommand(door_light,ON)
Thread::sleep(60000)
while (door_light.state != OFF) {
if ((door_contact.state != CLOSED) || ( door_motion.state != 0 )) {
logInfo("Test","door state(if): " + door_contact.state.toString)
logInfo("Test","motion state(if): " + door_motion.state.toString)
logInfo("Test", "waiting")
}
else {
sendCommand(door_light,OFF)
logInfo("Test","door state(else): " + door_contact.state.toString)
logInfo("Test","motion state(else): " + door_motion.state.toString)
logInfo("Test", "turning off")
}
Thread::sleep(30000)
}
logInfo("Test", "off, nothing to do")
end
Like I said I opened and closed the door plenty of times, but closed it definitely at about 1:21 and the light kept going on (with “door light triggered” in the openhab.log) until 1:57.
It seems to me like if the rule triggers were put on stack and taken one after another until the stack was empty. The rules kept being executed for each opening of the door, even if the door was already closed. I thought there is a limit of 5 (or less than 10) rules that can be run, but never thought the rules can get stacked and postponed, can they? Am I right or is there another explanation?
The most important question is how to avoid that? Is it possible to check - from within the rule - if the same rule has already been started and stop executing new rules then? (advices welcome)
I would even prefer to abandon the rule at all if the execution is not possible within 5 or 10 seconds, because I don’t need it after that time.
Also, is there a way to stop executing all the stacked rules? I didn’t want to reboot openhab, so I waited until it stopped blinking (somewhat annoying )
Here is the log:
2020-01-31 01:56:00.380 [INFO ] [.eclipse.smarthome.model.script.Test] - turning off
2020-01-31 01:56:06.245 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do
2020-01-31 01:56:06.252 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do
2020-01-31 01:56:06.257 [INFO ] [.eclipse.smarthome.model.script.Test] - door light triggered!
2020-01-31 01:56:06.260 [INFO ] [.eclipse.smarthome.model.script.Test] - door light triggered!
2020-01-31 01:56:06.261 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(start): CLOSED
2020-01-31 01:56:06.263 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(start): CLOSED
2020-01-31 01:56:06.264 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(start): 0
2020-01-31 01:56:06.267 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(start): 0
2020-01-31 01:56:30.386 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(else): CLOSED
2020-01-31 01:56:30.390 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(else): 0
2020-01-31 01:56:30.394 [INFO ] [.eclipse.smarthome.model.script.Test] - turning off
2020-01-31 01:56:30.395 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(else): CLOSED
2020-01-31 01:56:30.399 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(else): 0
2020-01-31 01:56:30.401 [INFO ] [.eclipse.smarthome.model.script.Test] - turning off
2020-01-31 01:56:51.549 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do
2020-01-31 01:56:51.557 [INFO ] [.eclipse.smarthome.model.script.Test] - door light triggered!
2020-01-31 01:56:51.561 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(start): CLOSED
2020-01-31 01:56:51.564 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(start): 0
2020-01-31 01:57:00.407 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(else): CLOSED
2020-01-31 01:57:00.411 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(else): 0
2020-01-31 01:57:00.414 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(else): CLOSED
2020-01-31 01:57:00.415 [INFO ] [.eclipse.smarthome.model.script.Test] - turning off
2020-01-31 01:57:00.418 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(else): 0
2020-01-31 01:57:00.421 [INFO ] [.eclipse.smarthome.model.script.Test] - turning off
2020-01-31 01:57:06.271 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do
2020-01-31 01:57:06.272 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do
2020-01-31 01:57:06.277 [INFO ] [.eclipse.smarthome.model.script.Test] - door light triggered!
2020-01-31 01:57:06.281 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(start): CLOSED
2020-01-31 01:57:06.286 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(start): 0
2020-01-31 01:57:07.421 [INFO ] [.eclipse.smarthome.model.script.Test] - door light triggered!
2020-01-31 01:57:07.425 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(start): CLOSED
2020-01-31 01:57:07.428 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(start): 0
2020-01-31 01:57:30.424 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(else): CLOSED
2020-01-31 01:57:30.428 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(else): 0
2020-01-31 01:57:30.430 [INFO ] [.eclipse.smarthome.model.script.Test] - turning off
2020-01-31 01:57:30.430 [INFO ] [.eclipse.smarthome.model.script.Test] - door state(else): CLOSED
2020-01-31 01:57:30.433 [INFO ] [.eclipse.smarthome.model.script.Test] - motion state(else): 0
2020-01-31 01:57:30.436 [INFO ] [.eclipse.smarthome.model.script.Test] - turning off
2020-01-31 01:57:51.570 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do
2020-01-31 01:58:00.434 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do
2020-01-31 01:58:00.440 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do
2020-01-31 01:58:06.293 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do
2020-01-31 01:58:07.435 [INFO ] [.eclipse.smarthome.model.script.Test] - off, nothing to do