isAfter/isBefore: strange result

I’m experiencing a strange behaviour with isAfter and isBefore. The result is not as expected. It is before and after the time the same. See code and result below.

Worth to mention is that it used to work. But than I updated the Jython package from 2.7.0 to 2.7.2 and also the helper libraries from 05.08.20 to 30.11.20. I don’t see that this should have an impact to these functions. Might there be a configuration I have overwritten?
OH is 2.5.10.

*******************************************************************************
Jython version:             2.7.2.final
Operating system:           Linux
OS Version:                 4.19.66-v7+
Java vendor:                Oracle Corporation
Java VM name:               Java HotSpot(TM) Client VM
Java runtime name:          Java(TM) SE Runtime Environment
Java runtime version:       1.8.0_181-b13
configuration.py installed: True
sys.path:                   /etc/openhab2/automation/lib/python
                            /etc/openhab2/automation/jython/Lib
                            /etc/openhab2/automation/jython/jython-standalone-2.7.2.jar/Lib
                            __classpath__
                            __pyclasspath__/
*******************************************************************************
    @rule("Rolladen: Rolladen am Morgen rauf")
    @when("Item swi_Astro_Nacht changed to OFF")
    @when("Time cron 33 59 7 ? * *")
    def RollershutterOpenAtMorning(event):

        Time8000 = DateTime.now().withTime(7, 30, 59, 30)       # 8 Uhr morgens (7:59:30)
        Before8 = DateTime.now().isBefore(Time8000)
        After8 = DateTime.now().isAfter(Time8000)
        BankHoliday = Ephemeris.isBankHoliday()
        Weekend = Ephemeris.isWeekend()

        LogAction.logInfo("Rolladen: RollershutterOpenAtMorning", "vor 8: {}, nach 8: {}, Feiertag: {}, Wochenende: {}".format(Before8, After8, BankHoliday, Weekend))

The resulting log is:

03-Dez-2020 07:34:01.295 [INFO ] [Rolladen: RollershutterOpenAtMorning] - vor 8: 0, nach 8: 1, Feiertag: 0, Wochenende: 0
03-Dez-2020 07:59:33.555 [INFO ] [Rolladen: RollershutterOpenAtMorning] - vor 8: 0, nach 8: 1, Feiertag: 0, Wochenende: 0

Cool… that works nicely! It looks like you have a typo…

Time8000 = DateTime.now().withTime(7, 30, 59, 30)

This is not 7:59:30, as you have in the comment, but 7:30:59.30. Both triggers of the rule occurred after this time.

You should probably get this migrated to ZonedDateTime in preparation for OH3…

    @rule("Rolladen: Rolladen am Morgen rauf")
    @when("Item swi_Astro_Nacht changed to OFF")
    @when("Time cron 33 59 7 ? * *")
    def RollershutterOpenAtMorning(event):

        Time8000 = DateTimeType().zonedDateTime.withHour(7).withMinute(59).withSecond(30).withNano(0)# 8 Uhr morgens (7:59:30)
        current_time = DateTimeType().zonedDateTime
        Before8 = current_time.isBefore(Time8000)
        After8 = current_time.isAfter(Time8000)
        BankHoliday = Ephemeris.isBankHoliday()
        Weekend = Ephemeris.isWeekend()

        LogAction.logInfo("Rolladen: RollershutterOpenAtMorning", "vor 8: {}, nach 8: {}, Feiertag: {}, Wochenende: {}".format(Before8, After8, BankHoliday, Weekend))

Thanks a lot! I don’t know how often I had a look at it…

Furthermore it is always interesting for how long a bug might remain without getting to the light…
The bug came in several weeks ago when I changed the similar solution as you suggest with DateTimeType but for DateTime to the usage of withTime()

1 Like