Homemade App getting old value for "lockCode"

I wrote an app that will program a door lock code using setCode(), then wait 5 seconds and check the value of lockCodes to verify that the setCode() function worked, and if not, try up to 4x more. I can see in the events for the door lock that the that value of lockCodes gets updated within a few seconds after setCode(), but each time my app queries lockCodes, even up to 20 seconds after the first execution of setCode(), the value of lockCodes is still the same as it was before I ran my app's function.

def findExistingAirbnbCodePosition(lock) {
    int airbnbCodePosition = 0
    Boolean codeAvailable = false
    if(debugMode) log.debug "Getting the door lock codes"
    def codes = lock.currentState("lockCodes").value
    if(debugMode) log.debug "The door lock codes from ${lock} are ${codes}"
    def codeJson
    codeJson = new JsonSlurper().parseText(codes)
    for(codePosition in codeJson) {
        if(debugMode) log.debug "The code position is ${codePosition.value} in ${codePosition.key}"
        for(codeData in codePosition.value) {
            if(codeData.key == "name") {
                if(debugMode) log.debug "The code name is ${codeData.value}"
                if(codeData.value == "RentalAutomator") {
                    if(debugMode) log.debug "KEY FOUND at position ${codePosition.key}"
                    airbnbCodePosition = codePosition.key.toInteger()
                    codeAvailable = true
        if(codeAvailable) break
    if(codeAvailable) {
        if(debugMode) log.debug "The availaable code position being returned is ${airbnbCodePosition}"
        return airbnbCodePosition
    return null

You can see in the screenshot below that the setCode() was run at 2023-11-19 10:53:19.821 PM, and then when I checked the value of lockCodes at 2023-11-19 10:53:29.850 PM, that code #3 "RentalAutomator" was not in the list. Then, if you reference the screenshot below that, you'll see a snip from the door lock device's event where this shows that the values for lockCodes was updated at 10:53:22.351 PM, which is also seen in the first screenshot.

Not sure if its hurting anything but the currentValue already returns the value, so the .value at the end is not needed.

Also what is being passed to the function exactly (variable lock)? I would assume a device object.

Have you tried subscribing to the lockCodes attribute of the device? That would probably work better than a delay. You could have a runIn delay that times out if the subscription never triggers and finds the new code.

1 Like

Looks like you didn't provide this possibly helpful context above, but I see a recent post where you wanted to use pauseExecution() instead of runIn(). With pauseExecution(), you are likely still using the device as cached when your app began running. Using runIn(), you would avoid that. I would suggest using runIn() instead -- this is almost always preferable.

If that is what you are doing here, that is probably related.

If you wanted to see what you could do without that. you could try passing true as the second parameter to currentValue(), meaning it will skip the cache. (The default is false.) I do not know if this will work around the above issue regardless, but if anything can....

Otherwise, the above are good ideas too.


I have a very similar "app" that works perfectly in Rule Machine to create new codes via text message or dashboard. In my case, I monitor the lock attribute "codeChanged" for value equals "added" after any attempt to add a new code or user. If the value doesn't report "added" within 30 seconds, it tries to add the new code again. Once the value of "codeChanged" for that lock equals "added", it then considers that confirmation that the new code has been stored to the lock.

I am now working on adding an additional logic that will create a global variable with the lock code, slot number, and code name and then send a notification to my phone after the lock reports the new code has been added.

Thank you. This worked!

1 Like

Yup, I noticed too. Removed.