q; how to instantiate PersistenceExtensions for a none default persistance service.
I’m using rrd4j as working db and postgresql for long therm historical data.
the rrd4j is the default service.
if I run the code below using rrd4j as default service then the rrd4j values are correct but no jdbc values are returned.
if I run the code below using jdbc as default service then the no rrd4j and no jdbc values are returned.
now I’ need to get historc values from jdbc
from the documentation I’ found the following but so far I did not manage to get it running.
PersistenceExtensions(PersistenceServiceRegistry registry TimeZoneProvider timeZoneProvider)
rules.JSRule({
name: "last Historic Item",
description: "find last historic item",
triggers: [triggers.TimeOfDayTrigger('00:00')],
execute: (event) => {
'use strict';
var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var begin = time.toZDT('00:00:00').minusDays(1);
var end = time.toZDT('23:59:59');
console.log("time from ", begin, " to ", end);
// RRD4J history
var defaultPersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");
var rrd4Item = items.getItem("room_8_device1_dht22_temperature");
console.log("rrd4jc Item is: ", rrd4Item.name, " ", rrd4Item.label, " ", rrd4Item.state);
var rrd4ValueCount = defaultPersistenceExtensions.countBetween(rrd4Item, begin, end);
console.log("rrd4j Persistent Item count: ", rrd4ValueCount);
var minRrd4jValue = defaultPersistenceExtensions.minimumBetween(rrd4Item, begin, end);
if( minRrd4jValue === null) {
console.log("rrd4j Persistent Item minimum: not found");
} else {
console.log("rrd4j Persistent Item minimum: ", minRrd4jValue.name, " ", minRrd4jValue.timestamp, " ", minRrd4jValue.state);
}
var maxRrd4jValue = defaultPersistenceExtensions.maximumBetween(rrd4Item, begin, end);
if( minRrd4jValue === null) {
console.log("rrd4j Persistent Item maximum: not found");
} else {
console.log("rrd4j Persistent Item maximum: ", maxRrd4jValue.name, " ", maxRrd4jValue.timestamp, " ", maxRrd4jValue.state);
}
// JDBC history
// this is not working
// var jdbcPersistenceExtensions = new PersistenceExtensions(PersistenceExtensions.get('jdbc'), ZonedDateTime.getZone());
var jdbcItem = items.getItem("room_0a_temperature_1");
console.log("jdbc Item is: ", jdbcItem.name, " ", jdbcItem.label, " ", jdbcItem.state);
var JdbcValueCount = defaultPersistenceExtensions.countBetween(jdbcItem, begin, end);
console.log("jdbc Persistent Item count: ", JdbcValueCount);
var minJdbcValue = defaultPersistenceExtensions.minimumBetween(jdbcItem, begin, end);
if( minJdbcValue === null) {
console.log("jdbc Persistent Item minimum: not found");
} else {
console.log("jdbc Persistent Item minimum: ", minJdbcValue.name, " ", minJdbcValue.timestamp, " ", minJdbcValue.state);
}
var maxJdbcValue = defaultPersistenceExtensions.maximumBetween(jdbcItem, begin, end);
if( maxJdbcValue === null) {
console.log("jdbc Persistent Item maximum: not found");
} else {
console.log("jdbc Persistent Item maximum: ", maxJdbcValue.name, " ", maxJdbcValue.timestamp, " ", maxJdbcValue.state);
}
},
tags: ["Test", "historicItem"],
id: "findLastHistoricItem"
});
with above code and rrd4j as default service:
```csv
10:14:28.979 [INFO ] [n.script.file.findLastHistoricItem.js] - time from 2024-07-23T00:00+02:00[SYSTEM] to 2024-07-24T23:59:59+02:00[SYSTEM]
10:14:28.983 [INFO ] [n.script.file.findLastHistoricItem.js] - rrd4jc Item is: room_8_device1_dht22_temperature OG Wohnzimmer Sensor 1 Temperatur 23.9 °C
10:14:29.111 [INFO ] [n.script.file.findLastHistoricItem.js] - rrd4j Persistent Item count: 1372
10:14:29.247 [INFO ] [n.script.file.findLastHistoricItem.js] - rrd4j Persistent Item minimum: room_8_device1_dht22_temperature 2024-07-23T07:38+02:00[Europe/Vienna] 23.40272727272727 °C
10:14:29.390 [INFO ] [n.script.file.findLastHistoricItem.js] - rrd4j Persistent Item maximum: room_8_device1_dht22_temperature 2024-07-23T21:52+02:00[Europe/Vienna] 25.7 °C
10:14:29.394 [INFO ] [n.script.file.findLastHistoricItem.js] - jdbc Item is: room_0a_temperature_1 UG Eingang Temperatur 18.6 °C
10:14:29.399 [INFO ] [n.script.file.findLastHistoricItem.js] - jdbc Persistent Item count: 0
10:14:29.403 [INFO ] [n.script.file.findLastHistoricItem.js] - jdbc Persistent Item minimum: not found
10:14:29.408 [INFO ] [n.script.file.findLastHistoricItem.js] - jdbc Persistent Item maximum: not found