Updated() being called twice?


(Jonathan Bisson) #1

Hey guys,

My updated() function for all my device handler are always being called twice when I edit their configuration and click save.

Is it as expected ? I couldn’t find any documentation on it.

thanks

jon.


Do Device Handlers Officially Support the "updated()" method?
Schedule() runs but is not sending commands to device
(Mike Maxwell) #2

Yea, that’s the way it works.


(codersaur) #3

Really? why? I’ve always assumed this is a bug.

In most of my device handlers, the update() command does various things (e.g. updating device parameters), and it seems inefficient to run it twice. I am currently thinking of adding code to prevent update() running twice, say with 5 seconds, to prevent the double execution.


(Don Caruana) #4

That’s what I started doing as well (coding to avoid double execution). Why it runs twice is bizarre to me.


(Mike Maxwell) #5

Lol, I didn’t say it was right!, it clearly isnt… me have bigger fish to fry…


#6

@zcapr17 That is what I do

def updated() {
    // Needed because updated() is being called twice
    if ( (Calendar.getInstance().getTimeInMillis() - state.updatedDate) < 5000 ) return

    /* MY CODE */

    //Part of workaround because updated() called twice
    state.updatedDate = Calendar.getInstance().getTimeInMillis()
}

(codersaur) #7

Thanks, I do something pretty similar, but instead of Calendar.getInstance().getTimeInMillis() I use now().


(Jack) #8

Hi. I’ve been trying to use your fix, but I am getting an error saying:

error groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method java.lang.Long#minus.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[class java.lang.Character]
[class java.lang.Number] @line 223 (updated)

How can I fix this?