AlarmSystem with appdaemon

I have migrated my alarm system to appdaemon. The previous solution based on 350+ lines of automation.yaml and was a pain to maintain.

This system work with any light system (should support color) and with any sensors who report on/off values. I have tested it with xiaomi hub, sensors, buttons.

You can configure these settings:

Key Description Default
language language used for notifcations in text and audio english
device_trackers list of tracked devices (for auto arming / disarming) []
armed_home_binary_sensors list of binary sensors to monitor (like doors, motion, sensors) in case you are home []
armed_away_binary_sensors list of binary sensors to monitor (like doors, motion, sensors) in case you are away []
water_binary_sensors list of binary sensors to monitor (like water leakage sensors) all the time []
fire_binary_sensors list of binary sensors to monitor (like water smoke sensors) all the time []
fire_temperature_sensors list of temperature sensors to monitor (like temperature sensors) all the time []
armed_home_image_processing_sensors list of image processing sensors to monitor in case you are home []
armed_away_image_processing_sensors list of image processing sensors to monitor in case you are away []
alarm_control_buttons list of buttons to control (single click arm_away, double click disarm, long pres arm_home) []
alarm_lights list of lights to indicate alarm status (red is triggered, blinking is pending) []
alarm_arm_night_after_time time to arm at evening 23:15:00
alarm_arm_night_before_time time to disarm at morning 06:00:00
sleep_after_time start of sleeptime at evening 23:15:00
sleep_before_time end of sleeptime at morning 06:00:00
vacation_control input boolean to configure a vacation mode which arms using armed_vacation None
guest_control input boolean to configure a babysitter mode which does not auto arm None
silent_control input boolean to mute the alarm sound None
siren_switches list of siren switches to turn on or off []
notify_service script to call for notification None
notify_title title parameter for script AlarmSystem triggered, possible intruder
cameras List of cameras (snapshots will be made once alarm is triggered) []
camera_snapshot_path Path for storing camera snapshots /tmp
camera_snapshot_regex Regex for camera snapshots “camera_.*\d+_\d+.jpg”
alexa_monkeys list of alexa voice monkeys for audio notifications []
telegram_user_ids list of telegram user ids used for notifications []

Feel free to report any errors, I will migrate this stuff to a real github repo in the future…

4 Likes

seems quite nice.

you could explain a little more about which parts are optional and which not.
and for instance i have no clue what the xiaomi hub does or what it has to do with it and if i need it and why.

you have notify_service point to a script?
why isnt that just a name from the notify you want to use?

how does guest control work?

Mandatory are a list of sensors and a color light otherwise it does not make sense. The whole xiaomi stuff is optional but recommended. It provides you with a cheap alarm siren and some hardware buttons.
The guest control is a ha input which ensures that if you leave home but guests are still there the alarm does not get armed.
The idea is that you normally do not have to arm or disarm the system because everything works automatically.

The notify is a script call because you can extend it this way. I will rename it to triggered script call or something like this. I will also add scene support.

thanks for explaining. wont be of any use for me this way but maybe for someone else.

there are some parts in your code that makes me wonder why though.
in parts like this:

        i = 0
        for sensor in self._armed_home_sensors:
            self.listen_state(
                self.trigger_alarm_while_armed_home_callback, sensor, new="on", old="off", duration=i)
            i += 1

if i put in 4 sensors like [“sensor.a”,“sensor.b”,“sensor.c”,“sensor.d”]
that code translates to:

   self.listen_state(self.trigger_alarm_while_armed_home_callback, "sensor.a", new="on", old="off", duration=0)
   self.listen_state(self.trigger_alarm_while_armed_home_callback, "sensor.b", new="on", old="off", duration=1)
   self.listen_state(self.trigger_alarm_while_armed_home_callback, "sensor.c", new="on", old="off", duration=2)
   self.listen_state(self.trigger_alarm_while_armed_home_callback, "sensor.d", new="on", old="off", duration=3)

why would you have a duration from 0 seconds at all?
why would the duration for every sensor be different?

duration is the amount of seconds that the state listened for is right.

For sensors this is not really needed. The idea is that you do not get 4 events at the same time. If you delay them a bit only the first one will trigger an action. The subsequent ones will see that the state is already set and just do nothing. I am not sure if appdaemon will call callbacks at the same time …

thats not the way it works.

now the fourth trigger needs to be in triggered state before it calls the callback.
a callback is called at the moment that a state changes in HA.

you dont change the time that the callback is called by the duration of a state.
with the devicetrackers it does even make less sence.
a duration from 300 or 301 does not make that the events take place at different moments.
if the second devicetracker changes state a second after the first they will still fire the callback at the exact same time.

make a new app like this:

init(...)
  self.listen_state(self.callback,"input_boolean.test")
  self.listen_state(self.callback2,"input_boolean.test")
def callback(self,...):
  self.log("callback 1 is called")
def callback2(self,...):
  self.log("callback 2 is called")

you will have 1 event doing 2 things. “at the same time”

Does this work with the Manual alarm control panel with MQTT support in HASS?
I use this panel together with an Android tablet and an MQTT alarm panel app to switch my alarm on and off.

(https://home-assistant.io/components/alarm_control_panel.manual_mqtt/)

1 Like

I would think so because I do not see much difference in the implementation.

I have migrated the app from gist to github:

after upgrade from 0.77.3 to 0.78.3 Alarm System do not start automatically
Similar to what happened when I upgraded AppDaemon to v.3

What happened?

Thanks in advance

is appdaemon running correct?
do you have errors in your log?

do you have api password set in HA and is it the same as your ha_key in appdaemon.yaml?

Many thanks ReneTode
I setted api password in HA the same as in ha_key in appdaemon.yaml and all works fine again.
I hope problem is solved

next time, first look at the logs if appdaemon is running correct and if there are errors :wink:
i wont hurt to do that on regular basis and it can tell you if there is a problem.

I get error like this:


2019-04-14 15:25:58.152920 INFO AppDaemon: Reading config


2019-04-14 15:25:58.260056 INFO AppDaemon: /conf/apps/apps.yaml added or modified


2019-04-14 15:25:58.260260 INFO AppDaemon: App 'alarm_system' changed


2019-04-14 15:25:58.260885 INFO AppDaemon: Terminating alarm_system


2019-04-14 15:25:58.261145 INFO AppDaemon: Initializing app alarm_system using class AlarmSystem from module alarm


2019-04-14 15:25:58.262143 INFO alarm_system: Hello from AlarmSystem


2019-04-14 15:25:58.263017 INFO alarm_system: Got armed_home sensors ['binary_sensor.door_window_sensor_garage_door']


2019-04-14 15:25:58.263874 INFO alarm_system: Got armed_away sensors ['binary_sensor.door_window_sensor_garage_door', 'binary_sensor.door_window_sensor_bathroom', 'binary_sensor.door_window_sensor_kitchen', 'binary_sensor.door_window_sensor_dining_room_garage', 'binary_sensor.door_window_sensor_main_door', 'binary_sensor.door_window_sensor_chill_out_corner', 'binary_sensor.door_window_sensor_porch']


2019-04-14 15:25:58.264711 INFO alarm_system: Got device trackers ['device_tracker.redminote5phonethor']


2019-04-14 15:25:58.264875 WARNING AppDaemon: ------------------------------------------------------------


2019-04-14 15:25:58.264986 WARNING AppDaemon: Unexpected error running initialize() for alarm_system


2019-04-14 15:25:58.265122 WARNING AppDaemon: ------------------------------------------------------------


2019-04-14 15:25:58.265459 WARNING AppDaemon: Traceback (most recent call last):


  File "/usr/local/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1581, in init_object


    init()


  File "/conf/apps/alarm.py", line 51, in initialize


    self.count_home_device_trackers(), self.count_not_home_device_trackers()))


  File "/conf/apps/alarm.py", line 132, in count_home_device_trackers


    return self.count_device_trackers("home")


  File "/conf/apps/alarm.py", line 127, in count_device_trackers


    if self.get_state(sensor) == state:


  File "/usr/local/lib/python3.6/site-packages/appdaemon/appapi.py", line 228, in get_state


    return self.AD.get_state(namespace, device, entity, attribute)


  File "/usr/local/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 701, in get_state


    return deepcopy(self.state[namespace])


KeyError: 'device_tracker.redminote5phonethor'

Are you sure that the device tracker name is correct? It looks like the device could not be found…

thanks.solvedi used old gist repo without appdaemon v3 support working fine but the documentation is weak… I’d like using my xmpp notification Plantform

The notification is quite flexible because you can run any script. The script can be configured in home assistant.

It has to be in /script folder inside conf folder of homeassistant?

It is just a home assistant script. You can check out this example:

Strange issue since last home assistant 99.x The alarsystem does not recognize when device_tracker goes to not_home But when it goes to home… Strange isn’t it?

here the logs:

2019-09-27 15:38:17.950538 INFO alarm_system: Got armed_home sensors ['binary_sensor.door_dining_room_garage', 'binary_sensor.door_entrance', 'binary_sensor.door_garage_garden', 'binary_sensor.garage_door', 'binary_sensor.window_chill_out_corner', 'binary_sensor.door_porch']
2019-09-27 15:38:17.952213 INFO alarm_system: Got armed_away sensors ['binary_sensor.door_dining_room_garage', 'binary_sensor.door_entrance', 'binary_sensor.door_garage_garden', 'binary_sensor.garage_door', 'binary_sensor.window_bathroom_downstairs', 'binary_sensor.window_bathroom_upstairs', 'binary_sensor.window_bedroom', 'binary_sensor.window_chill_out_corner', 'binary_sensor.window_evan', 'binary_sensor.window_bathroom_downstairs', 'binary_sensor.window_kitchen', 'binary_sensor.window_rose', 'binary_sensor.door_porch']
2019-09-27 15:38:17.954035 INFO alarm_system: Got device trackers ['device_tracker.phone_emilie', 'device_tracker.phone_thorsten']
2019-09-27 15:38:17.957056 INFO alarm_system: Got 0 device_trackers home and 2 device_trackers not home
2019-09-27 15:38:17.958331 INFO alarm_system: Got guest_mode False
2019-09-27 15:38:17.959310 INFO alarm_system: Got vacation_mode False
2019-09-27 15:38:17.960288 INFO alarm_system: Got silent mode False
2019-09-27 15:38:17.961231 INFO alarm_system: Got info volume 10
2019-09-27 15:38:17.962188 INFO alarm_system: Got alarm volume 100
2019-09-27 15:38:17.963601 INFO alarm_system: Got notify service notify.thorsten
2019-09-27 15:38:17.965289 INFO alarm_system: Got alarm state disarmed
2019-09-27 15:39:00.007138 INFO alarm_system: Callback alarm_disarm_auto from device_tracker.phone_thorsten:state not_home->home
2019-09-27 15:39:00.008659 INFO alarm_system: Ignoring status home of device_tracker.phone_thorsten because alarm system is in state disarmed
```