Update 4.2: JScript averageSince syntax changed?

running OH 4.2.0-release on openHABian since upgrade from 4.1.2.

  • value of the item EMS_Ueberschuss is “-2542.211483294 W” presently
  • default persistence is RRD4j and is filled (see pic)

The following JSScripting worked flawlessly:

var interval10min = time.ZonedDateTime.now().minusMinutes(10);
var ueberschussAVG = Number.parseFloat(items.getItem("EMS_Ueberschuss").history.averageSince(interval10min));

now I changed it to:

var interval10min = time.ZonedDateTime.now().minusMinutes(10);
var ueberschussAVG = Number.parseFloat(items.getItem("EMS_Ueberschuss").averageSince(interval10min));

but still in the logs:

2024-07-18 15:58:30.401 [ERROR] [mation.script.javascript.EmsBatLogic] - Failed to execute script: TypeError: (intermediate value).getItem(...).averageSince is not a function
        at <js>.:program(<eval>:21)
        at org.graalvm.polyglot.Context.eval(Context.java:399)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426)
        at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
        ... 21 more
2024-07-18 15:58:30.402 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'EmsBatLogic' failed: org.graalvm.polyglot.PolyglotException: TypeError: (intermediate value).getItem(...).averageSince is not a function

What did I miss while upgrading to 4.2.0?

As far as I can see the problem is that you should replace „history“ with „persistence“. You just removed „history“.

Thanks. I thought it was only “deprecated”, but not removed in 4.2.0 already… :astonished:
and then I had a look in the docs:

And there it only said: <item>.averageSince(ZonedDateTime). But with replaced “persistence” it now works:

var interval10min = time.ZonedDateTime.now().minusMinutes(10);
var ueberschussAVG = Number.parseFloat(items.getItem("EMS_Ueberschuss").persistence.averageSince(interval10min));

Those pages are for the raw Java APIs. Since Rules DSL use the raw Java that page also covers Rules DSL. However, in all other cases, the first place you should look is the docs for the automation add-on you are are using, in this case JS Scripting.

jRuby and Python do it differently too. I think Groovy uses the raw Java APIs like Rules DSL.

2 Likes