I needed a way to save the current state of some switches and lights so I could change them temporarily, and then restore them back to the way they were. Somewhat surprisingly, HA doesn’t seem to have a native feature for this. (If I’m wrong, by all means, enlighten me! ) So I searched the forum and found that the common technique was to create various input_boolean, etc. entities and then write various automations and scripts to get the job done. That seemed a bit cumbersome at best.
So, since the State Machine is basically a big, global variable, and can pretty much save whatever, I wrote a python_script that can save, and later restore, the states of switches and lights.
You need to add the following to your configuration.yaml file if you don’t have it already:
See https://www.home-assistant.io/components/python_script/ for more details.
The script accepts the following service data (i.e., parameters):
store_name - (Optional) The “domain” name to use for the entities created in the State Machine to hold the saved switch/light states and attributes. The default is
entity_id - (Optional) The entity_id(s) of the switch/light entities to save/restore. (Currently only entities of the switch, light or group components are supported. Groups will be expanded recursively into the individual entities they contain.) Can be a single entity_id, a list of entity_id’s, or a string containing a comma separated list of entity_id’s. When saving, the default is to save all existing switch and light entities. When restoring, the default is to restore all previously saved entities.
operation - (Optional) If supplied, must be
restore. The default is
When saving, the script will create an entity in the State Machine named
<store_name>.<component>_<entity> for each saved entity. The state will be the state of the entity, and if the entity is a light, and if the light has brightness and/or hs_color attributes, those will be saved as well. (Note that xy_color and rgb_color are translated to hs_color inside the light component, so only hs_color needs to be saved/restored.)
So, e.g., if store_name is light_store, and switch.abc and light.def are saved, and switch.abc is currently off, and light.def is currently on with a brightness of 128, the following (temporary) entities would be created in the State Machine:
After restoring the temporary entities will be removed from the State Machine.
Here is an example of how you might use it:
script: lights_on: alias: Turn on a given selection of lights, saving current state sequence: - service: python_script.light_store data: store_name: flash_store entity_id: - switch.kitchen_light - light.family_room_lamp - group.upstairs_lights - service: homeassistant.turn_on ... restore_lights: alias: Restore saved lights to the way they were sequence: - service: python_script.light_store data: store_name: flash_store operation: restore
One caveat: If you have any light groups (i.e., an entity of the light component that is actually a group of other lights), then you should not use the default for
entity_id (when saving.) Rather, you should specify the list of individual light entity_id’s. Unfortunately, a python_script cannot tell the difference between a light entity that is an actual light and a light entity that is a group of other lights. If both are saved & later restored, the result will not be what you want.
I would appreciate any comments, criticisms, suggestions or feedback of any kind.
You can obtain the code here:
Place it here:
<config> is your Home Assistant configuration directory.