rule "Daytime ON"
when
Time cron "0 0 6 ? * * *"
then
DayTime.postUpdate(ON)
end
rule "Daytime ON"
when
Time cron "0 0 19 ? * * *"
then
DayTime.postUpdate(OFF)
end
So now we have a “Flag” for the time
You want to trigger the light depending on the PIR so:
rule "smart light on"
when
Item pirDummy changed
then
if ((pirDummy.state == ON) && (YeelightColour.state == OFF) && (DayTime.state == ON)) {
YeelightColour.sendCommand(ON)
}
else if ((pirDummy.state == OFF) && (YeelightColour.state == ON) && (DayTime.state == OFF)) {
YeelightColour.sendCommand(OFF)
}
end
Yes, now gives you the current time. But giving that time meaning is what the ToD DP is for. It also centralized all of your time calculations in one place.
It may be overkill now if you only care what hour of the day it is in one place. But if you have two or more rules that care about the time of day then it really becomes a problem to have the calculations scattered throughout your rules.
Vincent’s solution is a simplified version of the ToD DP.
But, in reference to your first post, the important thing to realize is that Rules get triggered by events, not states. As written, your rule gets triggered once at 06:00 and every hour until 19:00.
What you need, based on your description, is to trigger the rule at 06:00, at 19:00, when ever the light changes state and when ever the PIR changes state.
Once the rule triggers, you check all the states and take the appropriate action.
Thanks, I may introduce the ToD DP later on, however at the moment the only things I’m able to integrate with openhab are a couple of smart lights, so at this scale there would be less code for me to just directly address local time.
Initially I did have the rule being triggered by the pir sensor changing state and then had the time as one of the conditions, however the rule wasn’t working and I thought how I was addressing time was wrong so I shifted it to how my current rule is written in my OP.
I did try using now, maybe I was writing the code wrong. I had actually tried one of your suggestions to another poster in a thread from about 6 months ago but it didn’t seem to work.
Do you mind sharing how to correctly use now in an if statement?
Shorter than now? now IS the current time. The withTimeAtStartOfDay gives you midnight today. The plusHours adds that many hours. The isBefore returns true if the passed in time is before the the time you are calling it on