Hi all,
I’ve also been trying to update the time check in my rule since DateType was deprecated.
Now I can’t get the if statement right.
val BedTime = now.withTimeAtStartOfDay.plusDays(1).minusHours(7)
if (now.isAfter(BedTime) && now.isBefore((Sunrise_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
logInfo('Its after bedtime and before sunrise', '--> Bedtime is ' + BedTime + ' PM')
postUpdate(LateNightState, ON)
} else {
logInfo('NOT after bedtime and before sunrise', '--> Bedtime is ' + BedTime + ' PM')
}
Event though the condition is true that it is after 5pm and before Sunrise tomorrow, how come the ‘if’ statement is not executed (i.e. to set LateNightState to ON)?
I’ve been wrestling with this all afternoon and I’m out of ideas.
If anyone can spot the problem it would be greatly appreciated.
Edit: Just as a long shot I changed the && to || and it did execute the if condition.
if (now.isAfter(BedTime) || now.isBefore((Sunrise_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
Now that does not make much sense since it is after 5 AND before sunrise, not OR
In case it helps anyone, here is the full rule (seems to be working)…
rule 'OpenHAB system started'
when
System started
then
postUpdate(LateNightState, ON) // Default this so thatthe lights come on
postUpdate(NightState, OFF)
postUpdate(DayTimeState, OFF)
Thread::sleep(30000) // Give thesystem enough time to load
logInfo('System started rule trigerred', '--> System started')
val BedTime = now.withTimeAtStartOfDay.plusDays(1).minusHours(2)
if (now.isAfter(BedTime) || now.isBefore((Sunrise_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
logInfo('LateNightState', '--> Its after bedtime and before sunrise')
postUpdate(LateNightState, ON)
postUpdate(NightState, OFF)
postUpdate(DayTimeState, OFF)
} else {
if (now.isAfter((Sunset_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli) || now.isBefore((Sunrise_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
logInfo('NightState', '--> It is evening, switch on outside and inside lights')
postUpdate(LateNightState, OFF)
postUpdate(NightState, ON)
postUpdate(DayTimeState, OFF)
} else {
logInfo('Daytime', '--> Daytime, switch all lights off')
postUpdate(LateNightState, OFF)
postUpdate(NightState, OFF)
postUpdate(DayTimeState, ON)
}
}
end