I have been trying to use time to automate several things in my rules. I attempted to create timers for things, like front door opens, and the foyer light comes on and then shuts off after 5 minutes. Or, hallway motion is tripped and the hall light comes on for 45 seconds. I could not get the timer to work, so I used the Thread::sleep(45000)
before sending the sendCommand(Hallway_Hue, 0)" command, which works great!
Now, I am trying to come up with something for my outside lights. I would like the garage and front door light to come on from 8:30 pm to Midnight, and I think there should be a more elegant way to express between 8:30 pm and midnight. Is there a way, in rules to have a when statement to be something like: Time cron between "30 20 * * *" and "59 23 * * *
I have it set up now with two rules to turn them on when Time cron "30 20 * * *", then a second rule in the same file that turns them off wen Time cron "59 23 * * *". I would like to simplify this rule.
That’s not the way openHAB works. A rule will be triggered and executed once. To switch lights on an off at a certain time, you would use two rules, one for on and one for off. The trigger then is the time
rule "switch on lights at 8:15pm"
when
Time cron "0 15 20 * * ?"
then
MyLights.sendCommand(ON)
end
rule "switch off lights at 12:00am"
when
Time cron "0 0 0 * * ?"
then
MyLights.sendCommand(OFF)
end
Be aware that openHAB uses quartz cron, so the first value is for the seconds, not the minutes!
Once again, Udo_Hartmann, thank you, I was getting my cron expressions from an on line calculator. I am still learning about cron… What does the “?” mean in an expression? From what I gather, the “*” means “any or all”…
Thanks again,
~John
EDIT: never mind, I just checked out the link on your post for quartz cron and this will help me immensely, Thank you!!
You’re welcome Please try out http://www.cronmaker.com/ but be aware that even this online calculator does unnecessary complex expressions, for instance 1/1 instead of *.
In question of the mysterious ?, this is very special, every Time cron expression has to have exactly one ?, either for day of month or for day of week (i.e. the 4th or the 6th place) because if you set both values you will see strange behavior (‘trigger only if the 17. of the current month is a Friday’ is, er, unusual)
The 7th place is optional and would be the year, so, not to forget the retirement, I will set a Time cron to “0 0 8 31 1 ? 2037”
Thank you with the help on the other post, I figured that I’d post my reply here rather than taking anything away from that post. You gave me the rule syntax for my hallway motion as:
import org.joda.time.DateTime
var Timer tHallway = null
rule "Turn on hallway light when motion detected"
when
Item Hall_Motion changed to OPEN
then
sendCommand(Hallway_Hue, 50)
if (tHallway!=null)
tHallway.cancel
tHallway = createTimer(now.plusSeconds(45)[|
sendCommand(Hallway_Hue, 0)
]
end
The light does turn on as expected, however, it does not turn off. I have tried to reboot my RasPi and also restarting OpenHab (ver. 1.8). This is what shows up in my log when I triggered the rule:
2016-05-31 18:57:20.161 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Turn on hallway light when motion detected': The name '<XFeatureCallImplCustom> != <XNullLiteralImpl>' cannot be resolved to an item or type.
Any ideas as to what would cause this? Also, I am not seeing any errors in designer either.
Thanks,
~John
EDIT: I also saw this in the events.log:
2016-05-31 19:46:35 - Hall_Motion state updated to OPEN
2016-05-31 19:46:35 - Hallway_Hue received command 50
2016-05-31 19:46:37 - Hallway_Hue state updated to 81.9230769230769197974950657226145267486572265625,57,50
This was after I walked from the living room into the kitchen, out of view of the hallway PIR, then back into the hallway.
Ok, I added the “)” and am still getting the same error as above. I did restart openhab, but did not reboot the raspberry pi. Funny, designer did not pick up on the missing bracket.
hmmm… Maybe there has to be a space before and after !=…
import org.joda.time.DateTime
var Timer tHallway = null
rule "Turn on hallway light when motion detected"
when
Item Hall_Motion changed to OPEN
then
sendCommand(Hallway_Hue, 50)
if (tHallway != null)
tHallway.cancel
tHallway = createTimer(now.plusSeconds(45))[|
sendCommand(Hallway_Hue, 0)
]
end
Just a thought… if you cause a rule file to reload while a timer in that file is already running, when that timer executes it’ll throw an error/exception. (I run into this all the time when writing/testing rules, I had to learn to ignore it.)
The spaces didn’t fix anything, I was still getting the same errors. I decided to take a look at the sample rules file again and started playing around with it and this is the rule that I came up with:
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
var Timer timer = null
rule "Turn on hallway light when motion detected"
when
Item Hall_Motion changed to OPEN
then
if(timer==null) {
sendCommand(Hallway_Hue, 75)
// first ON command, so create a timer to turn the light off again
timer = createTimer(now.plusSeconds(45)) [|
sendCommand(Hallway_Hue, 0)
]
} else {
// subsequent ON command, so reschedule the existing timer
timer.reschedule(now.plusSeconds(45))
}
end
This is working, but I have not tested the “subsequent ON command” yet… Anything in this rule that sticks out as unnecessary?