I'm looking to add some time of day checks to my custom app, but am not sure the easiest way to pull out hour for example from now()
I was looking at The Apache Groovy programming language - Blogs - Groovy Dates And Times Cheat Sheet
I tried
now().hour
now().properties
now[HOUR_OF_DAY]
Nothing worked. I want to do something like:
def triggerAlarm() {
if (avoidNightAlerts == true) {
if (now().hour > 20 || now().hour < 7) {
return
}
}
if (state.alarmTriggerTimestamp > 0) {
state.alarmTriggerTimestamp = 0
send(alertMessage)
}
}
TArman
December 16, 2023, 3:47pm
2
Did you try?
LocalTime.now().hour
I was also trying to do this and found stuff online about using the Calendar class instead. In this example I am getting the current date/time, subtracting a minute and then extracting the hour and minute to be used with scheduling a recurring task.
void scheduleSomething() {
def cal = Calendar.getInstance()
cal.add(Calendar.MINUTE, -1)
Integer hour = cal[Calendar.HOUR_OF_DAY]
Integer minute = cal[Calendar.MINUTE]
schedule( "0 ${minute} ${hour} * * ?", scheduledFunction)
}
2 Likes
Thanks all for the ideas and resources!
This one worked really easy for me. I'm assuming its 24 hour time format for the hours. Will see in a few hours.
2 Likes
here is another way
def now = new Date().format('MM/dd/yyyy h:mm a', location.timeZone)
sendEvent(name: "lastUpdate", value: now, descriptionText: "Last Update: $now")