App 'state' doesn't save state


(Richard Lee) #1

How is the ‘state’ variable in applications supposed to work? From the (minimal) documentation, it seemed that it would be a place to save state across invocations. However, simple examples show that

def startSomething() {
    state.busy = "yes"
}

def endSomething() {
    state.busy = "no"
}

does not actually work. If you log out the value elsewhere, it will be null.

REALLY need some better docs…


(C Chen) #2

Can you post the code? I have been using state in my apps and it seems to work fine. And I just tried the code below in the IDE, and I saw “yes” got printed when I turned “theSwitch” on and then “logSwitch” on. And “no” got printed when I turned “theSwitch” off then “logSwitch” off and then on.

preferences {
  section("Test") {
    input "theSwitch", "capability.switch"
    input "logSwitch", "capability.switch"        
  }
}
def installed() {
   initialize()
}
def updated() {
   unsubscribe()
   initialize()
}
def initialize() {
   subscribe( theSwitch, "switch.on", startSomething )
   subscribe( theSwitch, "switch.off", endSomething )
   subscribe( logSwitch, "switch.on", logSomething )    
}
def startSomething( evt ) {
   state.busy = "yes"
}
def endSomething( evt ) {
    state.busy = "no"
}
def logSomething( evn ) {
    log.debug "${state.busy}"
}

(Tony Gutierrez) #3

I’m also getting some flakiness in state vars.