Manual Home Alarm state restoration on HASS restart

Hmmm…yeah it’s not picking up all the changes, only some.

Hmm that’s odd?

image

Yeah not picking up at all. File isn’t modifying either.

Must be a permissions thing then?

File is owned by homeassistant user and i just tried it as 777, still no luck.

Tried deleting the file and restarting. Stumped. It writes some gibberish to the file but no alarm state.
image

Yeah I think the gibberish is just the shelve db meta data or something. Normally the state stuff is in plaintext at the end of the gibberish. I’ll have a look at mine when I get home to see if there’s anything about how I’ve set it up.

Not under all circumstances. I found out when killing appdaemon in testing that it doesn’t flush the saved data to the file immediately.

Ok will have to look at how to close, and move the open into the set_alarm routine.

Mine’s gibberish but with this at the end:

alarm_control_panel.ha_alarm▒Xpendingq.alarm_control_panel.ha_alarm▒disarmedq.alarm_control_panel.ha_alarm▒X

I run appdaemon in a docker container and my db file is owned by root with 644 perms.

Switched to root being owner and 644, still no luck, must be something odd with my setup. I’ll keep messing around, thanks. Wish this just worked as it’s by far the easiest solution.

1 Like

Hello,

For those interested, I updated the source code of Soul to work on my environment with a recent HA (0.98) :

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.1:
#   Customized version

class AlarmReset(hass.Hass):

  def initialize(self):
    
    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):
    device_db = shelve.open(self.args["file"])
    self.log_notify("State change: {} to {}".format(entity, new))
    device_db[entity] = new
    device_db.sync();
    device_db.close();
  
  def set_alarm(self, kwargs):
    device_db = shelve.open(self.args["file"])
    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 device_db:
          if device_db[entity] != state[entity]["state"]:
            self.log_notify("Setting {} to {} (was {})".format(entity, device_db[entity], state[entity]["state"]))
            self.call_service("alarm_control_panel/ifttt_push_alarm_state", entity_id = entity, state = device_db[entity])
            #new_state = self.set_state(entity, state = device_db[entity])
        else:
          self.log_notify("Adding {}, setting value to current state ({})".format(entity, state[entity]["state"]))
          device_db[entity] = state[entity]["state"]
    device_db.sync();
    device_db.close();
      
  def log_notify(self, message, level = "INFO"):
    #if "verbose_log" in self.args:
    self.log(message)

You will need to adapt the service call to the right on, in my case "alarm_control_panel/ifttt_push_alarm_state"

I also got around this by using NodeRed to save and restore the last known state.

@Soul : I have same issue with manual alarm panel on HA 0.114.2
After HA restart, alarm panel always trigger to arm_away
However my alarm panel setting need code to disarm so this apps won’t work
I can see from log, apps trying to disarm, but won’t do anything since need code
Any work-around?

2020-08-19 17:56:51.031856 INFO AppDaemon: Initializing app Alarm_Reset using class AlarmReset from module alarmreset
2020-08-19 17:56:51.138311 INFO AppDaemon: App initialization complete
2020-08-19 17:56:53.257955 INFO Alarm_Reset: AppDaemon restart detected
2020-08-19 17:57:03.055090 INFO Alarm_Reset: Checking for alarm_control_panel
2020-08-19 17:57:03.324111 INFO Alarm_Reset: Adding alarm_control_panel.security_panel, setting value to current state (disarmed)
2020-08-19 17:57:57.253081 WARNING HASS: Disconnected from Home Assistant, retrying in 5 seconds
...
2020-08-19 17:59:42.536413 WARNING HASS: Disconnected from Home Assistant, retrying in 5 seconds
2020-08-19 17:59:51.317232 INFO HASS: Connected to Home Assistant 0.114.2
2020-08-19 18:00:06.615512 INFO HASS: Evaluating startup conditions
2020-08-19 18:00:59.316354 INFO HASS: Startup condition met: hass state=RUNNING
2020-08-19 18:00:59.319287 INFO HASS: All startup conditions met
2020-08-19 18:01:00.672264 INFO AppDaemon: Processing restart for HASS
2020-08-19 18:01:00.675627 INFO AppDaemon: Terminating Alarm_Reset
2020-08-19 18:01:00.684828 INFO AppDaemon: Initializing app Alarm_Reset using class AlarmReset from module alarmreset
2020-08-19 18:01:00.814267 INFO Alarm_Reset: Home Assistant restart detected
2020-08-19 18:01:06.226156 INFO Alarm_Reset: State change: alarm_control_panel.security_panel to arming
2020-08-19 18:01:11.150843 INFO Alarm_Reset: Checking for alarm_control_panel
2020-08-19 18:01:15.165774 INFO Alarm_Reset: State change: alarm_control_panel.security_panel to armed_away
2020-08-19 18:01:29.788189 INFO Alarm_Reset: State change: alarm_control_panel.security_panel to disarmed