setLevel Method Platform Dependency

Does anyone know if the setLevel method with duration is used for anything other than the SetLevel command button in the device driver config page?...

def setLevel (level, duration) {
    ...
}

image

In Rule Machine creating a "fade dimmer level over time" action just calls the setLevel method without a duration parameter repeatedly:

image

def setLevel (level) {
    ...
}

I'm asking this because the dimmer device I'm writing a driver for only supports a transition of 5 seconds, so the duration parameter of SetLevel would seem a bit superfluous in this instance, however if it's a critical requirement for automations to work correctly, I'd attempt a workaround.

I would just make the duration optional in your case. That way it can be called from either method and you can ignore the duration. I have a few drivers that use setLevel but have no need for duration so I just ignore it.

def setLevel(level,duration=null)

1 Like

:point_up_2: This is what I do as well in my drivers that implement the setLevel command.

Thanks. That's certainly a sensible approach, however I suppose what I was asking is if I can only partially support the duration parameter (because the device has a 5 second limit) is anything else likely to break e.g. a platform dependency or Rule Machine etc. By implication from your answers, this would seem not to be the case.

I appreciate this might all be a case of trial and error.

Thanks

The RM action you are looking for that will call setLevel with both a level and duration is the plain "Set Dimmer Level" action, which has an optional "with this fade" option. (The one you are using above mimics a similar effect over a longer period of time with repeated calls, as you noticed--I'd say the intended use is more for things like a "gentle wake up" rule, not a regular setting of a dimmer level.) But RM is really beside the point--any app should be able to call setLevel with either one or two paramaters and not fail.

Per the Switch Level capability, drivers are required to implement both setLevel(level) and setLevel(level, duration). The above advice of defining a setLevel(level, duration=null) method in Groovy does this by making the second parameter optional and defaulting to null, whose value you can do with as you please. Hubitat custom would probably say this should use the value of a "Default transition time" preference in your driver if this parameter isn't specified (so if duration == null in this case; I'd be careful about just checking if (duration) since a value of 0 should probably be valid--for an ASAP transition--and both are false under Groovy truth), but there aren't any hard rules. What you actually do with the duration parameter, specified or not, is up to you, but if I were you, I'd use it where possible and just scale anything above five seconds down to that (if the device itself doesn't do that for you). The user would have to know this, but you could document it somewhere--lots of devices have little quirks like this.

3 Likes

Many thanks @bertabcd1234.