Does schedule() support multiple scheduled jobs for same handler?


(the rooms manager guy) #1

this seems to only schedule the last one:

schedule(now() + 1000, handlerMethod)
schedule(now() + 5000, handlerMethod)

so does this:

schedule(now() + 1000, handlerMethod, [overwrite: false])
schedule(now() + 5000, handlerMethod, [overwrite: false])

thanks!


(John) #2

Did you ever find out the answer to this? I’m scheduling something that requires multiple schedules with the same handler and I only see the last ones scheduled in the IDE. I tried [overwrite: false] but that didn’t help. Would rather not create multiple handlers to fix this (that seems like a band-aid).


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #3

Because the unschedule(scheduledHandler) method only takes the name of the Scheduled Handler Method (and not the time!), it stands to reason that Handler Method Name must be a unique key, and thus cannot be scheduled more than once.

http://docs.smartthings.com/en/latest/smartapp-developers-guide/scheduling.html#removing-scheduled-executions


(the rooms manager guy) #4

runin accepts the overwrite: false flag even though unschedule still accepts only the handler method name and not the time. so that inference may not work.

@shadowjig using multiple runin with the overwrite: false flag should work. then from the runin method handler setup the next set of runins. if that works for you?


(John) #5

Makes sense now. Although the docs seem lite on details!!! Do you have an suggestions to allow multiple schedules based on user input? I have hard coded 2 handlers to address this shortcoming. But I wanted to give the user the option to setup multiple schedules all using the same handler. But if there’s no way to make this dynamic, I have to hard code a limited number of handlers.

runIn doesn’t solve my problem. I want to run the schedules at specific times, not in x minutes from now. While I could write something to manage multiple schedules in the fashion, it’s not preferable.


(the rooms manager guy) #6

i understand its not preferable but given ST’s limitations there are not a lot of other options. :slight_smile:

heres a snippet as an example of what i meant:
in updated()

runIn( (nextScheduledTime1 - now()) / 1000, handlerMethod, [overwrite: false, data: [schedule: 1])
runIn( (nextScheduledTime2 - now()) / 1000, handlerMethod, [overwrite: false, data: [schedule: 2])
runIn( (nextScheduledTime3 - now()) / 1000, handlerMethod, [overwrite: false, data: [schedule: 3])

in handlerMethod(data)

if (data.schedule == 1)
   runIn( (nextScheduledTime1 - time()) / 1000, handlerMethod, [overwrite: false, data: [schedule: 1])
else if (data.schedule == 2)
   runIn( (nextScheduledTime2 - time()) / 1000, handlerMethod, [overwrite: false, data: [schedule: 2])
else if (data.schedule == 3)
   runIn( (nextScheduledTime3 - time()) / 1000, handlerMethod, [overwrite: false, data: [schedule: 3])

EDIT: for completeness here is the timeTodayAfter() method you could use to find the next schedule time for each time input by the user:

http://docs.smartthings.com/en/latest/ref-docs/smartapp-ref.html?highlight=timetoday#timetodayafter


(John) #7

I appreciate the example. But isn’t this limiting you to 3 hard coded schedules?

In order to add a new schedule you need to add a new if condition and schedule. This is what I was trying to avoid.


(the rooms manager guy) #8

not really here is a partial example from the snippet above:

in updated()

for (int i = 0; i < 10; i++)
   if (settings["nextScheduledTime$i"])
      runIn( (settings["nextScheduledTime$i"] - now()) / 1000, handlerMethod, [overwrite: false, data: [schedule: i])

in handlerMethod(data):

runIn( (settings["nextScheduledTime$data.schedule"] - now()) / 1000, handlerMethod, [overwrite: false, data: [schedule: $data.schedule])

you could literally have as many schedules as you wanted based on time schedule entered by the user.

note: these snippets are just examples to point in the right direction not meant to copy the code as is. :slight_smile:


(John) #9

Now that’s cool. I didn’t realize you could use variables in this way. Thanks for help.


(the rooms manager guy) #10

you are welcome.


(the rooms manager guy) #11

btw note that ST officially only allows 6 scheduled jobs at a time. they dont block creating more than 6 today but have assured us that they may do so at anytime in the future. :wink: