[SOLVED] Having a light turn off at sunrise using run_at_sunrise complaining about arguments

So I’m really starting to get the hang of this appdaemon now (still not ready quite yet to tackle the big kahuna that I need to migrate over but I now have my kids night light app written up

####################
boys_night_light:
  module: boys_night_light
  class: night_light_control
  lightID: light.boys_night_light
  constrain_presence: anyone
  constrain_start_time: 20:30:00

##########################
import appdaemon.appapi as appapi
import time

class night_light_control(appapi.AppDaemon):

  def initialize(self):
    self.log("Lets adjust the lights")

# Now its time for the fun stuff

    self.listen_state(self.adjust_light_function,(self.args["lightID"]))

    self.run_at_sunrise(self.sunrise_cb, 0)

# Found this cool function to convert the brightness to a percentage because well thats easier than doing math 
  def brightness_to_percent(self, brightness):
    return 255/100 * int(brightness)

# Now for our function
  def adjust_light_function(self, entity, attribute, old, new, kwargs):
    self.log("Turning on the night light and setting it to 50%")
    brightness = self.brightness_to_percent(50)
    self.turn_on(self.args["lightID"], brightness=brightness)
    self.log("Now going to sleep for 2 hours")
    time.sleep(120)
    self.log("Adjusting the light to 25%")
    brightness = self.brightness_to_percent(25)
    self.turn_on(self.args["lightID"], brightness=brightness)
    self.log("Now going to sleep for 2 hours")
    time.sleep(120)
    self.log("Adjusting the light to 5%")
    brightness = self.brightness_to_percent(5)
    self.turn_on(self.args["lightID"], brightness=brightness)

  def sunrise_cb(self, kwargs):
    self.turn_off(self.args["lightID"])

However when this app initializes its complaining with this

----------
2018-03-05 13:28:51.039357 WARNING Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 865, in check_config
    new_config[name]["module"], new_config[name]
  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 595, in init_object
    conf.objects[name]["object"].initialize()
  File "/config/hadaemon/apps/boys_night_light.py", line 13, in initialize
    self.run_at_sunrise(self.sunrise_cb, 0)
TypeError: run_at_sunrise() takes 2 positional arguments but 3 were given

2018-03-05 13:28:51.040291 WARNING ------------------------------------------------------------

I’m confused about why its getting 3 arguments passed into it when all I have defined is just the 2.

I followed the example from https://home-assistant.io/docs/ecosystem/appdaemon/tutorial/ where they go over using the run_at_sunrise and run_at_sunset. I don’t want an offset for this so thats why I just left it at 0 but I can’t see why it would think that its getting 3 arguments passed into it. I don’t see anything standing out that says hey I’m the 3rd thing you need to remove.

There is an error in that tutorial, from the current docs:

http://appdaemon.readthedocs.io/en/latest/APIREFERENCE.html?highlight=run_at_sunrise#run-at-sunrise

Try:

self.run_at_sunrise(self.sunrise_cb, offset=0)

Or just:

self.run_at_sunrise(self.sunrise_cb)

I’ll fix that tutorial.

Thank you very much for @aimc for the lightning response. Making that correction now. Lol I’m going over every line and I just couldn’t figure it out lol.

1 Like