Issues with state variables in custom app

I am writing a custom app to control my humidity. I am using state variables to keep track of my state machine.

in my app settings I see the state variables, but they are not retaining their values.

my code initializes the state variables:

def installed() {
logDebug "Installed with settings: ${settings}"
unsubscribe()
initialize()
}

def updated() {
logDebug "Installed with settings: ${settings}"
unsubscribe()
initialize()
}

def initialize() {
state.relay = 0;
state.timer = 0;
runEvery1Minute(CalculateFeedback)
}

My state machine that uses the state variables, this is the only code that uses or references the state variables:

    switch(state.relay) 
    {
    case 0:
        if (averageIndoorHumidity < tempTargetHumidity)
        {
            state.relay = 1
            state.timer = WaterOnTime;
            // Turn on the Water Relays
            humidifierSwitches.each
            {
                it.on()
            }
            logDebug "Turn On the Water Relay"
        }
        else
        {
            state.timer = 0
            humidifierSwitches.each
            {
                it.off()
            }
        }
        break;
    case 1:
        if (state.timer > 0)
        {
            state.timer = state.timer - 1
            humidifierSwitches.each
            {
                it.on()
            }
        }
        else
        {
            state.relay = 2
            state.timer = WaterOffTime
            // Turn off the water relays
            humidifierSwitches.each
            {
                it.off()
            }
            logDebug "Turn off the Water Relay"
        }
        break;
    case 2:
        if (state.timer > 0)
        {
            state.timer = state.timer - 1
            humidifierSwitches.each
            {
                it.off()
            }
        }
        else
        {
            state.relay = 0
        }
        break;
        
    default:
        logDebug "Fell into the default state!!" 
        state.relay = 0
        state.timer = 0
        break;
    }

These were working previously and now every iteration the state.relay and state.timer are equal to 0.

I don't know if it broke from a code change or from modifying settings. But I have removed the smart app and reinstalled it and it still isn't working. I have also tried to reboot the hub but I have the same experiance.

Any suggestions?

What OS version are you running?

I'm tracking this because I'm not sure if I'm running into the same problem as well. I'm going to keep an eye on this as I update some code.

Hubitat Elevation® Platform Version

2.1.8.117

Hardware Version

Rev C-3

are you sure you want to be using state and not atomicState?

https://docs.smartthings.com/en/latest/smartapp-developers-guide/state.html

Use more log.debug in the code to track what is happening.

I replaced state with atomicState and it is working. But originally it was working with just state.

I put in a log.debug at the entry and exit of the function and it was always zero upon entry and a valid value on exit. So it was not retaining its value as a state variable.

State is written when the app exits (atomicState is written immediately). So, is it possible you had two instances running at the same time? One updates the value to 1, while the other does not; first one exits, writing the 1, only to be followed immediately by the second instance exiting, writing a zero. This is a possible explanation, and is a timing related race condition. I've seen this before, and such an app would not always fail, in fact, it would usually work. Changing to atomicState was the fix for me too.

3 Likes