AppDaemon / appdaemon

:page_facing_up: Python Apps for Home Automation
Other
855 stars 419 forks source link

Daylight savings does not trigger initialize() #2144

Open rr326 opened 1 month ago

rr326 commented 1 month ago

What happened?

From the docs, when Daylight savings time changes, it is supposed to re-run initialize(). I have some timers that did not re-initialized and hence the run_daily() were off by an hour. I also found the same issue reported in an appdaemon forum, so it's not just me.

Version

4.4.2

Installation type

Docker container

Relevant log output

2024-11-03 01:00:00 INFO     AppDaemon            Daylight Savings Time transition detected

(but no "intiailize" or reloaded apps after that.

Relevant code in the app or config file that caused the issue

self.run_daily(
                self.cb_gw_on,
                self.parse_time(config["schedule"]["time"]),
                config=config,
            )

Anything else?

This is in appdaemon scheduler.py

                if old_dst_offset != dst_offset:
                    #
                    # DST began or ended, lets prove we noticed
                    self.logger.info("Daylight Savings Time transition detected")
                    #
                    # Re calculate next entries
                    #
                    next_entries = self.get_next_entries()

Are you sure that elsewhere this actually triggers an intialize() per the docs?

acockburn commented 1 month ago

This is a combination of 2 separate problems:

  1. I used to call initialize() when DST transitioned, but I removed that in favor of automatically recalculating the scheduler entries after the transition, but missed updating the docs to reflect this.

  2. The automatic calculation has edge cases that fail and after looking at it for several years I am of the opinion that there is no way to avoid some edge cases. For instance, during DST transitions, some times happen twice, and in other transitions some times never occur. This plus maybe some issues in the implementation make it very difficult to get right, and I can only test each edge case once a year!

So, what I think I will do, is either go back to the calling of initialize() which makes it the app writer’s responsibility and worked flawlessly, or maybe look at automatically re-applying the scheduler calls using the original args as if initialize() had been called, which should have the same effect if it is done after the transition. Either way needs work, but I will look into it.

rr326 commented 1 month ago

Awesome that you responded so quickly. Thanks Andrew.

Just re-calling initialize sounds like a good solution to me. Not the most efficient or fast, but it only happens twice a year, and simplicity on your end seems a win!

(I was literally thinking of implementing an app to kill appdaemon upon a transition, so it would be forced to reload.)