Hi all, I’ve got a rule which executes a switch based on an event: (i.e. when a then b) but want to make this time of day dependent. (i.e. when a AND time is between x and y then b).
Is this possible, if so what’s the syntax for writing a rule like this?
The following is only an untested guess, so take it with skepticism. But the point is to use Joda Time, use the LocalTime class which is a partial to represent morning and evening, and then compare now to make sure it’s in range. Use this example as a basis for Googling and Stackoverflowing the correct answer.
import org.joda.time.*
rule ButtonTimeOfDay
when
Item MyButton received command
then
val LocalTime morning = new LocalTime(8, 0) // 8am every day
val LocalTime evening = new LocalTime(18, 0) // 6pm every day
if (now.toLocalTime().isAfter(morning) && now.toLocalTime().isBefore(evening)) {
MyLamp.sendCommand(receivedCommand)
}
end
For this the simple code is good, but it doesn’t work if i try to execute the rule from a time in the evening till the next morning…
rule ButtonTimeOfDay
when
Item myitem received command
then
val LocalTime morning = new LocalTime(7, 0) // 7am every day
val LocalTime evening = new LocalTime(20, 0) // 20pm every day
if (now.toLocalTime().isAfter(evening) && now.toLocalTime().isBefore(morning)) {
sendCommand(anotheritem "ON")
}
end
rule ButtonTimeOfDay
when
Item myitem received command
then
val LocalTime morning = new LocalTime(7, 0) // 7am every day
val LocalTime evening = new LocalTime(20, 0) // 20pm every day
if (now.toLocalTime().isAfter(evening) || // triggers for 20-24h
now.toLocalTime().isBefore(morning)) { // triggers for 0-7h
sendCommand(anotheritem "ON")
}
end