Save/restore state after HA restart

Any one know how I can save and restore some specific switches states so when I restart HA they are restored back to what they were?

I looked at Recorder, but no luck there. There’s some very hacky python scrips from 2018 that I am not sure will work now, nor I know how I would get them working.

Save the state of your switch in an input_boolean.

Home Assistant normally doesn’t store a switch’s state, on startup it gets the state from the switch’s integration.

Which integration are using for the switch? Why is this integration not providing the switch’s correct state on startup?

I am using Switchbot for my button presser, the app itself has a “on/off” state (for light/rocker mode), but I do not believe it is exposed. I am merely using IFTT to trigger the button presser.

I also have one to turn on/off appliances that need a on/off state (switchbot has a simple press mode), if HA is restarted and the state not restored, it can cause automation to do dangerous things, which is why I am looking for a way to store the last state of a switch, and restore them upon HA restart…

Can you elaborate more on this?

I made a helper, a toggle.

input_boolean:
  input_boolean.disk:
    name: DiskOn
    icon: mdi:car
    initial: on

I assume, something like this?
initial: on
Specifically set so HA will set this as default…

Although, I don’t get the “restore state”:

If I don’t specify an initial value it’ll restore previous state?

Where can I set this helper toggle onto the switch itself?

If Switchbot can’t report the device’s true state, it seems like a spectacularly bad idea to use it for an application that can produce a “dangerous” outcome.

Saving the switch’s state in an input_boolean provides no guarantee that, after a restart, the input_boolean’s state matches the switch’s state. There’s a better chance it does but it won’t be if, for example, the switch is manually toggled while Home Assistant is restarting.

Open source, lazy appliance developers (Switchbot)… I’ll take “better than nothing”, thanks…

This definitely won’t happen, for sure.

After setting the toggle helper, and the config, do you know how I can apply it to the specific switch?

I do but I’m abstaining from providing advice for this “better than nothing”, “definitely won’t happen, for sure” system. Good luck.

Not so helpful, but, nethertheless, I managed.

Switch on/off will toggle input bool.
Bool on/off will toggle switch.

On HA restart, it’ll set the switch to on/off, whatever the input bool is set. So far so good, hasn’t gotten it wrong.

1 Like

I see you figured it out already.

1 Like

Any better way to do it in 2023?
@reesevader , you created 4 automations? Could you share them here please?

why creating these automations?
“Bool on/off will toggle switch”