Map options for the schedule command? Example/How To?

I have been using the schedule method for a long time to set devices to refresh, daily check for new driver versions, etc... I think I have the basics of it down pretty well. However, I am trying to make use of the options map it allows for with my version checking to set the driver name and current version so I can have it in just one place in the code AND make that update check the exact same regardless (so it can eventually be in a library).

The wiki is a little light on the example, as there is none about the Map for the options. It apparently does not work or is not saved across the scheduler the same way. Can someone clue me in on how it should be used?

Here are a few code fragments I am using for testing examples:

  • This one should run the CheckForUpdate every hour based on the Second and Minute values. That portion works great but the Map data does not appear to be passed. The "options" value is always null.
schedule( "${ Second } ${ Minute } * ? * *", "CheckForUpdate", [ 'Driver Name': "UnifiProtectAPI", 'Driver Version': "0.2.1" ] )

def CheckForUpdate( Map options = null ){
    if( options != null ){
        Logging( "Data = ${ options }", 3 )
        ProcessState( "Driver Name", options.'Driver Name' )
        ProcessState( "Driver Version", options.'Driver Version' )
    }
...
  • Here is one where I call CheckForUpdate directly (not as part of a schedule) that passes the options without issue.
CheckForUpdate( [ 'Driver Name': "UnifiProtectAPI", 'Driver Version': "0.2.0" ] )
  • Putting the options Map into the handler portion provides an "expecting ')', found 'UnifiProtectAPI' @ line"... error when saving:
schedule( "${ Second } ${ Minute } * ? * *", "CheckForUpdate( [ 'Driver Name': "UnifiProtectAPI", 'Driver Version': "0.2.1" ] )" )
  • Putting similar code in but escaping the quotes to be able to actually save it treats the whole thing as the hander name, which obviously does not exist and gets an error:
schedule( "${ Second } ${ Minute } * ? * *", "CheckForUpdate( [ 'Driver Name': \"UnifiProtectAPI\", 'Driver Version': \"0.2.1\" ] )" )

Map keys: don't need quotes. Lose the spaces in the map key names.

The definition for schedule() looks like this -- the options map is itself within a map parameter to schedule().

schedule(cronString, "handler", [options: [DriverName: "UnifiProtectAPI", ... ]])

def CheckForUpdate(options) {
   if(options) {
      Logging(...)
      ProcessState("Driver Name", options.DriverName)
      ProcessState("Driver Version", options.DriverVersion(
   }
}

I hate to change my naming conventions but OK. I did a super simple sample one though because it still does not appear to be working.

Here is the line calling a simple test schedule (happens right on time, I also tested one without my typical spaces around everything that worked exactly the same) and a sample of the failing log result (I even went to the basic logging just to keep this as simple a test as possible):

schedule( "${ Second } ${ Minute } * ? * *", "TestSchedule", [ options: [ DriverName: "UnifiProtectAPI", DriverVersion: "0.2.1" ] ] )

def TestSchedule( options ){
    log.info( "TestScheduled with options = ${ options }" )
    if( options ){
        log.info( "DriverName = ${ options.DriverName }" )
    }
}

[info]TestScheduled with options = null

You probably can use a key in quotes -- I just use the other way and know that it works. I didn't test the way you are doing it.

I think it needs to be in this format, known to work:

schedule(cronstring, "handler", [data:[..map-elements..], overwrite: true]])

You can omit overwrite, it defaults to true.

1 Like

Changing options to data in all places worked. I am making an edit to the wiki to add a second example to help clarify this.

Thanks for the help on things @bravenel!

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.