OH3: Access historic data within ECMA Script

Hi,

i have not found a solution to access historical data.
I need the maximumSince of an item.
Is there any example?

Thanks.

This should be straight forward. If you have the name of the item, you can get it via the ItemRegistry and then ask the item for the maximumSince. If your script is part of the rule, you already have the item registry in you scope.

The script should look something like that:

'use strict';

var ZonedDateTime = Java.type("java.time.ZonedDateTime");

var sinceDate = ZonedDateTime.now().minusHours(5);
var myPersistentItem = itemRegistry.getItem("MY_PERSITENT_ITEM");
var maximumValueSince = myPersistentItem.maximumSince(sinceDate);

In your case you have to substitute MY_PERSITENT_ITEM with the name of your item.

If you are interested in other historic values of your item, consult the official documentation of the persistence found here Persistence Extensions in Scripts and Rules.

If you need other since dates, have a look on the documentation of ZonedDateTime on how to construct a ZonedDateTime with methods like of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone), parse(CharSequence text), etc.

Hope this helps for a start.

Thanks for your answer, but when i try this i get an error.
[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘1ef0bf58ae’ failed: TypeError: myPersistentItem.maximumSince is not a function in at line number 11
I am using OH3.0 release on docker with default rrd4j persistence.

Can you post your script here. It would be easier than to interpret your error log.

This is the script

'use strict';

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Experiments");

var testItem = ir.getItem("ZW_Switch_Kocher_Power");
logger.info("The pyload is " + testItem);

var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var sinceDate = ZonedDateTime.now().minusHours(5);
var myPersistentItem = itemRegistry.getItem("ZW_Switch_Kocher_Power");
var maximumValueSince = myPersistentItem.maximumSince(sinceDate);

logger.info("test " + maximumValueSince);

If you have the name of the item (“ZW_Switch_Kocher_Power”) I see no need in getting the item into a variable by a REST call. Wouldn’t using:

var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var sinceDate = ZonedDateTime.now().minusHours(5);
var maximumValueSince =ZW_Switch_Kocher_Power.maximumSince(sinceDate);

work in your case?

Sorry Manfred,

I’ve send you in the wrong direction. The documents I referenced and the code I’ve given are intended for DSL scripts. For ECMA script, this is the right way to go:

'use strict';

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 myPersistentItem = ir.getItem("ZW_Switch_Kocher_Power");
logger.info("The pyload is {}", myPersistentItem);

var sinceDate = ZonedDateTime.now().minusHours(5);
var maximumValueSince = PersistenceExtensions.maximumSince(myPersistentItem, sinceDate);

logger.info("Historic item is {}", maximumValueSince);
logger.info("Item name is {}", maximumValueSince.name);
logger.info("Item state is {}", maximumValueSince.state);

I’ve tested the code with one of my items. The key here is to use the PersitenceExtension, which is the javascript object to access historic data of items. The documentation I got from the JavaDoc pages found here:

https://next.openhab.org/javadoc/latest/org/openhab/core/persistence/extensions/persistenceextensions

As you can see, the historic state is an item with name, state and a timestamp.The state has the value, you got from the PersistenceExtension. In you case the maximumSice value.

5 Likes

Thank you very much, this works perfectly.

1 Like