Presence Awareness Issues

Wondering if someone can help me out. I’m new to AppDaemon, and python. I’ve created a few automations based on time and sensors, and they are all working fine. I created one based on my presence, and it was working fine yesterday. For some reason, at 3:00am, it decided my state changed and ran the automation. I turned everything off, and after a few minutes it happened again.

I checked the status of my device tracker, and the status hadn’t changed since I arrived home hours before. I’ve pasted my code and what’s listed in my apps.yaml file below.


class Presence(hass.Hass):

  def initialize(self):
    self.listen_state(self.state_change, self.args["tracker"])

  def state_change(self, entity, attribute, new, old, kwargs):
    dave = self.get_tracker_state(self.args["tracker"])
    if dave == "home":
        self.arrive_home()

    else:
        #dave == "not_home":
        self.leave_home()
        
  def arrive_home(self):
    self.turn_on(self.args["script_unlock"])
    self.turn_on(self.args["scene_on1"])
    self.turn_on(self.args["scene_on2"])

   def leave_home(self):
    self.turn_on(self.args["script_lock"])
    self.turn_on(self.args["scene_off1"])
    self.turn_on(self.args["scene_off2"])

dave_presence:
  module: presence
  class: Presence
  script_unlock: script.1517501901267
  script_lock: script.1517500849790
  tracker: device_tracker.dave_s8
  scene_on1: scene.living_room_lights_100
  scene_on2: scene.kitchen_ceiling_lights_100
  scene_off1: scene.interior_lights_off
  scene_off2: scene.remotes_off

first:
you dont need to check with get_tracker_state, the state is directly there as variable named new.
second:
devicetrackers are not always stable.
if the code got triggered then the state will have changed, but probably only for a flash moment.
if i look in hass i see them flash from home to not_home and back regularly.
if that happens more often you could use a duration.

then it will only trigger if the state is not_home or home for the duration from … seconds.

Thanks! I’ll give it a shot.

Hi, I used to have that issue quite a lot with mine as well. I switched to using Bayesian sensors with multiple device trackers and that seems to have resolved the problem. Take a look at the Bayesian sensors.

I changed some of my code to this, and so far haven’t had any issues.

    self.listen_state(self.arrive_home, self.args["tracker"], old = "not_home", new = "home", duration=5)
    self.listen_state(self.leave_home, self.args["tracker"], old = "home", new = "not_home", duration=5)
2 Likes