Help with setting state on HASS restart

So I’m trying to set my alarm state on HASS restart, and I’ve modified the switch_reset app to just look at the alarm panel.

This is my code:

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
# 
#
# 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, "ha_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_switches, self.args["delay"])
    
  def appd_event(self, event_name, data, kwargs):
    self.log_notify("AppDaemon restart detected")
    self.run_in(self.set_switches, 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_switches(self, kwargs):
    self.log_notify("Setting switches")
    # 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)

and this is my apps config:

alarm_reset:
  module: set_alarm
  class: AlarmReset
  file: /conf/apps/preserved_states.db
  log: '1'
  delay: 0

it works to the point were it is saving the state into the preserved_states.db file. But when I restart my HASS container it simply restarts the app and doesn’t do anything. It doesn’t seem to see the HASS restart event as thi log line isn’t firing:

    self.log_notify("Home Assistant restart detected")


2018-03-22 23:56:08.730730 INFO AppDaemon: Initializing app alarm_reset using class AlarmReset from module set_alarm
2018-03-22 23:56:26.325019 INFO alarm_reset: State change: alarm_control_panel.ha_alarm to pending
2018-03-22 23:56:46.773951 INFO alarm_reset: State change: alarm_control_panel.ha_alarm to armed_home
2018-03-22 23:56:59.670491 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:04.672444 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:09.674792 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:14.676545 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:19.679023 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:24.681233 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:29.682907 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:34.688442 INFO AppDaemon: HASS: Connected to Home Assistant 0.65.5
2018-03-22 23:57:35.552707 INFO AppDaemon: Processing restart for HASS
2018-03-22 23:57:35.552909 INFO AppDaemon: Terminating hello_world
2018-03-22 23:57:35.553307 INFO AppDaemon: Terminating alarm_reset
2018-03-22 23:57:35.553770 INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
2018-03-22 23:57:35.554837 INFO hello_world: Hello from AppDaemon
2018-03-22 23:57:35.556067 INFO hello_world: You are now ready to run Apps!
2018-03-22 23:57:35.556197 INFO AppDaemon: Initializing app alarm_reset using class AlarmReset from module set_alarm

in the docs (breaking changes)

I introduced namespaces as a way of handling multiple plugins at the same time - the docs have more details, but if you are just using a single HASS instance, as everyone has been doing until now, you can safely ignore namespaces.

The “ha_started” event has been renamed to “plugin_started”
If you use this event, the name has been changed. The plugin started event has a parameter called name which gives the name of the plugin that was restarted.

Oh ok thanks! :slight_smile:

That was back in January though and I’ve only just started using appdaemon from the latest version, so I assumed the example apps would be current. Didn’t think to go back over old changelogs.

Works a treat now thank you!!!

1 Like

the problem is that the docs and example apps are grown to big.
it seems that Andrew once in a while just forgets to update a part. (it isnt much, but can happen)

where did you find that in the examples?

In the switch_reset example:

I basically copied this but stripped it down to just work for my alarm entiity. To fix my issue it was just a case of swapping

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

to

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

can you make a pull request to change it?
that way @aimc will notice it.

I’m a bit of a git noob, but I’ll have a go :slight_smile:

1 Like

Done (hopefully correctly!)

1 Like

Got it and merged it into the next release - thanks!

2 Likes