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")