Switch persistence

Should GPIO switch states persist through a restart?

I thought they use to (could be wrong) but currently they are definitely not. At least for me (v 0.76.1).

depends what you mean by restart.
I would have thought (to be confirmed) that if you only restart HA, state would persist, but if you reboot the Pi, I really doubt it would…

Yeah restart the service not the pi.

Problem solved: I had stupidly excluded the switch domain from history.

Edit: Hmm… or maybe not. I still see GPIO switches being turned off after I restart HA and they are visible in the history log.

Could someone else confirm this please?

Turn a GPIO switch on.
Restart HA.
Check to see if the switch is on or off.

I remember that I noticed this a few days ago as well. However, I thought this behavior is by purpose and therefore, I did not do any more research on this.

do you see this behaviour only with GPIOs or also with other switches?
Also are you on hass.io or another HA installation?
when you restart ha, regardless of what ha says, is the gpio switch still on (ha reports wrong status) or is the switch off (ha actively turned the switch off)?

I only have GPIO switches.

Hassio.

I’ll check when I get home tonight.

Because HA basically drives the switches (not just calls some API to turn them on and get their status) I wonder if it may be as design. If so it’s defo something that should change. Once you’ve checked the above if it doesn’t work as expected you may need to raise a bug on github

Finally got a chance to check this. The GPIO state and frontend switch state are the same, i.e. both not being preserved after a HA restart.

I’m not 100% sure they were ever preserved. So perhaps this should be a feature request rather than a bug report?

yeah just checked and I have the same for switches that do not provide a state:
image
You could indeed raise a feature request, and in the meantime you could save each switch’s state at shutdown and restore at startup.
Check this post:

@lolouk44

don’t want to disrupt this thread, but since i might be misunderstanding: shouldn’t the HA instance simply read the state of lights and switches ? at restart it is supposed to show the state of these entities, rather than interfere with them? Unless of course you should want to specifically, in which case a startup script could take care of that?

oops never mind, missed the bit of not providing state… sorry

@tom_l for the restore state, you should enable recorder: , not the history:
No extra component is necessary to restore the states of these booleans and many other entities making these persistent after reboot. see https://www.home-assistant.io/components/recorder/#restore-state

the link for the component doesn’t give the python script when I click it, simply a empty page with a prompt? maybe it has moved.

I have the recorder enabled and no domains or entities excluded.

recorder:
  purge_interval: 1
  purge_keep_days: 7

I did have them excluded from the logbook. Removing those exclusions did not help.

not sure about the switches and lights (and i dont need them thus commented out) but try this:

include:
  domains:
    - sensor
    - switch
#    - media_player
#    - light
    - automation
#    - script
    - input_boolean
    - input_select
    - input_number

for the inputs.
Don’t forget to NOT use initial: in their configuration, otherwise you overwrite/prevent the restore functionality. Use only their name (and icon if you don’t customize that)

snippet of my input_boolean.yaml:

##########################################################################################
# input_booleans are using restore state, so no initial state is set here
# icons are state dependent and set in: customize_input_boolean.yaml
##########################################################################################

notify_motion:
  name: Notify motion
1 Like

This custom component GitHub - PiotrMachowski/Home-Assistant-custom-components-Saver: This custom component allows you to save current state of any entity and use its data later to restore it. did the job for me.