Run_at_sunrise and sunset - errors

I have added a couple of new functions to run a block of code at sunrise and sunset and for some reason, it’s failing with the following error. Am I missing something here?

Error:

2017-12-21 07:16:23.328100 WARNING Unexpected error in worker for App sunset_lights_2:
2017-12-21 07:16:23.334834 WARNING Unexpected error in worker for App sunset_lights_1:
2017-12-21 07:16:23.335374 WARNING Unexpected error in worker for App sunset_lights_3:
2017-12-21 07:16:23.341853 WARNING Worker Ags: {'name': 'sunset_lights_2', 'function': <bound method TurnOnLightsAtSunset.sunrise_cb of <sunsetlights.TurnOnLightsAtSunset object at 0x75ad7$
2017-12-21 07:16:23.344975 WARNING Worker Ags: {'name': 'sunset_lights_1', 'function': <bound method TurnOnLightsAtSunset.sunrise_cb of <sunsetlights.TurnOnLightsAtSunset object at 0x75a06$
2017-12-21 07:16:23.345651 WARNING Worker Ags: {'name': 'sunset_lights_3', 'function': <bound method TurnOnLightsAtSunset.sunrise_cb of <sunsetlights.TurnOnLightsAtSunset object at 0x75ad7$
2017-12-21 07:16:23.346110 WARNING ------------------------------------------------------------
2017-12-21 07:16:23.346488 WARNING ------------------------------------------------------------
2017-12-21 07:16:23.346908 WARNING ------------------------------------------------------------
2017-12-21 07:16:23.364073 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 505, in worker
    function(utils.sanitize_timer_kwargs(args["kwargs"]))
  File "/home/homeassistant/.homeassistant/config/apps/sunsetlights.py", line 96, in sunrise_cb
    self.log("Sunrise Triggered")
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appapi.py", line 87, in log
    msg = self._sub_stack(msg)
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appapi.py", line 66, in _sub_stack
    stack = inspect.stack()
  File "/usr/lib/python3.4/inspect.py", line 1342, in stack
    return getouterframes(sys._getframe(1), context)
  File "/usr/lib/python3.4/inspect.py", line 1321, in getouterframes
    framelist.append((frame,) + getframeinfo(frame, context))
  File "/usr/lib/python3.4/inspect.py", line 1296, in getframeinfo
    lines, lnum = findsource(frame)
  File "/usr/lib/python3.4/inspect.py", line 709, in findsource
    if pat.match(lines[lnum]): break
IndexError: list index out of range

Code (inside initialize):

# Run at sunrise
self.run_at_sunrise(self.sunrise_cb)

# Run at sunset
self.run_at_sunset(self.sunset_cb)


def sunrise_cb(self, kwargs):
 self.log("Sunrise Triggered")

 dh_trigger = self.get_state("input_boolean.ad_dark_house_lights")
 self.log("Current ad_dark_house_lights trigger status = {}".format(self.dh_trigger))

 if dh_trigger == "off":
    self.turn_on("input_boolean.ad_dark_house_lights")
    msg = "Good morning. AppDaemon dark house flag turned ON at sunrise\n"
    self.log(msg)
    self.set_state("sensor.appd_notify_message", state=msg, attributes={"ifttt":False, "frontend":True, "announce":False})

do you also have a sunset_cb function?

It’s failing in the log function call due to some weirdness inside the stack. Are you by any chance defining sunrise_cb() inside the Initialize function? I can;t see because the code you posted is incomplete. If you have done that, try declaring it at the normal object level instead.

Yes, I verified both functions (sunrise_cb, sunset_cb) are defined outside.

Here is the full file.

The lines in the backtrace don’t really match the code you have, but these lines have a problem

     dh_trigger = self.get_state("input_boolean.ad_dark_house_lights")
     self.log("Current ad_dark_house_lights trigger status = {}".format(self.dh_trigger))

You either need to do self.dbtrigger = or format(db_trigger)

i dont think thats true.

self.dh_trigger could be another var then dh_trigger.
self.dh_trigger is global and dh_trigger local

i dont see where the problem is.
i would edit out line by line untill i found which line is giving the error.

That was the weird part. Its just normal function with no bells and whistles. It could be some formatting issue. I will your suggestion.

I corrected that and it was a mistake on my part. But it still venting an error about self.log :thinking:

so disable the log and see if you lose the error.
or it is the line before it and then you are on your way.

Actually, that was it. :man_facepalming: Earlier I didn’t test this at sunset time. Thanks for catching this. :+1:

1 Like