Help - Use of IF statement in SmartApp

I feel like I’m taking crazy pills here - I’ve spent upwards of a few hours trying variations on this code and it just isn’t performing as expected.

def x = true
def y = false

def eventHandler(evt) {
    if (x == true) log.debug "x is true"
    else log.debug "x is false"
    if (y == true) log.debug "y is true"
    else log.debug "y is false"
}

According to my logs, both X and Y are false. I’ve tried about a hundred variations, including looking up Groovy tutorials and working it with both strings and integers, but nothing works as expected. I’m sure this is just a rookie mistake, so I’d appreciate if someone can point out my error.

There are no “global” variable definitions allowed in SmartThings.
You can only do variable def’s within a method or…

You can use the official pre-shared global map calledstate”…

e.g.,

state.x = true
state.y = false
...

##NB: Groovy Web Console
Though it doesn’t deal with all the quirks of the SmartThings sandbox environment, one good way to play with Groovy code is the free Groovy Web Console

https://groovyconsole.appspot.com/

7 Likes

Unbelievable. The best part is, I don’t think I’ll have any trouble simply declaring my variables inside the event handler. Thanks a ton for your help.

2 Likes