Sun entity missing next_setting

After months of smooth operation, one of my apps that uses run_at_sunset() failed to initialize. After restarting, it no longer gets the error so maybe it’s not a serious defect but I thought I’d report it in case it needs some visibility.

Here is the app:

import appdaemon.plugins.hass.hassapi as hass
import datetime


class Curio_cabinet_light(hass.Hass):

  def initialize(self):
    # Configure a callback that occurs at sunset every day
    self.run_at_sunset(self.light_on)
    # Schedule a daily callback that will call run_daily() at 10pm every night
    self.run_daily(self.light_off, datetime.time(22, 00, 0))

  def light_on(self, kwargs):
    self.turn_on('light.dining_room_curio_cabinet_light')

  def light_off(self, kwargs):
    self.turn_off('light.dining_room_curio_cabinet_light')

and the error that was reported:

2018-06-27 11:48:26.081490 WARNING AppDaemon: ------------------------------------------------------------
2018-06-27 11:48:26.111515 WARNING AppDaemon: Unexpected error running initialize() for curio
2018-06-27 11:48:26.112866 WARNING AppDaemon: ------------------------------------------------------------
2018-06-27 11:48:26.149841 WARNING AppDaemon: Traceback (most recent call last):
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 1575, in init_object
    init()
  File "/home/homeassistant/.appdaemon/apps/curio.py", line 8, in initialize
    self.run_at_sunset(self.light_on)
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appapi.py", line 420, in run_at_sunset
    handle = self._schedule_sun(name, "next_setting", callback, **kwargs)
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appapi.py", line 406, in _schedule_sun
    event = self.calc_sun(type_)
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appapi.py", line 247, in calc_sun
    return self.AD.calc_sun(type_)
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 892, in calc_sun
    return self.sun[type_].timestamp()
KeyError: 'next_setting'

2018-06-27 11:48:26.151021 WARNING AppDaemon: ------------------------------------------------------------
2018-06-27 11:48:26.230734 WARNING AppDaemon: ------------------------------------------------------------
2018-06-27 11:48:26.231197 WARNING AppDaemon: Unexpected error running initialize() for blah
2018-06-27 11:48:26.231551 WARNING AppDaemon: ------------------------------------------------------------
2018-06-27 11:48:26.244763 WARNING AppDaemon: Traceback (most recent call last):
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 1575, in init_object
    init()
  File "/home/homeassistant/.appdaemon/apps/sun.py", line 38, in initialize
    self.run_at_sunset(self.sunset, offset = 15*60)
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appapi.py", line 420, in run_at_sunset
    handle = self._schedule_sun(name, "next_setting", callback, **kwargs)
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appapi.py", line 406, in _schedule_sun
    event = self.calc_sun(type_)
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appapi.py", line 247, in calc_sun
    return self.AD.calc_sun(type_)
  File "/srv/appdaemon/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 892, in calc_sun
    return self.sun[type_].timestamp()
KeyError: 'next_setting'

2018-06-27 11:48:26.245392 WARNING AppDaemon: ------------------------------------------------------------

seems to me that that is the race problem with HA’s settings. (latitude, longtitude and timestting in HA are set some time after AD detects that HA is started, so initialise is started before settings are active)
Andrew did a repair for that in the last update.

do you have AD 3.0.1 running or are you still on 3.0.0?

I updated to 3.0.1 less than a week ago. So this issue occurred on 3.0.1.

you updated less then a week ago and the error did occur 1 time after that?
to make sure everything is alright, please clear logs, shut down everything and restart in normal order (first HA then AD)
then see if the problem is still there.

I’ll do as you suggest but I can’t be confident that the problem would occur again even if I don’t restart. I’ll report the results in a few days.

the problem can only occur when you restart.
initialisings apps only occurs when you change the app or restart appdaemon :wink:

so you can only get that error when you restart the app.

what however probably will happen after the error is that the app isnt working at all.