[OH2] Problem with Persistence historicState

Hi, i want to read out some values from my influx.db database. But I alway get an error massage:

2021-02-08 15:35:17.558 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Historische Verbrauchsdaten’: cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null

My rule looks like this:

    rule "Historische Verbrauchsdaten"

        when

        Item ChZWPM_Y changed or

        Item ChZWPM_M changed

        then

        YearMonth = ChZWPM_Y.state + "-" + ChZWPM_M.state + "-01T23:30"
       
        YearMonthDay = DateTimeType.valueOf(YearMonth)

        JodaYearMonthDay = new DateTime(YearMonthDay.toString)

        LastDay = JodaYearMonthDay.dayOfMonth().withMaximumValue().toString

        ExactDay = new DateTime(LastDay.toString)

        HisValHa.postUpdate(ZHWirkenergieZAp.historicState(ExactDay ,"influxdb").state as Number)

    end

The database looks like:

What it does: The relevant values are stored at the end of every month to the db. With the Items ChZWPM_Y ánd ChZWPM_M I select year and month from basic ui. Then I calculate the last day of the selected month of witch I want to read out the values.

It might be a problem with the DateTime format? But I can’t see it. Do you have any ideas?

Thanks in advance
Tobias

Why don’t you log out ExactDay to see what you have made?

It seems a little odd to not declare any of your variables.

Hi, thanks for that hint. My declaration is:

var String YearMonth = ""
var String LastDay = ""
var DateTimeType YearMonthDay
var DateTime JodaYearMonthDay
var DateTime ExactDay

As a result for “ExactDay” i get:

2020-02-29T23:30:00.000+01:00

I’m not sure if that DateTime works out to be before your first data record, when time zone is taken into account.

When you use historicState() it is never going to match an exact record to the second. It will return the next oldest record, assuming the data remained valid until next change.

So to get records from last day of month, you can just ask for the first day of the next month.

Thank you for quick help and leading me to the right direction. I use now the first day of the next month. My final solution looks like this:

var String YearMonth = ""
var String FirstDay = ""
var DateTimeType YearMonthDay
var DateTime JodaYearMonthDay
var DateTime ExactDay

rule "Historische Verbrauchsdaten"
    when
    Item ChZWPM_Y changed or
    Item ChZWPM_M changed
    then

    YearMonth = ChZWPM_Y.state + "-" + ChZWPM_M.state + "-01T23:59"

    YearMonthDay = DateTimeType.valueOf(YearMonth)

    JodaYearMonthDay = new DateTime(YearMonthDay.toString)

    FirstDay = JodaYearMonthDay.plusMonths(1).dayOfMonth().withMinimumValue().toString  

    ExactDay = new DateTime(FirstDay.toString)

    HisValHa.postUpdate(ZHWirkenergieZAp.historicState(ExactDay,"influxdb").state as Number)
    HisValWP.postUpdate(ZWWirkenergieZAp.historicState(ExactDay,"influxdb").state as Number)
end

It’s bad luck your first “exact day” worked out to be before your oldest data. I believe the database always stores by UTC, and the persistence framework converts to/from your local timezone.