Scene with template values

I’m trying to create a scene on the fly using scene.create in a script so I can save the state my lights are in, flash a color, and then have them return to their previous state. I’m using templates in scene creation to save the current value.

notify_blue:
  alias: Notify blue
  sequence:
  - service: scene.create
    data:
      scene_id: temp_notify_lights
      entities:
        light.lr_floor_lamp:
          state: '{{ states("light.lr_floor_lamp") }}'
          brightness: '{{ state_attr("light.lr_floor_lamp", "brightness") }}'
          rgb_color: '{{ state_attr("light.lr_floor_lamp", "rgb_color") }}'
  - service: light.turn_on
    entity_id: light.notify_lights
    data:
      rgb_color:
      - 0
      - 0
      - 255
  - delay:
      milliseconds: 250
  - service: light.turn_off
    entity_id: light.notify_lights
  - delay: 
      milliseconds: 250
  - service: scene.turn_on
    entity_id: temp_notify_lights

I suspect the scene is getting created with the template text (e.g. '{{ state_attr("light.lr_floor_lamp", "brightness") }}'') instead of with the concrete values the template resolves to when I create the scene (e.g. 254). If that’s the case the template will resolve when I try to apply the scene which will apply the state the lights are currently in which isn’t very helpful.

I’ve been trying to verify this but it seems temp scenes are only saved in memory and not in scenes.yaml. I can see the created scenes in the scenes GUI but the edit button is grayed out since it’s a temp scene.

Any ideas on how to achieve this or check how temp scenes are getting saved?

It’s my understanding that scenes do not support templates.

I searched around quite a lot and somehow didn’t come across that post. Thanks for the link though, hopefully creating scenes based on current states will be an option at some point in the future.

@ joelspiers15
If your color lamps happen to be LIFX, here is a simple script that does the trick. Not sure if it will cross over to other RBG bulbs, but maybe it will help you.

blink_lights_tv:
  alias: 'Incoming Notification'
  sequence:
    - service: light.lifx_effect_pulse
      data:
        entity_id: light.tv_left, light.tv_right
        rgb_color: [255,255,255]
        brightness: 128
        period: 0.2
        cycles: 3
        mode: blink
        power_on: False

They’re hue so that won’t work. There is a flash option for Hue which is supposed to do that but it doesn’t actually return the lights to their previous state

I think I read a comment in GitHub about a proposed ability to capture the current state of entities. So you can take a snapshot of the current states, apply a scene, then revert back to the snapshot (effectively “undo-ing” the scene).

FWIW, the home automation software I’ve used for years has scenes with two possible behaviors:
set
set-restore

The set behavior is like how Home Assistant’s scenes work. Turn on the scene and done; there’s no way to revert to the previous states.

The set-restore behavior lets you turn on a scene and then, if you wish, turn off the scene so that all entities are automatically restored to their previous state. Under the hood, this kind of scene automatically takes a snapshot of all entity states so it knows how to put them back afterwards. This allows you to layer one scene atop another and easily be able to peel them off, one by one.

The classic example of this is you have the Movie scene running and then someone rings the doorbell. This activates the Doorbell scene that pauses your movie and displays the entry camera on the TV, brightens the room lights, and turns on the hallway light. After you paid for the pizza, hit play and that turns off the Doorbell scene and automagically restores the room to its previous state (no door camera on screen, lights dim, etc).

Have you already tried this?

action:
  - service: scene.create
    data:
      scene_id: before_something
      snapshot_entities:
      - light.1
      - light.2
      - ...

and then:

action:
  - service: scene.turn_on
    data:
      entity_id: scene.before_something