Location.mode is lagging one mode behind


(TAG) #1

Hello,

I am having a bizarre issue where the location.mode is not evaluating to the current mode, more specifically - it is evaluating to the previous mode. I originally thought it was something to do with my program, then that it was something to do with a different smartapp. I ended up unistalling the other smartapps and copied the “keep me cozy” example (but deleted most of the code so the only remaining is checking the state and printing it to the log. Problem still exists.

Here is the sequence I see:

  • Start app (mode is home), smartapp shows location.mode=home
  • change mode to away, smartapp shows location.mode=home
  • change mode to home, smartapp shows location.mode=away
  • change mode to night, smartapp shows location.mode=home
  • change mode to home, smartapp shows location.mode=night

Any ideas? Here is the last code I tried:

preferences {
}

def installed()
{
subscribe(location)
subscribe(app)
}

def updated()
{
unsubscribe()
subscribe(location)
subscribe(app)
}

def changedLocationMode(evt)
{
log.debug “Current mode = ${location.mode}”
}

def appTouch(evt)
{
log.debug “Current mode = ${location.mode}”

}

// catchall
def event(evt)
{
log.debug “Current mode = ${location.mode}”
}


(Brian) #2

First thing i see is your subscribe statements arent referring to your handlers. the only thing working in your code right now is the catchall

subscribe(location, changedLocationMode)
subscribe(app, appTouch)

(TAG) #3

Thanks for the comments - I am still learning, and literally just copied that section from the example.

I changed the code for the subscribes. It still acts the same way. I noticed that if I manually trigger through the “trigger now” button, that it updates correctly. Changing the mode immediately breaks the smartapp again…as it states states the previous change once again (the state that was shown with the manual trigger).

preferences {
}

def installed()
{
subscribe(location, changedLocationMode)
subscribe(app, appTouch)
}

def updated()
{
unsubscribe()
subscribe(location, changedLocationMode)
subscribe(app, appTouch)
}

def changedLocationMode(evt)
{
log.debug “Current mode = ${location.mode}”
}

def appTouch(evt)
{
log.debug “Current mode = ${location.mode}”

}

// catchall
def event(evt)
{
log.debug “Current mode = ${location.mode}”
}


(Brian) #4

Its a timing thing. You are reading the location.mode before its actually had a chance to change.

preferences {

}

def installed()
    {
        subscribe(location, changedLocationMode)
        subscribe(app, reportTheMode)
    }

def updated()
    {
        unsubscribe()
        subscribe(location, changedLocationMode)
        subscribe(app, reportTheMode)
    }

def changedLocationMode(evt)
    {
        log.debug "Mode event happened"
        runIn(3, reportTheMode)
    }

def reportTheMode(evt)
	{
		log.debug "Current mode = ${location.mode}"
    }

(TAG) #5

Brilliant! That was the problem - thanks!!!


(Tony Gutierrez) #6

Instead of doing that, just look at evt.value for the new mode. It will have the current value you want


(TAG) #7

I was having intermittent issues with the previous fix. I just made the change you suggested and it appears to work. I will run it a few days to confirm it is solid.

Thanks for the help!


( Cosmo) #8

Sorry to resurrect a ridiculously old thread, but how would I go about adding a “current location mode” tile to a DTH. I’m thinking a value tile, simply reads the current hub location mode.

I couldn’t find anything more relevant.
I’m trying to add the tile to a virtual minimote DTH with its button push/held 1-4 assigned to toggle the hub mode manually. I just want to have it within the same DTH so I don’t have to switch from the (virtual) device DH to the “more” tab to see.

I can copy the code here if necessary, but I just want to add this single “display” tile to the DH