runOnce Scheduling

Hey all,

Wrote an app where I attempt to bypass the 4 schedule limit but having one event trigger, schedule a start/stop time, then go on to the next schedule. I was attempting to use the runOnce command…However, I am running into issues that I am hoping someone can assist.

The syntax, as I understand it is this:

First, I get the input:

input “timeOnDay1”, title: “Time to turn on”, “time”, required: false
input “timeOffDay1”, title: “Time to turn off”, “time”, required: false

Then I schedule the first event so that it runs daily:

schedule(timeOnDay1 , “turnOnSwitchDaily”)

Then, each method runs the next item:

def turnOnSwitchDaily() {
turn on something
runOnce (timeOffDay1, turnOffSwitch1)
}

def turnOffSwitch1() {
turn off something
}

While it appears that the runOnce properly schedules in the log, about a minute after turnOnSwitchDaily() fires, the turnOffSwitch1() fires no matter what the time is set at timeOffDay1. I thought this was just the IDE, but the real app does the same thing and the ONLY thing I can think of is that the format from the input isn’t correct for the runOnce scheduling…Anything thoughts?

I can work around this by unscheduling the start and stop in each method, but that seem clunky unless that is the only way to accomplish it.

Thanks!

1 Like

I’m doing something similar in SmartRules, using runOnce and maintaining a queue of scheduled events myself. I haven’t run into any issues with runOnce, although I am generating the date string in the iOS app, not from the SmartApp preferences, so I’m not sure if something is different there.

I tried a few things and I am of the conclusion that I am not giving the correct parameters for the runOnce command. And what I mean by that is while the date is correct in the timeOffDay variable (and DOES work with the subscribe command), that it feels like it doesn’t get picked up in the runOnce command…

Anyone else have any advice?

Can you show what the time you are using looks like? Here is the format I am using:

"yyyy-MM-dd'T'HH:mm:ss.SSSZ"

from the eventlog in the IDE:

timeOffDay1:2015-02-02T11:44:00.000-0700

Obviously, the calendar date is wrong in the IDE.

Could that be the problem? “schedule” only uses the time, and runs it every day, while runOnce uses the full date. Maybe your date is wrong.

1 Like

Hmmm…the example in the documentation shows the format to be the same, and the issue appears in the app and IDE. If I debug the date in the actual app, it shows that date a the future time set in the app itself…I may try to hardcode a future date/time in there to see if that works.

from the documenation:

runOnce(dateTime, handlerMethod, options)

Executes the handlerMethod once at the specified date and time. The dateTime argument can be either a Date object or a date string.

options
Optional. Map of options. Valid options:

[overwrite: true or false]

Specify [overwrite: false] if you do not want the most recently created job for the handlerMethod to overwrite an existing job. See the discussion in the runIn documentation above for more information.

def someEventHandler(evt) {
// execute handler tomorrow, at the current time
runOnce(new Date() + 1, handler)
}

def handler() {
switch.off()
}
def someEventHandler(evt) {
// execute handler at 4 PM CST on October 21, 2015 (e.g., Back to the Future 2 Day!)
runOnce(“2015-10-21T16:00:00.000-0600”, handler)
}

def handler() {
// do something awesome, like ride a hovercraft
}

Brice,

It appears you may be correct…it appears if you send a date that is PAST to the runOnce command it skips it and moves to the next instance…when I ‘refreshed’ the dates in the app it works much better…

Good call!

2 Likes