I have created rules that suppose to work only between curtain times with “now.getHourOfDay” only these rules don’t work very consistently, some rules seems to work all the time while other work only sometimes.
here is the rule that seems not to work:
it supposed to turn on a light if a motion sensor is triggered but only between the hours on18:00 and 20:00 on weekdays. if I remove the section with “now.getHourOfDay” this works perfectly (just not in the hours I want it to)
rule "Kitchen light on weekdays if triggered"
when
Item mqtt_topic_2faa32a4_occupancy received update
then
if (mqtt_topic_2faa32a4_occupancy.state == 'true' && isInDayset("workday") && kitchenlighttimer === null) {
if (now.getHourOfDay > 18 && now.getHourOfDay < 20){
miio_generic_0DF3A513_power.sendCommand(ON)
miio_generic_0E0BE239_power.sendCommand(ON)
kitchenlighttimer = createTimer(now.plusMinutes(10), [|
miio_generic_0DF3A513_power.sendCommand(OFF)
if (mqtt_topic_298ef5dd_occupancy.state == 'false' && network_pingdevice_10_0_0_168_online.state == OFF){
miio_generic_0E0BE239_power.sendCommand(OFF)
logWarn("events", "livingroom lights off")
}
kitchenlighttimer = null
])
}
}
end
On the other hand here is a rule that seems to work flawlessly:
rule "Living room Sensor triggered when not home"
when
Item mqtt_topic_298ef5dd_occupancy received update
then
if (mqtt_topic_298ef5dd_occupancy.state == 'true' && isInDayset("workday") && network_pingdevice_10_0_0_122_online.state == OFF) {
if (now.getHourOfDay > 10 && now.getHourOfDay < 18){
val mailActions = getActions("mail","mail:smtp:81520fb9")
mailActions.sendHtmlMail("sXXX@gmail.com", "Intruder Alert", "Living room sensor was triggered and you are not home")
mailActions.sendHtmlMail("sXXX@XXX.com", "Intruder Alert", "Living room sensor was triggered and you are not home")
sendNotification("sXXX@gmail.com", "Living room sensor was triggered and you are not home")
}
}
end
Any help on how I can make this work will be appreciated.
I’m not sure I understand the logic of that.
What does “now.getHourOfDay” calculate?
As far as I understand It supposed to be true between 18:00 and 20:00.
But it doesn’t.
Every time the rule runs, you turn on the light, then start a new timer to turn it off. That does not stop, cancel or destroy any existing timer. That variable is just a handle, a pointer, a reference to an independent timer.
Must be like a disco with all those flashing lights.
See “Rules DSL” example
It looks to see if there is an existing running timer before making a new one.
Ok, just for fun add var Timer kitchenlighttimer = null to the top of the rule and make the suggested >= <= changes Scott mentioned and see what happens.
I just tested it and this works. I was under the impression that “now.getHourOfDay” assumes 00 minutes (I think it allows to add decimal numbers of minutes).
Anyway, Thank you.
I have var Timer kitchenlighttimer = null at the top of the rules file.
I know it works fine because I tried it in simplified form (without the “now.getHourOfDay”) and everything worked 100% the light went of after 10 minutes.
By the way you said at the top of the rule? is it possible that I can’t put var Timer kitchenlighttimer = null as the very first line, when I tried it gave me a bunch of errors, I had to put an unnecessary import in-front of it?