Multiple Switches in Groovy?

(JIm) #1

Trying to learn the basics of Groovy.

If I have an input selection to select more than one switch. Then in the handling routines I can’t seem to figure out how to select which switch to turn on/off or whatever. I can see how to do it for just one, but not how to select particular ones. I’m sure it’s simple, just not seeing it.

(Arn B) #2

Probably not exactly what you want to do, but it may help. Change the name ‘thesiren’ to whatever you have defined
if (settings.thesiren)
thesiren.each //fails when not defined as multiple contacts
if (it.hasCommand(“beep”))
it.beep([delay: 2000])
it.on([delay: 2000])[delay: 2250])

I beleive to do all of them at one time it’s “thesiren(beep)”

(JIm) #3

Another Groovy question. I have an existing smartapp I was modifying for my own use. In that app there is a variable that I am trying to compare. Here is what I have:

def MyUserName = settings."userName${i}"
If ({MyUserName} = “John”) {
do something

I can’t seem to get the if to work.

(Arn B) #4

Maybe this will work?
def MyUserName = settings.userName(i)
If (MyUserName == “John”)
{do something}

A single = sets MyUserName to “John”

AFAIK the {} signs around a variable name are used when putting out the contents a defined field in a message
log.debug “my user name is {MyUserName}”

If you have not done so already, suggest following the documentation for Writing Your First SmartApp.

(JIm) #5

l did have the double =, just mis typed my post. I will try the other changes. Thanks for the help.

(JIm) #6

Found this example in someone elses post. It works for string compare.