Yet another newbie question

I’m getting better at this, but… I have this script calling a function in another. It works, but fires immediately upon saving, not when the run_daily is set to

import appdaemon.plugins.hass.hassapi as hass
import datetime
 
class TimedLights(hass.Hass):
      def initialize(self):
        self.log("TimedLights Initialized")
        kwargs = {"light_device": "light.pedestal", "light_value": "on", "light_color": "255,0,0", "light_brightness":"255"}
        time = datetime.time(12, 29, 0)
        self.run_daily(self.do_lights(kwargs), time)

      def do_lights(kwargs):
        self.log("Do Lights Running")
        lighting = self.get_app("Lights")
        lighting.run_light(kwargs)

Lights.py

import appdaemon.plugins.hass.hassapi as hass
 
class Lights(hass.Hass):
  def initialize(self):
    self.log("Lights Initialized!")

  def run_light(kwargs):
  
    self.log("Here we go!")
  
    device = kwargs["light_device"]
    light_value = kwargs["light_value"]
    if "light_color" in kwargs:
      light_color = kwargs["light_color"]
    if "light_brightness" in kwargs:
      light_brightness = kwargs["light_brightness"]
    else:
    light_brightness = 255
      
    if light_value == "on":
      self.turn_on(light_device, brightness = intBrightness)
    elif light_value == "off":
      self.turn_off(light_device)
    elif light_value == "dim":
      self.turn_on(device)

In the run daily you should just use the name of the callback without any arguments, e.g. remove the (kwargs)

Ok, that’s one hurdle. Now I run this:

self.Lights = self.get_app("Lights")
self.log(self.Lights)

And the log shows NONE and of course, the error log says:

AttributeError: ‘NoneType’ object has no attribute ‘run_light’

Lights is the name is the module but may not be the name of the app. How is it configured in your apps.yaml?

Lights:
  module: lights
  class: Lights

And thanks for putting up with the newbie :wink:

another error that isee.
its a callback and should be:

def do_lights(self, kwargs):

same with tun_light

With these edits it runs, but immediately. If I remove kwargs, it doesn’t run, but throws the error below

timed_lights.py

import appdaemon.plugins.hass.hassapi as hass
import datetime
 
class TimedLights(hass.Hass):
  #initialize() function which will be called at startup and reload
  def initialize(self):
    self.lights = self.get_app('Lights')
    self.log(self.lights)
    self.log("TimedLights Initialized!")
    kwargs = {"light_device": "light.pedestal", "light_value": "on", "light_color": "255,0,0", "light_brightness":"255"}
    time = datetime.time(13, 26, 10)
    self.run_in(self.do_lights(kwargs), 20)

  def do_lights(self, kwargs):
    self.log("Do Lights")
    self.log(self.lights)
    self.lights.run_light(kwargs)

lights.py

   import appdaemon.plugins.hass.hassapi as hass
 
class Lights(hass.Hass):
  #initialize() function which will be called at startup and reload
  def initialize(self):
    self.log("Lights Initialized!")

  def run_light(self, kwargs):
  
    self.log("In Run_Light")
  
    light_device = kwargs["light_device"]
    light_value = kwargs["light_value"]
    if "light_color" in kwargs:
      light_color = kwargs["light_color"]
    if "light_brightness" in kwargs:
      light_brightness = kwargs["light_brightness"]
    else:
      light_brightness = 255
      
    if light_value == "on":
      self.turn_on(light_device, brightness = light_brightness)
    elif light_value == "off":
      self.turn_off(light_device)

Error Log

2018-02-23 15:36:32.733644 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1651, in read_app
    self.init_object(name, class_name, module_name, self.app_config)
  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1446, in init_object
    self.objects[name]["object"].initialize()
  File "/config/appdaemon/apps/timed_lights.py", line 13, in initialize
    self.run_in(self.do_lights(), 5)
  File "/config/appdaemon/apps/timed_lights.py", line 19, in do_lights
    self.lights.run_light()
  File "/config/appdaemon/apps/lights.py", line 12, in run_light
    light_device = kwargs["light_device"]
NameError: name 'kwargs' is not defined

From your config it should be get_app(“Lights”)

Yeah, I noticed that part. I keep changing stuff to test. I figure if I type randomly I’ll be like the monkeys and Hamlet, ya know? lol

I edited the code above to what I have that runs, but the timing isn’t taken into consideration yet. Still runs immediately upon saving

As I told you several posts ago you have the run in wrong

Sorry, I was backtracking and trying to get it working moving forward. Without kwargs I get into run_light in lights.py, but it stops when reading kwargs[“light_device”]. The concept of kwargs is new to me, so I thinking I’m not actually passing or defining them correctly. Sorry, I’m coming from Windows programming land - lol

Error log shows:

2018-02-23 15:52:19.993990 WARNING AppDaemon: ------------------------------------------------------------
2018-02-23 15:53:19.884156 WARNING AppDaemon: ------------------------------------------------------------
2018-02-23 15:53:19.885567 WARNING AppDaemon: Unexpected error during loading of TimedLights:
2018-02-23 15:53:19.886767 WARNING AppDaemon: ------------------------------------------------------------
2018-02-23 15:53:19.890139 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1651, in read_app
    self.init_object(name, class_name, module_name, self.app_config)
  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1446, in init_object
    self.objects[name]["object"].initialize()
  File "/config/appdaemon/apps/timed_lights.py", line 13, in initialize
    self.run_in(self.do_lights(), 0)
  File "/config/appdaemon/apps/timed_lights.py", line 19, in do_lights
    self.lights.run_light()
  File "/config/appdaemon/apps/lights.py", line 12, in run_light
    light_device = kwargs["light_device"]
NameError: name 'kwargs' is not defined

You pass it in as a parameter after the callback

1 Like

FInally got it. Thanks for the help!

timed_lights.py

import appdaemon.plugins.hass.hassapi as hass
import datetime
 
    class TimedLights(hass.Hass):
      def initialize(self):
        self.lights = self.get_app('Lights')
        kwargs = {"light_device": "light.pedestal", "light_value": "on", "light_color": "255,0,0", "light_brightness":"255"}
        time = datetime.time(13, 26, 1)
        self.run_in(self.lights.run_light, 2, **kwargs)

lights.py

import appdaemon.plugins.hass.hassapi as hass
 
class Lights(hass.Hass):
  def initialize(self):

  def run_light(self, kwargs):
    light_device = kwargs["light_device"]
    light_value = kwargs["light_value"]
    if "light_color" in kwargs:
      light_color = kwargs["light_color"]
    if "light_brightness" in kwargs:
      light_brightness = kwargs["light_brightness"]
    else:
      light_brightness = 255
      
    if light_value == "on":
      self.turn_on(light_device, brightness = light_brightness)
    elif light_value == "off":
      self.turn_off(light_device)
1 Like