Date-Time Calculation issue OH2 to OH3

I have a rule for calculating the minutes until sunset, this is quite useful for switching off heavier load items as the day ends and solar power diminishes.

The rule below worked fine with OH2, but with the migration to OH3(I did a clean install) this rule does not work. It is the ONLY thing that does not work with OH3 thus far, which quite impressed me.


Number MinutesUntilSunset "Minutes Until Sunset [%.0f min]"


rule "Minutes Until Sunset"
    Time cron "0/30 * * * * ?"
MinutesUntilSunset.postUpdate((new DateTime(Sunset_Time.state.toString).millis - new DateTime(Current_DateTime.state.toString).millis) / 60000)

I am aware that OH3 now uses Javas native date/time format but I just cannot get my head around it. Any help appreciated!

As documented in the release notes, DateTime no longer exists in openHAB 3. It’s been replaced by ZonedDateTime. See Rules | openHAB and DateTime Conversion (openHAB 3.x) for lots of examples of working with ZonedDateTime.

Given that we have ZonedDateTimes now, the best approach would probably be to use the stuff built into the classes anyway.


// in the rule

    var minsToSunset = ChronoUnit.MINUTES.between(now, Sunset_Time.state.getZonedDateTime)

You might need to cast Sunset_Time.state to a DateTimeType.


Hi Rick,

I have a similar problem to calculate the minutes between the last state change (saved in a DateTime item) and now. I want to write the rule as ECMA script but I can not calculate with the variable now and the DateTime item.

ECMA code:

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

var now = new Date(new Date() - 10000)
var lastAction = itemRegistry.getItem(‘Spuelmaschine_OpStateLastChange’).getState()"now = " + now)"lastAction = " + lastAction)"now = " + now)"lastAction = " + lastAction)

diff = lastAction - now"minutes= " + diff)

2021-02-13 13:15:03.808 [INFO ] [enhab.model.script.Rules.Experiments] - now = Sat Feb 13 2021 13:14:53 GMT+0100 (CET)
2021-02-13 13:15:03.808 [INFO ] [enhab.model.script.Rules.Experiments] - lastAction = 2021-02-12T19:14:05.614500+0100
2021-02-13 13:15:03.816 [INFO ] [enhab.model.script.Rules.Experiments] - diff= NaN

How can I convert the dates to caculate a time difference (in minutes)?

Thank you for your help!

Thanks @rlkoshak appreciate the pointer, although Java Time is quite a big change(for me) it certainly has benefits and a lot more built in functionality. You were correct Sunset_Time.state needed to be cast.

Working rule:

import java.time.temporal.ChronoUnit

rule "Minutes Until Sunset"
    Time cron "0/30 * * * * ?"
    var minsToSunset = ChronoUnit.MINUTES.between(now, (Sunset_Time.state as DateTimeType).getZonedDateTime)
    var minsPastSunrise = ChronoUnit.MINUTES.between((Sunrise_Time.state as DateTimeType).getZonedDateTime, now)

Why my question gets the solution tag???

@johannesbonn apologies, I fat fingered that one

1 Like

There is no more DateTime. It has been replaced with ZonedDateTime. To get now use Once oyu do that the above will work in all the languages.

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

var now =;
var minsToSunset = ChronoUnit.MINUTES.between(now, (Sunset_Time.state as DateTimeType).getZonedDateTime());
1 Like

Thank you Rick,
this was the solution. One last Question:-)
My timestamp (here Sunset_Time) is in the past, so the result is negativ between jetzt1 (now) and my timestamp. When I change the two parts in the expression I get the following error in the log:

Script execution of rule with UID 'Java_Test' failed: TypeError: (itemRegistry.getItem("Spuelmaschine_OpStateLastChange").getState() ,> jetzt1).getZonedDateTime is not a function in <eval> at line number 45

And here my expression:

var laufzeitMinuten = ChronoUnit.MINUTES.between((itemRegistry.getItem('Spuelmaschine_OpStateLastChange').getState(), jetzt1).getZonedDateTime());

Do you have an idea what my failure is?

If this is JavaScript, that line is a little overly complex. All the Item’s current states are stored in an items dict. Also, the state of a DateTime Item is not a ZonedDateTime. But you can get a ZonedDateTime from it. So

var laufzeitMinuten = ChronoUnit.MINUTES.between(items['Spuelmaschine_OpStateLastChange'].getZonedDateTime(), jetzt1);

Not I’m assuming that jetzt1 is itself also already a ZonedDateTime.

Even if that wasn’t a problem, your parens are in the wrong place and the expression is essentially meaningless.

1 Like

Thank You!!! Now its running like it should!