Error in switch_reset on HASS restart

Ah great point. But

if hasattr(self, 'device_db') == False:
  self.device_db = shelve.open(self.args["file"])

does not work, it always returns False.

I can’t close the shelve on close due to:

So I am out of ideas again.

you dont need to close the db when hass stops.
it generates warnings but thats no problem.
you want the db to function when hass is restarted.

the terminate function runs in front of the init.
so if you close it in the terminate you shoulf get:

  1. everything running correct
  2. hass is stopped, warnings are generated
  3. hass restarts
  4. ad starts again and executes the terminate warnings stop
  5. ad starts the init and everything is running correct.

Ahh had not found terminate. Seems to work so far, thanks!

  def terminate(self):
    self.device_db.close()
1 Like

I stopped using this app last April when the functionality was moved in to HA. Is there some reason you are still using it?

i didnt even know they added it.
can you tell me in which update they added it?
and is it optional or automaticly?

Looking at my git log, I deleted switch_reset.py on April 26th, so it was a release or two (or three) before then.

Seems the trick would be removing the “initial” tag from input_booleans:

Seems it was added around March:

I also had the idea this was implemented but it never worked for me, this explains why…

as i see it it was 0.39 and i run 0.40
but i remember now that it didnt work for me either.
i had the same problem with that as with the app, it did set the states after starting, triggering all kind of things on restart.

and i cant use it now anyway because i kicked out the recorder long ago.

What helped me a lot to prevent those issues is this automation:

alias: 'Delay automations on restart'
hide_entity: true
trigger:
   - platform: homeassistant
     event: start
action:
  - service: automation.turn_off
    data:
      entity_id:
        - group.all_automations
  - delay:
      seconds: 120
  - service: automation.turn_on
    data:
      entity_id:
        - group.all_automations

my biggest problem is that i have mostly “dumb” devices.
TV’s for instance are switched on and off with the same command.

if i have turned on the TV the state in hass is “on”
after a restart hass turn the switch to on and that causes the TV to turn off.

those kind of things are only to be prevented if HA changes the state BEFORE it is active.
so the saved state must be first state to avoid state_change.
as soon as a state is set i can only supress the action by registring that its a first change, and that can be done in AD.

@liquidox is there a way you can post your full switch_reset app here? I seem to have the same problem but I have no idea/experience in writing/editing the apps myself, I just steal with pride. So just re-using your app would be great!

@ReneTode; this seems to be the sam issue we where discussing here

Oh I don’t actually use switch_reset anymore, I do use the (related) persistence package:

And some of my Appdaemon apps control their own initial state upon reloads.

Thanks. This made me search again on all the options. It seems this is now default behavior of HASS. https://www.home-assistant.io/components/recorder/#restore-state

This works great as long as you do not (!) use an initial state.

The problem using the HA behaviour is that it doesn’t survive the database purge. Anything that doesn’t change for the length of the database purge time will not be restored, and you get a random value.

Ouch! What is your preferred method?

I have gone back to switch_reset.py. I moved away from it when HA got the capability of restoring from the database, but then moved back when I discovered I had a number of input_booleans that don’t change state very often, but I need restoring correctly when needed.

Thanks. Would you mind posting the full app you are using now? I’m encountering some errors and seem to be caused by some database issues and have no clue how to adapt it myself!

Greatly appreciated!

It is whatever is in the appdaemon release. I haven’t altered it.

Are you sure all is still working correctly? Im having issues

I haven’t restarted in several weeks, so I can’t be sure, but your error looks more like something with the file system rather than anything wrong with the app.

1 Like