Need help with this simple test python

I’m working on learning how to script with python and appdaemon. I’m working on moving at the moment some of my simple automations out of smartthings and over to HA. I’m in no way yet ready to completely uncouple from ST yet but figured I’d start with the fun stuff and then eventually move onto the tedious stuff of moving the zwave/zigbee things.

So as I’m working on learning this I followed the guide on learning python and I just wanted something simple and dumb to start with.

import homeassistant.appapi as appapi

class testing(appapi.AppDaemon):

  def initialize(self): 
    self.listen_state(self.light, "light.kitchen_light", new="on")

  def turn_off_kitchen_light_if_on (self, entity, attribute, old, new, kwargs):
    kitchen_light_switch = self.get_state("light.kitchen_light")
    if  kitchen_light_switch == "on":
      self.turn_off("light.kitchen_light")

Just as a learning example I wanted to do if you turn on the kitchen light it immediately turns back off.

But I can’t for the life of me figure out why this isn’t executing.

My next script will be turning on the outside lights at 20 mins after sunset and then off at sunrise but figured I’d start a little simpler as per the tutorial.

The first argument to listen_state is the name of a callback function. I don’t see any function in this class named light. You probably meant “self.turn_off_kitchen_light_if_on”.

1 Like

like @anderson110 said, you need to put the defname in the listen_state.
this might help you:

1 Like

Thank you both very much. I can script bash like a mad man but this is my first foray into python so I’m trying to learn :slight_smile:

1 Like

@ReneTode So your guide is actually exactly what I found just in html format. The git format is a lot easier to follow. I just didn’t follow you exactly since I don’t have a boolean to enable/disable so I figured I’d just go with something extremely simple and stupid :slight_smile: . Light turns on turn it back off

import homeassistant.appapi as appapi

class testing(appapi.AppDaemon):

  def initialize(self): 
    self.listen_state(self.turn_off_back_porch_light_if_on,"input_light.back_porch_light", new="on")

  def turn_off_back_porch_light_if_on (self, entity, attribute, old, new, kwargs):
    kitchen_light_switch = self.get_state("light.back_porch_light")
    if kitchen_light_switch == "on":
       self.turn_off("light.back_porch_light")

I feel like I’m missing something really simple and its staring me right in the face but I just don’t see it.

Yeah hold off on any replies. Turns out I was missing something. Put the script in the right dir. Testing it out now.

EDIT: Ok figured it out and thank you for your guide because I couldn’t use the import homeassistant.appapi instead had to use the below code

import appdaemon.appapi as appapi

class testing(appapi.AppDaemon):

  def initialize(self): 
    self.listen_state(self.turn_off_back_porch_light_if_on,"light.back_porch_light", new="on")

  def turn_off_back_porch_light_if_on (self, entity, attribute, old, new, kwargs):
    back_porch_light_switch = self.get_state("light.back_porch_light")
    if  back_porch_light_switch == "on":
      self.turn_off("light.back_porch_light")

Now if the back porch light turns on its immediately turned off. Off to more fun adventures :slight_smile:

1 Like

Ok so I followed your part 2 of your tutorial and I have this

import appdaemon.appapi as appapi
import datetime

class sun_down_lights(appapi.AppDaemon):

  def initialize(self):
    self.run_at_sunset(self.light_on_function,offset=int (self.args["sunset_offset"])
    self.run_at_sunrise(self.light_off_function,offset=int(self.args["sunrise_offset"])

  def light_on_function (self, kwargs):
      self.turn_on(self.args["lightID"])

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

but its complaining about the sunrise part

    self.run_at_sunrise(self.light_off_function,offset=int(self.args["sunrise_offset"])
       ^
SyntaxError: invalid syntax

I’ve been reading through everything I can find about this but I can’t find anything thats wrong with it. There were 2 commas initially with the function and the offset but I removed those to make it happy and I also noticed that according to the docs its no longer runatsunrise/runatsunset instead its now run_at_sunrise/run_at_sunset so I’m working on trying to understand the syntax here but I don’t get why its ok with the first entry but has an issue with the second one.

Keeping it simple before I move onto using my motion sensors to turn on lights :slight_smile: Thanks to your tutorial with explaining how to use functions in here saves a lot of time with repeating things over and over for different scenarios.

1 Like

Hah parenthesis the nemesis of all times.

  def initialize(self):
    self.run_at_sunrise(self.light_off_function, offset=int (self.args["sunrise_offset"]))
    self.run_at_sunset(self.light_on_function, offset=int (self.args["sunset_offset"]))
2 Likes

i corrected those errors in the code.
but in your case it probably helped you to learn understanding errors :wink:

lol yeah I actually thought you did that on purpose for the student to figure out on their own about what the problem is and why. I liked it as a teaching tool

Thank you master

1 Like