Hello, I’m trying to convert some of my automations to appdaemon. So i got this frankencode from various sources and it kinda works.
class MotionLights(appapi.AppDaemon):
def initialize(self):
self.handle = None
# Check some Params
# Subscribe to sensors
if "sensor" in self.args:
self.listen_state(self.motion_on, self.args["sensor"])
self.listen_state(self.motion_off, self.args["sensor"])
else:
self.log("No sensor specified, doing nothing")
def motion_on(self, entity, attribute, old, new, kwargs):
if new == "on":
if "entity_id" in self.args:
self.log("Motion detected: turning {} on".format(self.args["entity_id"]))
self.turn_on(self.args["entity_id"], brightness = int(self.args["brightness"]), rgb_color = [self.args["rgb1"],self.args["rgb2"], self.args["rgb3"]])
self.cancel_timer(self.handle)
def motion_off(self, entity, attribute, old, new, kwargs):
if new == "off":
if "entity_id" in self.args:
self.log("Motion stoped on: {}".format(self.args["sensor"]))
if "delay" in self.args:
delay = self.args["delay"]
else:
delay = 60
self.cancel_timer(self.handle)
self.handle = self.run_in(self.light_off, delay)
def light_off(self, kwargs):
if "entity_id" in self.args:
self.log("Change brightness on {} for 10 seconds then turn off".format(self.args["entity_id"]))
self.call_service("light/turn_on", entity_id = (self.args["entity_id"]), brightness = 50)
self.cancel_timer(self.handle)
self.handle = self.run_in(self.light_off_delay, 10)
def light_off_delay(self, kwargs):
self.cancel_timer(self.handle)
self.handle = self.turn_off(self.args["entity_id"])
def cancel(self):
self.cancel_timer(self.handle)
How can i stop this part of code to execute if state of light is off?
def motion_off(self, entity, attribute, old, new, kwargs):
if new == "off":
if "entity_id" in self.args:
self.log("Motion stoped on: {}".format(self.args["sensor"]))
if "delay" in self.args:
delay = self.args["delay"]
else:
delay = 60
self.cancel_timer(self.handle)
self.handle = self.run_in(self.light_off, delay)
def light_off(self, kwargs):
if "entity_id" in self.args:
self.log("Change brightness on {} for 10 seconds then turn off".format(self.args["entity_id"]))
self.call_service("light/turn_on", entity_id = (self.args["entity_id"]), brightness = 50)
self.cancel_timer(self.handle)
self.handle = self.run_in(self.light_off_delay, 10)
def light_off_delay(self, kwargs):
self.cancel_timer(self.handle)
self.handle = self.turn_off(self.args["entity_id"])
def cancel(self):
self.cancel_timer(self.handle)
I know that i have to use self.get_state(self.args[“entity_id”]) but i’m having trouble to figure that part.