Page 1 of 2

Smartthings: possible refresh solution?

PostPosted: Sat Jan 21, 2017 1:51 pm
by R305
Hey Kevin & Scott,

Background:
I have an AD2Pi and a SmartThings hub, and have been having a problem where the alarm status in Smartthings does not update when we come home and disarm. So my smartthings actions that key off of the alarm disarming do not trigger. I would have to go into the Alarmdecoder item and hit "Refresh" for it to change to disarmed. Then things would trigger. Then I noticed in the "recent events" log that after install it was getting a refresh command every 1 minute. I found this in the groovy code that you setup with a cron command. For some reason (I still don't know why) after the first arm/disarm cycle, the cron schedule got removed. That part of the code doesn't get called again to establish the cron schedule. So that's where the update problem seems to have come from.

Possible Solution:
So I was hacking around trying to get the cron to be called if for some reason it was cancelled. Reading about it, it has a 1 minute limit on how often the function is called. So, to solve the update issue I also stumbled on a way to refresh it faster than every 1 minute. Instead of using the recurring scheduling system of cron, I decided to chain together a runIn() schedule at the end of every update. With runIn() you can schedule function calls as soon as 1 second in the future. So right now I have it updating / refreshing every 10 seconds. The clock in Smartthings is not super steady in the 1 second range, so I don't want the calls to overlap.

Here are the groovy code edits I made to the AlarmDecoder (Service Manager) Smart app. Seems stable after about a day of use. Can you tell me if you think this creates any new problems?
(Note: this is not the whole code, just the parts I edited)

In the 'Initialize()' function call refreshHandler() instead of scheduleRefresh():
Code: Select all
    /* commented out for runIn()
    scheduleRefresh()
    */
    refreshHandler()


Commented out 'scheduleRefresh()' completely
Code: Select all
/* commented out for runIn()
def scheduleRefresh() {
    def minutes = 1

    def cron = "0 0/${minutes} * * * ?"
    schedule(cron, refreshHandler)
}
*/


Added the runIn() command (set to 10 seconds) at the end of 'refresh_alarmdecoders()'
Code: Select all
def refresh_alarmdecoders() {
    log.trace("refresh_alarmdecoders-")
    getAllChildDevices().each { device ->
        // Only refresh the main device.
        if (!device.deviceNetworkId.contains(":switch"))
        {
            log.trace("refresh_alarmdecoders: ${device}")
            device.refresh()
        }
    }
    runIn(10,refreshHandler)  // Just added
}


Thoughts and/or criticisms?

Rob

Re: Smartthings: possible refresh solution?

PostPosted: Thu Jan 26, 2017 10:44 am
by R305
Minor Update:

This modified code has been running for almost a week now and seems to be stable. I have also increased the refresh rate from every 10 seconds to every 5 seconds, with no detrimental effects I can see. It now approximates the responsiveness of my other Zigbee and Zwave devices connected to the Smartthings hub.

So, I think it works - the 1 minute lag may be gone. But before declaring victory, I would love to hear from Kevin and/or Scott about whether or not he thinks this mucks up something else. Thoughts please?

Rob

Re: Smartthings: possible refresh solution?

PostPosted: Thu Jan 26, 2017 11:05 am
by kevin
Hey if it's working better and it's not interfering with anything then I'd say it's a win. If you want to submit a Pull Request we can merge your changes into the project.

Re: Smartthings: possible refresh solution?

PostPosted: Mon Jan 30, 2017 4:53 pm
by R305
OK Kevin, will do. Might take a little while longer though since I would hate to pass on any real problems it might have. I've made a few tweaks for stability so far to that end.

Keep up the good work. I'm a fan of this device!

Rob

Re: Smartthings: possible refresh solution?

PostPosted: Fri Mar 03, 2017 12:14 pm
by ratty
How is this refresh change going, will it be added to the alarmdecoder-smartthings gihub repository?

Kevin, are you planning on adding a alarmdecoder : VirtualMotionSensor device handler to go with the alarmdecoder : VirtualContactSensor you have in now?

Re: Smartthings: possible refresh solution?

PostPosted: Fri Mar 03, 2017 12:29 pm
by kevin
I'm not planning for anything - that is a question for Scott - all of this is greek to me :)

Re: Smartthings: possible refresh solution?

PostPosted: Mon Mar 06, 2017 6:52 am
by ratty
kevin wrote:I'm not planning for anything - that is a question for Scott - all of this is greek to me :)


Greek or geek? :lol:

Re: Smartthings: possible refresh solution?

PostPosted: Thu Mar 23, 2017 1:38 pm
by R305
Yeah, so....

R305 wrote:Might take a little while longer though since I would hate to pass on any real problems it might have.


Turns out my update is in directly conflict with one of SmartThings' coding guidelines about chaining multiple runIn() commands. It's literally right in the code reference (DOH). Am working on a better solution that will be more stable and hopefully worthy of distributing. Apologies for whipping out the code above before thoroughly checking it.

Hopefully more to come on this.

Rob

Re: Smartthings: possible refresh solution?

PostPosted: Thu Apr 20, 2017 9:58 am
by kevin
We should have a release this week that addresses this refresh issue as well as some other issues. Along with smart home monitor integration.

Re: Smartthings: possible refresh solution?

PostPosted: Thu Apr 20, 2017 2:43 pm
by kevin
New webapp and smartthings are released - the refresh issue is gone (new setup instructions) and there is smart home monitor integration.