Search code examples
pythontypeerror

HASS - usual method for getting sunrise and sunset time stopped working - how to solve it?


I have this python program that I’ve been running on home assistant every day for over a year now.

It is called via shell script:

#!/bin/bash
python3 /config/solax/compute_pv_schedule.py $1 $2 $3 $4 $5 $6

Yesterday for the first time the script returned an error, which appears in the logs like this:

(...)
 File "/config/solax/compute_pv_schedule.py", line 331, in <module>
 v_sunrise_hour = int(v_sun.get_sunrise_time().strftime(\'%H\'))
 ^^^^^^^^^^^^^^^^^^^^^^^^
 File "/usr/local/lib/python3.12/site-packages/suntime/suntime.py", line 34, in get_sunrise_time
 time_delta = self.get_sun_timedelta(at_date, time_zone=time_zone, is_rise_time=True)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/usr/local/lib/python3.12/site-packages/suntime/suntime.py", line 131, in get_sun_timedelta
 UT += time_zone.utcoffset(at_date).total_seconds() / 3600
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: utcoffset(dt) argument must be a datetime instance or None, not datetime.date

The offending lines on the python program could not be simpler, and as previously said they have been working fine without any issue for over a year till now:

from suntime  import Sun, SunTimeException
(...)
v_sun          = Sun(k_latitude, k_longitude)
v_sunrise_hour = int(v_sun.get_sunrise_time().strftime('%H'))  ## <<<< offending line
v_sunset_hour  = int(v_sun.get_sunset_time().strftime('%H'))

As anyone seen this behavior as well?
It seems like something is broken in the underlying python libs, does it not?

Can anyone help here, some suggestion on how to fix this?
Or maybe we need a bug to be open on github? Is this a known bug?

Any feedback would be greatly appreciated.

Cheers,
-jprates


Solution

  • Yes, this looks like a bug in a commit from 3 days ago. In the short term, you should be able to work around this by changing the code to:

    now = datetime.now()
    v_sunrise_hour = int(v_sun.get_sunrise_time(now).strftime('%H'))
    v_sunset_hour  = int(v_sun.get_sunset_time(now).strftime('%H'))
    

    FOLLOWUP

    I submitted a bug report.