item.persistence.lastUpdate() not always working

Hello everyone,

after the migration to version 4.2.1 I have the following behavior, but I’m not sure if it was the same before:

In a js-rule I call the .persistence.lastUpdate() and have the problem that very often null comes out!

2024-09-24 09:46:03.484 [INFO ] [.file.generic_min-_and_max_values.js] - Generic Min- and Max values js | 1727163963451 | forEach | loopKey: minDay item.persistence.lastUpdate(): 2024-09-24T09:46:03+02:00[Europe/Berlin]
2024-09-24 09:50:41.634 [INFO ] [.file.generic_min-_and_max_values.js] - Generic Min- and Max values js | 1727164241625 | forEach | loopKey: minDay item.persistence.lastUpdate(): null

The database looks like this:
image

The item is defined as follows:

item = items[event.itemName];

The rule is very long and contains some loops…

Can anyone explain this behavior? Is this simply a runtime error, but if so, why isn’t an older date returned instead of null?

openHAB 4.2.1
persistence: jdbc as standard

Hi Tobias,

see discussion on this thread.

long story short:

  • if there’s an update of the item value in parallel to the time your lastUpdate-call, the response is ‘null’
  • there’s an lastUpdate attribute in the making on the item itself - no persistence needed, coming with 4.3 if all works out
  • for the time being, you could insert an if-clause on the lastUpdate for “null” and then try again with the next rule-call…

Ahh, didn’t see that :slight_smile:

As a workaround, I now have a while loop that waits for the result for a maximum of 5 seconds:

        if(item.persistence.lastUpdate() == null)
        {
            var sleep = true;
            while(sleep == true)
            {   
                //log(ruleName + ' | sleep | item.persistence.lastUpdate(): ',item.persistence.lastUpdate());
                var newTime = time.ZonedDateTime.now().toInstant().toEpochMilli();
                if((newTime-timeStamp) >= 5000 || item.persistence.lastUpdate() != null)
                {
                    log(ruleName + ' | sleep | item.persistence.lastUpdate(): ',item.persistence.lastUpdate());
                    sleep = false;
                }
            }
        }
        if(item.persistence.lastUpdate() == null)
        {
            log(ruleName + ' | .lastUpdate() is null!' );
            return;
        }

Maybe someone can still use this :slight_smile:

You can use simpler workaround:
replace ITEM.lastUpdate in ITEM changing DSL rules to ITEM.previousState.getTimestamp

or wait till 4.3 where it may work as per item.persistence.lastUpdate() not always working

Is this also possible in javascript?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.