DateTime conversion from item in OpenHab3

  • Platform information:
    • Java Runtime Environment: 11
    • openHAB version: OH3

Hi all,
at the moment I am getting a bit lost with the new OH3 DateTime conversion and I have no clue how I need to solve this. Hope someone sees my mistake.

My previous rule part on OH2 looks the following:

if((PowerMeter_Meter3_totalkWh.lastUpdate.millis - PowerMeter_Meter3_totalkWh.previousState(true,“influxdb”).timestamp.time) > 0) {
consumption3 = (actualconsumption3 - previousconsumption3)
} else {
consumption3 = 0
}

which worked totally fine. With the new OH3 I am forced to change this to joda time zonedDateTime, but I have no clue how this works. I have read many examples but all request a datetime input, which I do not have I think.

As latest I tried the following:

val joda_LastOpen = new DateTime((PowerMeter_Meter3_totalkWh.lastUpdate as DateTimeType).zonedDateTime.toInstant.toEpochMilli)

    if((joda_LastOpen - PowerMeter_Meter3_totalkWh.previousState(true,"influxdb").timestamp.time) > 0) {
    consumption3 = (actualconsumption3 - previousconsumption3)
    } else {
        consumption3 = 0
    }

Here I get the following error:
Cannot cast from ZonedDateTime to DateTimeTyp

Scuby

You can calculate a Duration from the ZDTs like this:

val start = new DateTime((PowerMeter_Meter3_totalkWh.lastUpdate as DateTimeType).zonedDateTime
val end = PowerMeter_Meter3_totalkWh.previousState(true,"influxdb").timestamp

if (Duration.between(start, end).toMillis() > 0) {
    consumption3 = actualconsumption3 - previousconsumption3
} else {
    consumption3 = 0
}

You probably also need to add an import for the Duration:

import java.time.Duration

Thanks a lot for the hint. Me helped the change to:

    val ends1 = PowerMeter_Meter2_totalkWh.lastUpdate.toInstant().toEpochMilli()
    val start1 = PowerMeter_Meter2_totalkWh.previousState(true,"influxdb").timestamp.toInstant().toEpochMilli()


    if(ends1 - start1 > 0) {
    consumption2 = (actualconsumption2 - previousconsumption2)
    } else {
        consumption2 = 0
    }
1 Like

If you like working with epoch millis that’s also a way to do it! :slight_smile:

Ahhh ok both are still possible. Good to know.

I also have a problem with calculating a duration in OH3

The following rule worked in OH2 but no longer in OH3

var Number TasterKuecheOLTime // Taster Küche oben links Drück-Dauer
//

rule “KuechenTaster oben links PRESSED”
when Channel “enocean:rockerSwitch:A601…:FEF8…:rockerswitchA” triggered “DIR1_PRESSED”
then
TasterKuecheOLTime= now.millis
end
//
rule “KuechenTaster oben links RELEASED”
when Channel “enocean:rockerSwitch:A601…:FEF8…:rockerswitchA” triggered “DIR1_RELEASED”
then
if(now.millis - TasterKuecheOLTime> 500) {
// Taster lang gedrückt

} else {
// Taster kurz gedrückt

}
end

Could you please help what needs to be changed for OH3?

Replace now.millis with “now.toInstant().toEpochMilli()”

Thank you @tsmit for your quick and concrete help. My rules work again.

I have read the date time conversion manual for OH3 but it was difficult to understand. If this is the only way in OH3 to analyze, if a switch is short or long pressed, than OH3 is not easy for end users - at least for time based rules.