Syntax Help: groovy.lang.MissingMethodException: No signature of method: java.lang.String.active() is applicable for argument types: () values: []

I am hoping someone can help me with a Groovy syntax question. I am sure its something silly, but I am not able to figure out how to fix an error I am getting:
groovy.lang.MissingMethodException: No signature of method: java.lang.String.active() is applicable for argument types: () values: []

I have my alarm panel integrated with SmartThings via AD2Pi and I want my alarm motions to show up like my zwave motions showing activity. I have 3 motions and so I created 3 virtual devices so now I am working on a SmartApp to update the state of these virtual devices when the alarm reports activity. If there is a way to do this in the alarm device code that doesn’t require a SmartApp, even better but I don’t know how to do that. I am new to virtual devices and how they work.

The reason I am getting this error is I am trying to dynamically specify the device in the code based on the event name using a variable. I know I could create a case statement or several If statements to hardcode the device name, but I am sure there is a way to dynamically specify that Xmotion.active(). My alarm has 20 zones so after getting the motions to work, I plan to create virtual devices for the others as well and thus want to simplify my code. This is where I am hoping someone can help me.

Code:

preferences {
    section("Which Alarm?") {
        input "alarm", "capability.alarm", multiple: false, required: true
    }
    section("Kitchen Motion"){
        input "kitchenMotion", "capability.motionSensor", multiple: false, required: false
    }
    section("Foyer Motion"){
        input "foyerMotion", "capability.motionSensor", multiple: false, required: false
    }
    section("Upstairs Motion"){
        input "upstairsMotion", "capability.motionSensor", multiple: false, required: false
    }
}

def installed() {
    initialize()
}

def updated() {
    unsubscribe()
    initialize()
}

def initialize() {
    subscribe(alarm, "kitchenMotion", motionHandler)
    subscribe(alarm, "foyerMotion", motionHandler)
    subscribe(alarm, "upstairsMotion", motionHandler)
}

def motionHandler(evt) {
    def deviceName = evt.name
    def deviceValue = evt.value
    log.debug "deviceName: ${deviceName}, deviceValue: ${deviceValue}"
    if (evt.value == "active") {
        ("${deviceName}").active()
        //"${deviceName}".active()
        //("${deviceName}.active()")
    } else {
        ("${deviceName}").inactive()
        //"${deviceName}".inactive()
        //("${deviceName}.inactive()")
    }
}

You can see I tried many different things like putting () around the deviceName variable in different places, but none of these work. The log.debug statement confirms that ${deviceName} is “kitchenMotion” for example so not sure why I cannot transpose that variable and make it execute “kitchenMotion.active()”.

Thanks in advance!

What do you mean by "execute “kitchenMotion.active()”? kitchenMotion.active() is the value of an attribute of the kitchenMotion device. Assume for a moment that the event was that, kitchenMotion reported active. What do you want to do?

Bruce, thanks for the reply.

I want “${deviceName}”.active() = kitchenMotion.active(). The deviceName variable will be set to one of the following values which match my input values in preferences for the device:
kitchenMotion, foyerMotion, upstairsMotion

I know that deviceName matches these input names by the log output, but the syntax of my statement is causing the error. I am hoping I can dynamically set the device to mark active and inactive versus hard coding something like:
if (evt.name == “kitchenMotion”) {
kitchenMotion.active()
} else if (evt.name == “foyerMotion”) {
foyerMotion.active()
} …

This idea from @tgauchat may work

try

settings."${deviceName}".active()
1 Like

Kevin, thank you very much!! This worked great. I knew it was something simple so I really appreciate your help.

1 Like