Groovy syntax question


I’m having a bit of a time figuring out the proper syntax to parse the value from between the “[ ]”.
For example:

x = currentState.value
How do I get just the “on” value without resorting to a crude replace method?

Thank you.

Assuming I have a switch declared in my smartapp as mySwitch, the following returns “x=on”

def x=mySwitch.currentState("switch")
log.debug "x=${x.value}"

You could strip the first and last characters using substring:

x = currentState.value
x = ​​​​​​​x.substring(1, x.size() - 1)

Thank you guys for your replies.

@philh30: your example copied verbatim returns:


@gvitzi: Your code returns:

groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.substring() is applicable for argument types: (java.lang.Integer, java.lang.Integer) values: [1, 0]
Possible solutions: toString(), toString(), toString(), toString(), subList(int, int), subList(int, int) 

So I’m sorry to report I remain in the same boat. Can anyone please offer a solution? I’m sure this can’t be the an unusual need or not have a specific means to return just the value without brackets.

From the exception you got, it appears that currentState.value returns an ArrayList
Meaning that [on] is a list with one item “on”

In this case you could get the first item this way:

x = currentState.value[0]

1 Like

Which essentially works out to:

YOU, @gvitzi = officially my hero!

Thank you!!!