Duplicate commands with different passed parameters

I noticed in the generic Dimmer Switch device that there are two setLevel() commands. One accepts just the value, and the other accepts both value and duration. Is groovy smart enough to know which to use based on the parameters passed?

Say a command has 2 parameters. Length and direction. Could I have 3 versions of the command like: start(), start(length), start(length, direction) that would be called based on whether none, one, or two parameters are passed? The command would choose defaults for any missing parameters.

You don’t HAVE to pass a parameter. But if you don’t need a parameter to be passed it would be weird to add one to the method.

If you want to have parameters be optional then set a default parameter for the method. This is how our setlevel command works.

So we do this on the backend:

setLevel(value, duration = null)

Here the value parameter is required, but duration is optional because a default value has been set. If no value is passed it will be set to null, if a value is passed it will be set to the passed value.

Here it is in our Docs: http://docs.smartthings.com/en/latest/getting-started/groovy-basics.html#defining-methods

1 Like

Thanks Tim! It’s not so much that the parameters aren’t needed, but the actual command can accept varying levels of parameters and I want to add the flexibility to the command, particularly for potential SmartApps. Sounds like my best bet is to use a Map as the passed parameter, then check the various values in the map to see whether they’re populated to determine what action to take.

My primary use is for adding colorLoop functionality to zigbee color bulbs (like directly attached Hue, LightStrips, Osram RGB, etc…). The zigbee colorLoop command set can accept 4 parameters, but you can update just 1 or all 4 depending on your goal. I was hoping to develop a single command that would take the appropriate action depending on what was passed, so the user could control one function or all at once.

I guess I could update all 4 in the device command and use custom attributes for non-passed parameters. I think the setColor works this way.

1 Like