I created a Time of Day Helper that should be on between 12:00am and 12:05am. For some reason it is turning on/off an hour too early (11:00pm - 11:05pm).
{{ utcnow() }} and {{ now() }} both report the expected times.
My timezone is set correctly.
I created this on November 5th, the day before daylight savings ended, though if daylight savings was an issue I would have expected it to move an hour out, not an hour in.
Duh, I should have known that. Thanks for pointing that out.
Regardless, daylight savings transitions should not impact this helper, should it? Shouldn’t this helper still turn on at 12am before and after time changes?
FWIW, a quick browse of the code (i.e., homeassistant/components/tod/binary_sensor.py) seems to indicate it’s a bug. It converts the naive time to UTC when the sensor is created, then every day adds a full day (i.e., 24 hours), so it never adjusts when DST changes. I bet a restart of HA (or maybe just disable & re-enable it) makes it work correctly again (until the next DST change.)
A restart of HA does seem to have addressed the issue since the sensor worked as expected at midnight. I suppose a workaround could be to automate a restart of HA whenever the time changes.
For me, I am going to hard-code a time into a template I am working on rather than relying on this sensor for now.
fyi, hardcoding a time will only work if you use now() in your template. Otherwise it’ll still be wrong if you check at midnight as DST is applied at different times depending which timezone you are in.
I’ve been looking at the code, and it could definitely use some improvement. Specifically, for this issue, when DST ends there will be a window of time that will repeat. The size of the window and when it happens depends, of course, on where you are. The question, though, becomes, what should happen during this repeating window of time?
E.g., let’s say DST ends at 2:00 am, and the time instantaneously changes back to 1:00 am. All the times from 1:00:00 am to 1:59:59.9999 will happen once, then happen again a second time, before it actually becomes 2:00 am.
In this scenario, let’s say you have after set to 1:15:00 (and before set to something 2:00:00 or later.) The binary sensor will be off before 1:15, then change to on at 1:15. What should happen when it becomes 1:00 again? Should it go back off for 15 minutes, then come back on? Or should it just stay on until it gets to the before time? I could see both scenarios being valid / useful.
EDIT: FWIW, I believe it would be much easier to implement the latter – i.e., once it hits the specified after time it stays on until the before time is reached, even if it’s technically not honoring the after for potentially a short period of time. And besides being easier to implement, it would then work more like it does now (one on/off pair per day), and how I would expect most use cases would want it to work.
And regarding when DST starts, that’s even easier. E.g., if the before time was 2:15, and DST started at 2:00:00 and instantaneously jumped ahead to 3:00:00, the sensor should go off at 3:00, because there isn’t any 2:15.
Don’t know. However, I do recall seeing some changes (in general) to better deal with DST changes, so it’s possible the issue has been addressed, either directly or indirectly. Honestly, though, haven’t looked at this in quite a while.