Manual Home Alarm state restoration on HASS restart

Hello everyone.

I have defined a manual alarm:

alarm_control_panel:
  - platform: manual
    name: "Home Alarm"
    pending_time: 60
    trigger_time: 120
    disarm_after_trigger: false

and would like the alarm state to be kept on HASS reboot, but it doesn’t seem to be, although recorder is active for all domains.

recorder:
  db_url: !secret mysql_db
  purge_days: 7

Does anyone if this state restoration on HASS reboot is even possible for a manual home alarm?

Thanks in advance.

1 Like

The recorder currently does not save and restore the alarm state.

As to whether it is possible - yes it is. I’ve achieved this using the AppDaemon with a modification of the SwitchReset app. If you’re interested in such an implementation I can share it.

I would appreciate it.

Thanks in advance.

There you go:

You’d need to configure first AppDaemon, I haven’t tested it with AppDaemon 2, still running 1.5.2 (I think)

1 Like

I’ll have to check AppDaemon first, that’s a given.

Thanks anyway.

Hi man!
You saved my life.

I’ve configured your setup for my Manual_MQTT_Alarm Control Panel because it didn’t keep the previous state. With your configs now it works!

Thanks

Which appdaemon did you use? is it 2? mine is not working, throwing this error:

    2017-11-10 22:05:39.199591 WARNING Worker Ags: {'kwargs': {}, 'name': 'switch_reset', 'type': 'timer', 'function': <bound method SwitchReset.set_switches of <switch_reset.SwitchReset object at 0x759b2390>>, 'id': UUID('0b000dcd-b390-44f5-85ec-230d139e1e9a')}
2017-11-10 22:05:39.200189 WARNING ------------------------------------------------------------
2017-11-10 22:05:39.206381 WARNING Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/appdaemon/appdaemon.py", line 505, in worker
    function(utils.sanitize_timer_kwargs(args["kwargs"]))
  File "/home/homeassistant/.homeassistant/appdaemon/apps/switch_reset.py", line 59, in set_switches
    dev_db = shelve.open(self.args["file"])
  File "/usr/lib/python3.4/shelve.py", line 239, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
  File "/usr/lib/python3.4/shelve.py", line 223, in __init__
    Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
  File "/usr/lib/python3.4/dbm/__init__.py", line 88, in open
    raise error[0]("db type could not be determined")
dbm.error: db type could not be determined

I’m still using AppDaemon 1.5.2

How would I integrate this with hass.io? I installed AppDaemon 0.4.0 from https://github.com/hassio-addons/repository, but I’m not sure where to add your .cfg file and the switch_reset.py?

Thanks!

Also interested in this with the latest AppDaemon (3?). My Hass often seems to hang and I have a script to restart it, but I lose alarm state when that happens.

I’m getting the same error :frowning:

OK got past that :slight_smile:

I’ve now got this working. I’ll happily share if people are still interested :smiley:

Please do!

So instead of trying to extend the set switch app, I just created a new one just for resetting the alarm. Mainly to reduce the complexity while I was learning and in case I ran into issues. Here’s what I ended up with:

import appdaemon.plugins.hass.hassapi as hass
import shelve
import time

#
# App to reset alarm_control_panel to previous state after HA restart
#
# Args:
#
# delay - amount of time after restart to set the state
# file - file in which to save state
#
# Release Notes
#
# Version 1.0:
#   Initial Version

class AlarmReset(hass.Hass):

  def initialize(self):
    
    self.device_db = shelve.open(self.args["file"])
    self.listen_event(self.ha_event, "plugin_started")
    self.listen_event(self.appd_event, "appd_started")
    self.listen_state(self.state_change, "alarm_control_panel")
       
  def ha_event(self, event_name, data, kwargs):
    self.log_notify("Home Assistant restart detected")
    self.run_in(self.set_alarm, self.args["delay"])
    
  def appd_event(self, event_name, data, kwargs):
    self.log_notify("AppDaemon restart detected")
    self.run_in(self.set_alarm, self.args["delay"])

  def state_change(self, entity, attribute, old, new, kwargs):
    self.log_notify("State change: {} to {}".format(entity, new))
    self.device_db[entity] = new
  
  def set_alarm(self, kwargs):
    self.log_notify("Checking for alarm_control_panel")
    # Find out what devices are avaiable.
    # If we don't know about it initialize, if we do set the switch appropriately
    state = self.get_state()
    for entity in state:

      type, id = entity.split(".")
      if type == "alarm_control_panel":
        if entity in self.device_db:
          if self.device_db[entity] != state[entity]["state"]:
            self.log_notify("Setting {} to {} (was {})".format(entity, self.device_db[entity], state[entity]["state"]))
            new_state = self.set_state(entity, state = self.device_db[entity])
        else:
          self.log_notify("Adding {}, setting value to current state ({})".format(entity, state[entity]["state"]))
          self.device_db[entity] = state[entity]["state"]
  
  def log_notify(self, message, level = "INFO"):
    #if "verbose_log" in self.args:
    self.log(message)

The bit that fixed the error above was changing

self.listen_event(self.ha_event, "ha_started")

to

self.listen_event(self.ha_event, "plugin_started")

as that changed in recent versions of appdaemon.

I also learnt that you shouldn’t create an empty file for it to save into - you need to just supply it with the name you want and let it create the file.

Have tested it a few times and seems to work fine :smiley:

Hmmm, not quite working for me. I see this when I change the alarm:

INFO Alarm_Reset: State change: alarm_control_panel.home_alarm to armed_home

But on reboot, i get:

INFO Alarm_Reset: Adding alarm_control_panel.home_alarm, setting value to current state (disarmed)

Doesn’t seem to be writing to my DB file, tried changing ownership/permissions and no luck.

My apps.yaml config is this:

Alarm_Reset:
  class: AlarmReset
  module: alarmreset
# Choose where to store the states across restarts
  file: "/home/homeassistant/.homeassistant/preserved_states"
  delay: 10

Strange. If you cat the DB file after a state change you should see the latest state at the end of the cat output.

Interesting, working now.

1 Like

One important thing I found out is that you should close and open the database after each “save” operation. If it’s not saved and there’s a power outage you’ll lose the saved state.

Shelve doesn’t save to the file after every operation? It must do, as my file changes whenever the state does.