Count how many times a sensor is triggered before starting the alarm

I’m using HA as my alarm system, but one of the things I would like to implement is that I need two motion sensors to be tripped, but somehow the alarm goes off when the first motion sensor triggers, so obviously I have an error in my app.

The thought was to create a variable “sensor_count” which starts as 0 then when a motion sensor triggers it should add 1 to that count and when the count is 2 or above the alarm should be triggered. Furthermore once the alarm is deactivated the sensor_count should be set to 0 again, so that when the alarm is on again there still needs to be two new motion sensor triggers before the alarm sounds.

But I can’t se why that doesn’t work?

import appdaemon.plugins.hass.hassapi as hass
import datetime

class alarm(hass.Hass):

  def initialize(self):

   self.listen_state(self.mode, "input_boolean.alarm_mode")

   group_skal = self.get_state("group.skal", attribute="entity_id")
   for entity in group_skal:
            self.listen_state(self.skal, entity, new='on')

   group_motion = self.get_state("group.motion", attribute="entity_id")
   for entity in group_motion:
            self.listen_state(self.motion, entity, new='on')
            
   self.listen_state(self.outside, "binary_sensor.presence_28", new = "on")
   
   self.sensor_count = 0
       
  def mode (self, entity, attribute, old, new, kwargs):
    state = self.get_state("input_boolean.alarm_mode")
    friendly_state =  "tændt" if state == "on" else "slukket" if state == "off" else state
    self.notify("Alarmen er {}".format(friendly_state), name = "telegram_all")
    if state == 'off':
      self.turn_off("input_boolean.burglary_mode")
      #global sensor_count
      self.sensor_count = 0
      

  def skal(self, entity, attribute, old, new, kwargs):
        friendly_name = self.get_state(entity, attribute="friendly_name")
        if old == "off" and new == "on" and self.get_state("input_boolean.alarm_mode") == 'on':
            self.notify(f"ALARM! {friendly_name} er åbnet!", name="telegram_all")
            self.turn_on("input_boolean.burglary_mode")

  def motion(self, entity, attribute, old, new, kwargs):
        friendly_name = self.get_state(entity, attribute="friendly_name")
        #global sensor_count
        self.sensor_count += 1
        if old == "off" and new == "on" and self.get_state("input_boolean.alarm_mode") == 'on' and self.sensor_count <2:
            self.notify(f"Bevægelse {friendly_name}, kun 1 sensor trigget -ingen alarm!", name="telegram_morten")
            self.motion
        elif old == "off" and new == "on" and self.get_state("input_boolean.alarm_mode") == 'on' and self.sensor_count >=2:
            self.notify(f"ALARM! Bevægelse {friendly_name}!", name="telegram_all")
            self.turn_on("input_boolean.burglary_mode")

  def outside(self, entity, attribute, old, new, kwargs):
      home = self.get_state("input_boolean.house_mode")
      if home == "off":
        self.notify(f"Bevægelse udenfor -ingen alarm!", name="telegram_morten")

Any errors in the appdaemon logs? Please post the logs. Also, add some parameter/flow logging to the code so you can see what is happening, i.e. self.log(self.count)

This line also looks a little out of place.

I thought I solved it by declaring my variable as global, but apparently that didn’t do the trick.

Now the result is, that after Appdaemon restart or no triggering af the alarm (only arming/disarming) first time a motion sensor turns on, then the alarm is triggered. When I then deactivate the alarm and a motions sensor turns on then I get a message that only one sensor was triggered so the alarm itself is not triggering yet, which is as intended. I don’t get any errors in the log, but I can’t figure out why it doesn’t work? It must be either the way I declare the variable or how I reference it in the individual functions?

import appdaemon.plugins.hass.hassapi as hass
import datetime

class alarm(hass.Hass):

  def initialize(self):

   self.listen_state(self.mode, "input_boolean.alarm_mode")

   group_skal = self.get_state("group.skal", attribute="entity_id")
   for entity in group_skal:
            self.listen_state(self.skal, entity, new='on')

   group_motion = self.get_state("group.motion", attribute="entity_id")
   for entity in group_motion:
            self.listen_state(self.motion, entity, new='on')
            
   #self.listen_state(self.outside, "binary_sensor.presence_28", new = "on")
   
   self.sensor_count = 0
       
  def mode (self, entity, attribute, old, new, kwargs):
    state = self.get_state("input_boolean.alarm_mode")
    friendly_state =  "tændt" if state == "on" else "slukket" if state == "off" else state
    self.notify("Alarmen er {}".format(friendly_state), name = "telegram_all")
    if state == 'off':
      self.turn_off("input_boolean.burglary_mode")
      global sensor_count
      self.sensor_count = 0
      

  def skal(self, entity, attribute, old, new, kwargs):
        friendly_name = self.get_state(entity, attribute="friendly_name")
        if old == "off" and new == "on" and self.get_state("input_boolean.alarm_mode") == 'on':
            self.notify(f"ALARM! {friendly_name} er åbnet!", name="telegram_all")
            self.turn_on("input_boolean.burglary_mode")

  def motion(self, entity, attribute, old, new, kwargs):
        friendly_name = self.get_state(entity, attribute="friendly_name")
        global sensor_count
        self.sensor_count += 1
        if old == "off" and new == "on" and self.get_state("input_boolean.alarm_mode") == 'on' and self.sensor_count <2:
            self.notify(f"Bevægelse {friendly_name}, kun 1 sensor trigget -ingen alarm!", name="telegram_morten")
        elif old == "off" and new == "on" and self.get_state("input_boolean.alarm_mode") == 'on' and self.sensor_count >=2:
            self.notify(f"ALARM! Bevægelse {friendly_name}!", name="telegram_all")
            self.turn_on("input_boolean.burglary_mode")

  #def outside(self, entity, attribute, old, new, kwargs):
  #    home = self.get_state("input_boolean.house_mode")
  #    if home == "off":
  #      self.notify(f"Bevægelse udenfor -ingen alarm!", name="telegram_morten")