Item.history not working after migrating to OpenHAB 4.20

Just migrated from 4.19 (32 bits) to 4.20 (64 bits).

Full install from GitHub manually downloaded xz image (RaspBerry Imager fails to download the image with 404 !).
Data and configuration restored from last backup.

Most seems to work, but Item.history

Item.history now fails with error message

failed: org.graalvm.polyglot.PolyglotException: TypeError: undefined has no such function “averageSince”<<

Here is my code:
// Update Average value
var My_Item = items.getItem(‘Lx_01_Illuminance’);
var My_Avg_Item = items.getItem(‘Lx_01_Illuminance_Avg’);

var now = time.ZonedDateTime.now();
var My_Time = time.ZonedDateTime.now().minusMinutes(7);

console.log(utils.OPENHAB_JS_VERSION);

var My_Avg_Val = My_Item.history.averageSince( My_Time );

My_Avg_Item.postUpdate(My_Avg_Val);

Any help would be appreciated.

Best regards to all community members.

See openHAB JavaScript library releases for a full list of changes.

Persistence actions have been enhanced. You now need to use persistence instead of history.

Thank you so much for your answer, Mark.

I saw persistence. It looked great and simple to correct.
Unfortunately, it doesn’t work.

Here is the log messages:

2024-08-08 16:53:45.902 [INFO ] [pt.ui.Set_Item.Lx_01_Illuminance_Avg] - 5.3.1

2024-08-08 16:53:45.913 [INFO ] [pt.ui.Set_Item.Lx_01_Illuminance_Avg] - Item Lx_01_Illuminance State 246.00173280466052 lx History ZZZZ [object Object]!+++!!

2024-08-08 16:53:45.925 [WARN ] [rnal.defaultscope.ScriptBusEventImpl] - State ‘[object Object]’ cannot be parsed for item ‘Lx_01_Illuminance_Avg’.

And the code
// Update Average value
var My_Item = items.getItem(‘Lx_01_Illuminance’);
var My_Avg_Item = items.getItem(‘Lx_01_Illuminance_Avg’);

var now = time.ZonedDateTime.now();
var My_Time = time.ZonedDateTime.now().minusMinutes(7);

console.log(utils.OPENHAB_JS_VERSION);
console.log( 'Item ’ + My_Item.name + ’ State ’ + My_Item.state + ’ History ZZZZ ’ + My_Item.persistence.averageSince( My_Time ) + ‘!+++!!’ );

var My_Avg_Val = My_Item.persistence.averageSince( My_Time );

My_Avg_Item.postUpdate(My_Avg_Val);

See here: JavaScript Scripting - Automation | openHAB

averageSince returns a persistedState object. To get the state, you will have to us My_AvgVal.state. That is what you should send to postUpdate.

There are other methods for persistedState (quantityState, numericState).

Thank you again, Mark.

This is what I ended up with reading the persistence information and modifying/testing the code while you were posting your answer.
I’d better have been waiting for you ! :rofl:

Indeed, the returned value is now a persistedState object.
This makes things a bit more complicated and less intuitive.

For the community: here is my corrected code:
// Update Average value
var My_Item = items.getItem(‘Lx_01_Illuminance’);
var My_Avg_Item = items.getItem(‘Lx_01_Illuminance_Avg’);

var now = time.ZonedDateTime.now();
var My_Time = time.ZonedDateTime.now().minusMinutes(7);

//var My_Avg_Val = My_Item.history.averageSince( My_Time ); Old code not working anymore
var My_Avg_Val = My_Item.persistence.averageSince( My_Time ).state;

//console.log( 'Item ’ + My_Item.name + ’ State ’ + My_Item.state + ’ History ZZZZ ’ + My_Avg_Val + ‘!+++!!’ );

My_Avg_Item.postUpdate(My_Avg_Val);