Need help trying to dynamically change the constraints for a command option

I'm trying to do something that I think should be simple, but am getting stuck.

I have a driver with a custom command and a drop-down selection. I'd like to be able to change what is in the drop-down as the driver runs.

This is something that is pretty easy to do with "enum" types in a preference, but I can't seem to make it work with a command. Can it be done (maybe there's a undocumened data structure holding the options for commands and that could be manipulated).

As a simple coding example, see below and compare the "preference" versus the "command". In a preference, you can have a simple method that returns a list to change the "enum" drop down over time. In the command, however, you can also include a method to define the enum, but it seems to only use the values the first time the driver loads. Not very helpful for what I'm trying to do here.

import groovy.transform.Field
@Field static List<String> itemList = ["00"]
@Field static Integer count = 0

metadata {
    definition (name: "Control Selection Test", namespace: "zigbeeTools", author: "ABCDE") {
 
        command "testSelector", [[name:"selectedItem", type:"ENUM", constraints:( getItems() )]]
    
        command "showData"
    }
    preferences {
        input name:"selectedItem2", title: "Dynamic Preference", type:"enum", options:getItems(), required: true 
    }
}

List<String> getItems() {
    count++
    itemList << "${count}".padLeft(2, "0")
    log.debug "Returning list items ${itemList}"
    return itemList
}

void testSelector(selectedItem){
    log.debug "Selected: ${selectedItem}"
}

Capabilities and commands do not offer dynamic features, as far as I am aware. You can do a bit with preferences because they are rendered on page load for that specific device (and the values, if set, stored for that particular installed instance); I assume the rest is because it's shared among all devices that use the driver and needs to be known at any time.

You can do this with commands in general. I do it like this:

command "adjustMicrowaveState", [[name: "Cook status*", type:"ENUM", description:"Cook status mode to set", constraints: cookStatusNames()]]

I suspect that there is a scope or lifetime issue with the Field approach that you're using. @jvm33, what does the enum selector for the command show relative to your initialized value for the Field? Try declaring a different List in that method and return that instead.

I think you need to find a way to declare a mutable object at script-level scope with a lifetime that works for your use case. Someone else here hopefully has more knowledge than me about how to accomplish that.

I think what is happening is that, if the function defining the command's constraint values (i.e., your cookStatusNames()) can be evaluated when the driver is first saved to the system -- basically, at compile time -- then it works. But it can't be adjusted thereafter.

EDIT: I tried to cook up an example, but I couldn't make it work before I ran out of ideas. So, you are probably right. :wink:

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