Hello all – I’m having trouble getting a cron job to fire correctly. The app I’m working on is for a scheduled mode change by day of the week.
The job WILL fire at the correct time when testing it. For instance, if it is 9 PM when I install the app, and set the time to fire at 9:02, the mode will change successfully. What has caught me off guard was that yesterday my mode change was set for 11:59 PM and fired around 9:30 PM ish.
If it matters, the time zone I am in is EST, but it seems like when I put logic in to modify the time to EST, the job will not fire.
I know that the code is a bit sloppy – I haven’t had much time to play around with Groovy to learn all of the functions that are available to make the code the most efficient.
preferences {
section("Configuration") {
input "dayOfWeek", "enum",
title: "Which day of the week?",
multiple: false,
metadata: [
values: [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
'Monday to Friday',
'Saturday & Sunday',
'All Week'
]
]
input "time", "time", title: "At this time"
input "newMode", "mode", title: "Change to this mode"
}
section( "Notifications" ) {
input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes", "No"]], required: false
input "phoneNumber", "phone", title: "Send a text message?", required: false
}
}
def installed() {
initialize()
}
def initialize() {
def d = timeToday(time)
def h = d.format('HH')
def m = d.format('mm')
//def h = d.format('HH', TimeZone.getTimeZone("EST")) // SET TIME ZONE TO EST
//def m = d.format('mm', TimeZone.getTimeZone("EST")) // SET TIME ZONE TO EST
log.debug "hour=" + h + " minute=" + m
def weekDay
if(dayOfWeek == "Monday"){
weekDay = '1'
}
if(dayOfWeek == "Tuesday"){
weekDay = '2'
}
if(dayOfWeek == "Wednesday"){
weekDay = '3'
}
if(dayOfWeek == "Thursday"){
weekDay = '4'
}
if(dayOfWeek == "Friday"){
weekDay = '5'
}
if(dayOfWeek == "Saturday"){
weekDay = '6'
}
if(dayOfWeek == "Sunday"){
weekDay = '7'
}
if(dayOfWeek == "Monday to Friday"){
weekDay = '1-5'
}
if(dayOfWeek == "Saturday & Sunday"){
weekDay = '6,7'
}
if(dayOfWeek == "All Week"){
weekDay = '1-7'
}
def cronString = h + " " + m + " * * " + weekDay + " ?"
log.debug cronString
schedule(cronString, changeModeByDay)
}
def changeModeByDay() {
log.debug "changeModeByDay, location.mode = $location.mode, newMode = $newMode, location.modes = $location.modes"
if (location.mode != newMode) {
if (location.modes?.find{it.name == newMode}) {
setLocationMode(newMode)
send "${label} has changed the mode to '${newMode}'"
}
else {
send "${label} tried to change to undefined mode '${newMode}'"
}
}
}
FYI – I’ve omitted some of the functions. I didn’t think including them would be necessary. Any help would be appreciated, as well as any code optimizations that you may see.