Scene before and after on flasing light not working. Little Help!

Super simple script.
I try to copy current states of a light_bulb_1, then make it flash with red coulor, then set the before state to get it how it was.
I keep getting the the light on red after the script.

sequence:
  - service: scene.create
    data:
      scene_id: the_before_time
      snapshot_entities: light.light_bulb_2
  - repeat:
      count: '6'
      sequence:
        - service: light.toggle
          data:
            color_name: red
            brightness_pct: 100
          target:
            entity_id: light.light_bulb_2
        - delay:
            hours: 0
            minutes: 0
            seconds: 1
            milliseconds: 0
  - service: scene.turn_on
    data:
      transition: 1
    target:
      entity_id: scene.the_before_time
mode: single
alias: Flashing ligth

On the “Show Trace” i get this:

1


3

What does light.light_bulb_2 look like in developer tools before running this script? Does it actually have color details in its attributes or is there just no color information (and thus its defaulting to white)?

The way scene snapshotting actually works is it captures the exact state of the entities you snapshot and then puts them back in that state afterwards. But it can only capture what is listed. So if your light entity does not have any color information in its state beforehand then no color information will be captured and that won’t be changed afterwards.

It has this attributes:

min_mireds: 153
max_mireds: 500
supported_color_modes:
  - color_temp
  - xy
friendly_name: ligth_bulb_3
supported_features: 59

I think I understand what you mean. I´m sure I can set it colour to red, and also all kinds of whites…

I´m very new to all this HA world and the learnig curve is a B.

Any susgestion?

So I’ve seen this problem before on the forum but since I don’t actually have any lights that support color myself I’ve never had to solve it myself. The only thing that comes to mind is using entities instead of snapshot_entities in scene.create so you can be sure the attributes you need are captured. Something like this:

service: scene.create
data:
  scene_id: the before_time
  entities:
    light.light_bulb_2:
      state: "{{ states('light.light_bulb_2') }}"
      brightness: "{{ state_attr('light.light_bulb_2', 'brightness') | int(0) }}"
      color_mode: "xy"
      xy_color: "{{ state_attr('light.light_bulb_2', 'xy_color') | default([0, 0]) }}"
      ...

I’m not sure if I got those attribute names right I just saw that your light supports xy color mode so I took my best guess. But hopefully you get what I mean. Basically pull the attributes from your light and use the default filter to provide alternative values when they are missing.

I’m not sure if this is the best way, perhaps others that have lights with colors have a better solution.

ufff all that you just say is like chineese to me. I’ll try to look it up.

So for my approach basically do this:

  1. Turn your light fully off
  2. Go to developer tools, capture the attributes of the light
  3. Turn your light red (or some other color)
  4. Go to developer tools, capture the attributes of the light
  5. Compare the two, see what changed
  6. For each attribute that changed, add them to the scene.create call like this:
service: scene.create
data:
  scene_id: the before_time
  entities:
    light.light_bulb_2:
      state: "{{ states('light.light_bulb_2') }}"
      changed_attribute_1: "{{ state_attr('light.light_bulb_2', 'changed_attribute_1') | default(<sensible default>) }}"
      changed_attribute_2: "{{ state_attr('light.light_bulb_2', 'changed_attribute_2') | default(<sensible default>) }}"
      ...

That’s basically it. You’re trying to figure out all the attributes that are missing when the light is off and make sure that when you restore your scene you set every relevant attribute of the light, not just what happened to exist at the time the scene was captured. You’ll have to figure out what “sensible default” is for each attribute either by looking up the attribute in doc or playing around with it in the services tab until you figure it out.

A little more info. Perhaps it´s needed.

1.- I use zigbee2mqtt
2.- The code works as it sopose but only when in the before time state the light was on.

This is the new code:

sequence:
  - service: scene.create
    data:
      scene_id: the_before_time
      snapshot_entities: light.light_bulb_2
  - repeat:
      count: '4'
      sequence:
        - service: light.toggle
          data:
            xy_color:
              - 0.701
              - 0.299
            brightness: 255
          target:
            entity_id: light.light_bulb_2
        - delay:
            hours: 0
            minutes: 0
            seconds: 1
            milliseconds: 0
  - service: scene.turn_on
    data: {}
    target:
      entity_id: scene.the_before_time
mode: single
alias: Flashing ligth