Hi!!
AppDaemon n00b here, but I do have a light background in Python and I’ve been using HA heavily for about a year. I copied one of the example pieces of code for Sequence (I’ll paste it below for reference) and created entries in my apps.yaml to get it working. Works like a champ, thanks!! However, I want to modify it, and I’m not sure what the best option is…
Currently it’s triggering when my door shuts, based on a contact sensor, but with a 20 minute delay. I want to instead trigger it when the door lock is unlocked, reduce the delay to 10 minutes (for things like getting groceries, etc) but only actually engage the lock if the door is closed. I imagine something like:
def action(self, kwargs):
if self.args["sensor"].get_state() == "on":
self.call_service(kwargs["service"], entity_id = kwargs["device"])
would work? Any thoughts? Better options?
Thank you!
-Chad
sequence.py (starting with the class):
class Sequence(appapi.AppDaemon):
def initialize(self):
if "sensor" in self.args and "state" in self.args:
# Call the method that needs to run:
self.listen_state(self.state_change, self.args["sensor"], new = self.args["state"])
def state_change(self, entity, attribute, old, new, kwargs):
# self.verbose_log("{} turned {}".format(entity, new))
if "sequence" in self.args:
for entry in self.args["sequence"]:
self.run_in(self.action, entry["delay"], device = entry["entity"], service = entry["service"])
def action(self, kwargs):
self.log("Calling {} on {}".format(kwargs["device"], kwargs["service"]))
self.call_service(kwargs["service"], entity_id = kwargs["device"])
apps.yaml (relevant section):
Lock The Back Door:
module: sequence
class: Sequence
sensor: binary_sensor.back_door
state: "on"
sequence:
- entity: lock.back_door_lock
service: lock/lock
delay: 1200 # 20 min