Save and restore the state of a switch in a script


Some weeks ago Taras help me, how I can save and restore brightness of a light in a script:

Now I’ve tried to save and restore the on-off state of a switch, but it hasn’t worked. How can I save (at the beginning of the script) and restore (at the end of the script) the state of a switch?

My other question:
This switch is connected to a traditional light bulb. Can I handle the switch like a light entity?

Use scenes. Create a scene with the current state then recall it at the end

Thank you,

It’s perfect!

Just for interest, how can it solve with input.boolean?

And Is it possible, that I handle the switch like a light entity (naturally, without brightness, etc…)

You’d have to have an input_boolean for every light that you have, then use templates to set the state of the input_boolean that matches your light.

If you’re talking about using a turn_on/turn_off service that will turn things on/off, use homeassitant.turn_on or homeassistant.turn_off.