This page last changed on Dec 15, 2013 by orjo.
since it is not possible to call macros in a rule, how can i define sleep times in a rule?
I need a sleep of 100ms in a rule.
Thank you very much.
It is Java so you can do this:
Posted by aktur at Dec 15, 2013 17:11
Thank you, it works.
Posted by orjo at Dec 15, 2013 20:58
Out of curiosity. Is OR halted during this sleep period or are other rules still running in the background along with the rest of OR say UDP or tcp etc?
Posted by nwe at Dec 17, 2013 10:16
The rule engine is stopped and the thread it is running too. Some other parts running in different threads will be running though. Anyway, this solution is suitable for really short delays, like 100ms, I would say up to 1s. If you need more delay in rules, like minutes, hours or even more then a proper approach would be:
1. Make 2 rules and declare a new fact.
2. In the first rule insert the new fact defined previously.
3. In the second rule do the following:
rule "second rule"
execute.command("delayed command by 1h");
Posted by aktur at Dec 17, 2013 11:18
I did a little testing with using sleep in rules.
The goal to achieve was to run macros to control an AV-Scene. (Turn On AV-Receiver - Turn on TV - Turn on Satellite Receiver - wait 4s to turn on - Send Input HDMI1 to AV-Receiver)
I have the following rule:
$evt : Event(source=="UDPL_HIFI", value=="WATCH_TV_TV_ON")
log("rule: WATCH_TV_TV_ON starts NOW");
log("rule: WAIT 4s for devices");
log("rule: Go On");
execute.command("(HDMI 3) [AV-Receiver]");
log("rule: WATCH_TV_TV_ON is ON");
Alle the InMemory Virtual Command-Rules look like this:
$evt : Event(source=="(SENSOR)_IMVC_AVPOWER", value=="on")
log("rule: AV_IMVC_ON is ON");
There is no sleep in any of them. Every IMVC-Rule writes an Log-Entry before and after executing the "Real" command.
The created Log looks like this:
INFO 2013-12-22 19:29:36,274 : rule: WATCH_TV_TV_ON
INFO 2013-12-22 19:29:40,274 : rule: WATCH_TV_TV_ON starts NOW
INFO 2013-12-22 19:29:40,275 : rule: WAIT 4s for devices
INFO 2013-12-22 19:29:44,275 : rule: Go On
INFO 2013-12-22 19:29:44,432 : rule: WATCH_TV_TV_ON is ON
INFO 2013-12-22 19:29:44,434 : rule: UDPL Hifi Reset
INFO 2013-12-22 19:29:44,934 : rule: SAT_IMVC_ON
INFO 2013-12-22 19:29:45,098 : rule: SAT_IMVC_ON is ON
INFO 2013-12-22 19:29:45,100 : rule: TV_IMVC_ON
INFO 2013-12-22 19:29:45,371 : rule: TV_IMVC_ON is ON
INFO 2013-12-22 19:29:45,373 : rule: AV_IMVC_ON
INFO 2013-12-22 19:29:45,528 : rule: AV_IMVC_ON is ON
THe first Sleep is OK but why are the IMVC-Rules called after the Sleep-Time of 4seconds? In the Main-Rule it is called after sending all the IMVC-Rules.
@Michal Rutka: Could you post a complete Code-Sample of the two Rules for non Programmers (like me)
Thank you in advance.
Posted by orjo at Dec 22, 2013 18:47
This is expected behavior. A single rule execution blocks other rules and the whole sensor thread. To add delays without blocking you need to do the following: (not tested so beware of typos)
rule "Rule 1"
Event($s: source, $v: value, source=="UDPL_HIFI", value=="WATCH_TV_TV_ON")
insert(new DelayEvent($s.toString(), $v.toString()));
rule "Rule 2"
timer (int: 4s)
rule "Rule 3"
timer (int: 8s)
$de : DelayEvent(source=="UDPL_HIFI", value=="WATCH_TV_TV_ON")
Posted by aktur at Dec 23, 2013 09:50
Thank you Michael, that works perfect.
I will try to adopt this to my other scenes.
Posted by orjo at Dec 26, 2013 08:40