OpenHab3 Java time conversion problem

Hi all,

Recently upgraded to OH3 and I don’t seem to be able to fix this last issue with time conversion from my old rules to java-time.
I am therefore hoping for someone to assist in converting the to times (now.isAfter and now.isBefore) to java time:-)

Thank you!

rule "OpenHAB system started - astro"
System started
createTimer(now.plusSeconds(180)) [ |
    logInfo("RULE", "--> astro init")
    if (now.isAfter((CivilDusk.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli) ||
        now.isBefore((CivilDawn.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
    ) {
        logInfo("RULE", "--> Night ON")
        postUpdate(Night, ON)
        postUpdate(Day, OFF)
        postUpdate(swDaylight, OFF)
    } else {
        logInfo("RULE", "--> Day ON")
        postUpdate(Night, OFF)
        postUpdate(Day, ON)
        postUpdate(swDaylight, ON)


now is a ZonedDateTime so you want to compare it with another ZonedDateTime and not epoch millis:

    if (now.isAfter((CivilDusk.state as DateTimeType).zonedDateTime) ||
        now.isBefore((CivilDawn.state as DateTimeType).zonedDateTime)
    ) {

Usually you can always avoid epoch milli conversions when you use the java.time API. It makes the code more readable too. :slight_smile:

What a forum :raised_hands: :smiley:
That did it - thank you very much. I seem to always struggle with all the different time conversions, despite having read all the excellent threads on the forum.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.