Instantiate PersistenceExtensions for none default persitence service

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

First, why not use the persitence properties on the Item? You don’t need to import PersistenceExtensions.

var rrd4ValueCount = rrd4Item.persistence.countBetween(begin, end);

Even if you didn’t want to use that (which is the recommended way), you still don’t need to import the Java class yourself. It’s already imported for you in actions.

var defaultPersistenceExtensions = actions.PersistenceExtensions;

You have to tell it which persistence you want to use when you don’t use the default. Since rrd4j is the default persistence, you need to tell it to use JDBC.

var JdbcValueCount = jdbcItem.persistence.countBetween(begin, end, "jdbc");

See JavaScript Scripting - Automation | openHAB

The last argument to all the persistence methods is an optional one and it’s the name of the service to query if you don’t want to use the default service.

Thanks for the reply.
this is the solution as sugested Instantiate PersistenceExtensions for none default persitence service - #2 by rlkoshak.

rules.JSRule({
    name: "count 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 rrd4Item = items.getItem("room_8_device1_dht22_temperature");
        console.log("rrd4jc Item is: ", rrd4Item.name, " ", rrd4Item.label, " ", rrd4Item.state);

        var rrd4ValueCount = rrd4Item.persistence.countBetween(begin, end);
        console.log("rrd4j Persistent Item count: ", rrd4ValueCount);

        // JDBC history
        var jdbcItem = items.getItem("room_0a_temperature");
        console.log("jdbc Item is: ", jdbcItem.name, " ", jdbcItem.label, " ", jdbcItem.state);

        var jdbcValueCount = jdbcItem.persistence.countBetween(begin, end, 'jdbc');
        console.log("jdbc Persistent Item count: ", jdbcValueCount);

    },
    tags: ["Test", "historicItem"],
    id: "countHistoricalItem"
  });
  
my error(sorry) the item i used on the initial post is not part of the jdbc persistance.

thanks again Ernst

While we are looking at some code here:

This line is completely unnecessary and in fact you are not using it anywhere.

You almost never need to use Java.type in JS Scripting rules. Just about everything you need is already imported and made available to you through the helper library which itself is imported by default automatically.

Everything else looks fine. I’m glad you got it working!