Updated() being called twice?

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.



Yea, that’s the way it works.

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.

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

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

1 Like

@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()

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

1 Like

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?