I am using scene.create
and snapshot_entities
to save the state of some entities so I can change them and restore them later. I created a script to do this, which gets passed the entities to save with snapshot_entities
and a name to give the new scene created by scene.create
. This works fine and the scene is created and can be recalled manually. The problem is, when I go to call the scene later in the automation, I get an error (in the UI it says the scene does not exist, and if I do it in YAML the automation disappears from the UI which I think means there is an error). I have seen so many examples of doing this so I am not sure what I am doing wrong.
automations.yaml
- id: '1678941674157'
alias: Scene - Movie Mode
description: Toggle movie mode on or off, save and restore light states
trigger:
- platform: state
entity_id:
- input_boolean.movie_mode_toggle
from: 'off'
to: 'on'
id: mm-on
- platform: state
entity_id:
- input_boolean.movie_mode_toggle
from: 'on'
to: 'off'
id: mm-off
condition: []
action:
- choose:
- conditions:
- condition: trigger
id: mm-on
sequence:
- service: script.save_light_states
data:
snapshot_name: movie-mode-before
lights_to_save:
- light.living_room_lights
- light.dreamview
- service: light.turn_on
target:
entity_id: light.dreamview
- service: scene.turn_on
target:
entity_id: scene.living_room_dim
- service: light.turn_off
target:
entity_id:
- light.kitchen_light
- light.patio_door_lights
- conditions:
- condition: trigger
id: mm-off
sequence:
- service: scene.turn_on
target:
entity_id: scene.movie-mode-before #this line is the problem
mode: single
scripts.yaml
save_light_states:
alias: Save snapshot of given lights
description: Save passed light states to scene of passed name using snapshot_entities
variables:
snapshot_name: "{{ snapshot_name }}"
light_list: "{{ lights_to_save }}"
fields:
snapshot_name:
name: Snapshot Name
required: true
selector:
text:
lights_to_save:
name: Lights to Save
required: true
selector:
entity:
multiple: true
sequence:
- service: scene.create
data:
scene_id: "{{ snapshot_name }}"
snapshot_entities:
"{{expand(light_list) | selectattr('entity_id', 'defined')
| map(attribute='entity_id') | list}}"
mode: single