I was looking for the same functionality and came up with a way to do it via Python Scripts (i.e., https://www.home-assistant.io/components/python_script/.) I’m still pretty new to HA, so this may or may not be an optimal or “proper” way, but it does seem to work. Any comments/feedback are welcome.
In my case I wanted to save the current state of all switches/lights, turn them all on for a period of time, then restore them all to their previous state. Here are the two scripts I came up with:
To save the current state (which I put in ~homeassistant/.homeassistant/python_scripts/save_lights.py):
DOMAIN = 'light_store'
STORE_ENTITY_ID = '{}.{{}}'.format(DOMAIN)
saved = hass.states.entity_ids(DOMAIN)
for entity_id in saved:
hass.states.remove(entity_id)
all_switches = hass.states.entity_ids('switch')
all_lights = hass.states.entity_ids('light')
for entity_id in all_switches:
cur_state = hass.states.get(entity_id)
if cur_state is None:
logger.error('Could not get current state for {}.'.format(entity_id))
else:
hass.states.set(STORE_ENTITY_ID.format(entity_id.replace('.', '_')),
cur_state.state)
for entity_id in all_lights:
cur_state = hass.states.get(entity_id)
if cur_state is None:
logger.error('Could not get current state for {}.'.format(entity_id))
else:
attributes = {}
brightness = cur_state.attributes.get('brightness')
if brightness is not None:
attributes['brightness'] = brightness
hass.states.set(STORE_ENTITY_ID.format(entity_id.replace('.', '_')),
cur_state.state, attributes)
And to restore the previous states (~homeassistant/.homeassistant/restore_lights.py):
DOMAIN = 'light_store'
STORE_ENTITY_ID = '{}.{{}}'.format(DOMAIN)
saved = hass.states.entity_ids(DOMAIN)
all_switches = hass.states.entity_ids('switch')
all_lights = hass.states.entity_ids('light')
for entity_id in all_switches:
old_state = hass.states.get(STORE_ENTITY_ID.format(entity_id.replace('.', '_')))
if old_state is None:
logger.error('No saved state for {}.'.format(entity_id))
else:
turn_on = old_state.state == 'on'
service_data = {'entity_id': entity_id}
hass.services.call('switch', 'turn_on' if turn_on else 'turn_off',
service_data)
for entity_id in all_lights:
old_state = hass.states.get(STORE_ENTITY_ID.format(entity_id.replace('.', '_')))
if old_state is None:
logger.error('No saved state for {}.'.format(entity_id))
else:
turn_on = old_state.state == 'on'
service_data = {'entity_id': entity_id}
brightness = old_state.attributes.get('brightness')
if turn_on and brightness is not None:
service_data['brightness'] = brightness
hass.services.call('light', 'turn_on' if turn_on else 'turn_off',
service_data)
for entity_id in saved:
hass.states.remove(entity_id)
The state is saved to new entities named ‘light_store.xxx’ where xxx is the entity ID of the switch or light being saved (with dots replaced with underscores). E.g.:
These entities are removed after using them to restore the switches/lights back to their previous states.
Although these scripts as written save/restore all switches and lights, I think it would be very easy to enhance to take a list of entity IDs to operate on instead. Also, I suppose a unique name could be specified for the “light store” if that was needed.
Comments/thoughts? Is it reasonable to dynamically create and destroy entities for temporarily saving state?