Auto locking my doors

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

Personally, I like to include as many of conditions as possible in the listen parameter, to make the callbacks as specific as possible. In general, this makes the callbacks clearer and more reusable.

So I would have

    self.listen_state(self.door_unlocked, new="on",  old="off", duration=600)

and then the callback is only called when the device has switched for 10 minutes, so the logic is simpler.

no wont work.
you need self.get_state(self.args[“sensor”]) == “on”:

check out the API for how to use functions.
http://appdaemon.readthedocs.io/en/2.1.12/APIREFERENCE.html

1 Like

Thanks!!
After messing around with it after I posted this, I got close to what you posted. I left off the self:

  if get_state(self.args["sensor"]) == "on":
NameError: name 'get_state' is not defined

That’s in my error log when I tried that. I’ll add the self.get_state…

Appreciate the guidance and the link!

-Chad

1 Like