[Solved] Access PersistenceExtensions in Rule

Hi all,

I have this tiny ECMA script on my OH3:

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Experiments");
var PersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");

var UpdateItemDate = ir.getItem("CovidLK_lastUpdateDiepholz").state;
logger.info("UpdateItemDate is: {}", UpdateItemDate);

var myPersistentItem = ir.getItem("CovidLK_Inzidentpro100kDusseldorf");
logger.info("Persistence item is: {}", myPersistentItem);

var maximumValueSince = PersistenceExtensions.maximumSince(myPersistentItem, UpdateItemDate);

My issue is to access the PersistenceExtensions using the UpdateItemDate item. This is returned from a JSONPATH transformation in a given format:

  - id: lastUpdateDiepholz
    channelTypeUID: http:string
    label: last Update Diepholz
    description: null
    configuration:
      stateExtension: "03251"
      stateTransformation: JSONPATH:$.meta.lastUpdate

I’m now struggeling in converting that DateTime format to the correct format, required to access the PersistenceExtenstions. The item is now of type ‘String’, but I also tried with type ‘DateTime’.

The log of the above script returns:

2021-04-05 07:57:49.966 [INFO ] [enhab.model.script.Rules.Experiments] - UpdateItemDate is: 2021-04-05T00:00:00.000+0000
2021-04-05 07:57:49.984 [INFO ] [enhab.model.script.Rules.Experiments] - Persistence item is: CovidLK_Inzidentpro100kDusseldorf (Type=NumberItem, State=97.44692278376591, Label=Inzident pro 100k Düsseldorf, Category=, Tags=[Point], Groups=[CovidLK])
2021-04-05 07:57:49.985 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: 1
java.lang.ClassCastException: Cannot cast org.openhab.core.library.types.DateTimeType to java.time.ZonedDateTime
	at java.lang.Class.cast(Class.java:3605) ~[?:?]
	at jdk.nashorn.internal.scripts.Script$Recompilation$10113$\^eval\_$cu1$restOf.:program(<eval>:11) ~[?:?]
	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:456) ~[jdk.scripting.nashorn:?]

Hope you can help me here!

Many thanks.

Matthias

As documented, the second argument must be a ZonedDateTime. Assuming CovidLK_lastUpdateDiepholz is a DateTime Item, you just need to pull the ZonedDateTime.

items["CovidLJ_lastUpdateDiepholz"].getZonedDateTime() // there is no reason to pull an Item from the ir just to get it's state

If it’s a String Item, why? Make it a DateTime Item.

Many thanks. That solved exactly my issue.