How to change the color of a lightgroup and after a delay bring it back in previous state

I’ve been looking on the forum and found a script but that is only for one light.
What i want is when my gate opens (trigger) a group of lights has to become red for a while.
After that the previous state of the lights has to be restored.
I made an automation for that but the problem is when the lights were off they are not restored to the previous state.

Use a snapshot scene to restore the lights to their original state.

There’s an example in the documentation:

Creating scenes on the fly

Thx but i tried that.
It restores to the previous state only if the lights were on.
If the lights were off it doesn’t restore to the previous state.

Without any details about what you did exactly, I can’t explain why it doesn’t work for you but I can confirm that it works for me; it restores the light’s original state even if it was off.

This is what i have:
Two automations for detecting the gate opened or closed.
When the light is on and the gate opens the light becomes red as it should be.
Then when i close the gate the light is restored to the previous state and that’s ok.

When the light is off and i open the gate the light becomes red.
When i close the gate the light goes off and also that’s ok.

BUT…when somebody turns the light on after that it becomes red and that’s not ok.
I had that same problem when i used Fibaro HC3 but was able to resolve that.

So what happens is that when the light is off and the gate opens HA does not capture the state of the light.

So i really don’t understand how you get this to work ?

I found a workaround but can’t get it to work.

First i tried all this with a lightgroup but the code here is with only one light.

https://community.home-assistant.io/t/return-light-to-previous-state-after-automation/147516/7

- id: '125'
  alias: "Gate opened"
  trigger:
  - platform: state
    entity_id: binary_sensor.797
    from: "off"
    to: "on"
  condition: []
  action:
  - service: scene.create
    data:
      scene_id: before
      snapshot_entities:
      - light.hue_go_1
  - service: light.turn_on
    target:
      entity_id: light.hue_go_1
    data:
      rgb_color:
        - 255
        - 0
        - 0
      brightness: 255
- id: '126'
  alias: "Gate closed"
  trigger:
  - platform: state
    entity_id: binary_sensor.797
    from: "on"
    to: "off"
  condition: []
  action:
  - service: scene.turn_on
    target:
      entity_id: scene.before

Sorry but I no longer understand the problem you’re attempting to describe because these two statements appear to be contradictory:

As I read it;
It works fine if the lights is already on. Gate opens → Light turns red. Gate closes → Light returns to prior state (White or warm or whatever, but still on.)

His problem appears to be that when the light’s starting state is off and the gate opens.
Gate opens → Light turns on and red
Gate closes → Light turns off but retains the red colour setting, meaning that the next time someone turns on the light, it turns on red.

I’m sorry if i haven’t made myself clear so i will try again.

  • somebody turns the light on by a button or whatever (no automation) and they are yellow like it was in
    the previous state.

  • somebody turns the light off (the previous state is stored, yellow)

  • gate opens and light becomes red

  • gate closes and light goes off.

  • next time somebody turns the light on by a button or whatever the light becomes red and that’s not
    correct. It has to be yellow as captured with snapshot_entity.

Hope i made myself clear now :face_with_hand_over_mouth:

That is exactly what i’m trying to say.

I have a similar automation for my fridge and freezer doors. When left open more than 5 minutes, an automation switches on my kitchen counter lights to red. When closed it returns them to white, but doesn’t switch them off, as I have a separate automation that turns those lights off after 30 minutes of no movement in the kitchen anyway. That might be one bodgy way to solve your problem, if you have any motion sensors. I’m sure there is a neater way to do it, Taras is usually able to steer people in the right direction for these things.

Thanks for the clarification.

That behavior is due to the light’s retention of its previous properties (like color, color temperature, brightness, etc). When turned on it restores its previous properties (this is a feature of the light’s firmware, not Home Assistant).

Example:

  1. Light is off.

  2. Home Assistant has no way of knowing what the light’s previously-used properties are (color, color temperature, etc) other than it’s currently off.

  3. If you create a snapshot scene, the only property known to Home Assistant is that the light is off. That’s the only thing that will be restored when the snapshot scene is applied.

  4. Gate opens, light is turned on and set to red.

  5. The light retains ‘red’ as the color it will use when turned on the next time.

  6. Gate closes, snapshot scene is applied and light is simply turned off. Its color isn’t changed from red to something else because the previous color was unknown to Home Assistant when the snapshot scene was created (because the light was off).

  7. Someone turns on the light and it automatically uses its previous properties which means it displays red.

Correct but in the link i posted somebody found a solution.
So in my case:

  • Gate opens
  • Turn the light on for 250 ms in its previous state(let’s say yellow) and capture the state.
  • Light becomes red after those 250 ms.
  • Gate closes, light goes to previous state, off or yellow.

That works i think but i can’t get the script to work (i’m using HA just for a couple of months)

His script starts with:
alias: Front Light Siren
In that way the script doesn’t show up in the UI and i can’t turn it on.
I made a small test script and that shows up in the UI.
In Studio code server it starts with the name i gave it,Testscript:
and i can turn it on.

Thanks for your time both of you @Sidioussam and @123

Turn on the light (without specifying any other properties) before creating the snapshot scene. However, do this only if the light’s current state is off.

- id: '125'
  alias: "Gate opened"
  trigger:
  - platform: state
    entity_id: binary_sensor.797
    from: "off"
    to: "on"
  condition: []
  action:
  - if: "{{ is_state('light.hue_go_1', 'off') }}"
    then:
      - service: light.turn_on
        target:
          entity_id: light.hue_go_1
      - delay:
           milliseconds: 250
  - service: scene.create
    data:
      scene_id: before
      snapshot_entities:
      - light.hue_go_1
  - service: light.turn_on
    target:
      entity_id: light.hue_go_1
    data:
      rgb_color:
        - 255
        - 0
        - 0
      brightness: 255

This does not turn the light off so i a couple lines of code but it doesn’t turn off either.

`- id: '125'
  alias: "Gate opened"
  trigger:
  - platform: state
    entity_id: binary_sensor.797
    from: "off"
    to: "on"
  condition: []
  action:
  - if: "{{ is_state('light.hue_go_1', 'off') }}"
    then:
      - service: light.turn_on
        target:
          entity_id: light.hue_go_1
      - delay:
          milliseconds: 250
      - service: scene.create
        data:
          scene_id: before
          snapshot_entities:
          - light.hue_go_1
    else:
      - service: scene.create
        data:
          scene_id: before
          snapshot_entities:
          - light.hue_go_1
  - service: light.turn_on
    target:
      entity_id: light.hue_go_1
    data:
      rgb_color:
        - 255
        - 0
        - 0
      brightness: 255`

Correct. There’s more that needs to be done than what I provided in my previous example (which was only meant to demonstrate a portion of the technique you described from the other topic). My example only showed how to capture the properties of a light that was previously off.

What remains to be done is that after scene.before is applied, the light must also be turned off (otherwise it will be left on). However, this extra step of turning off the light must only be done if the light was previously off (obviously you don’t want to turn it off it was previously on).

The tricky part is knowing when to perform the extra step of turning off the light and when not to. One way to do this is to make the first automation turn on an Input Boolean whenever it encounters light.hue_go_1 is off. The second automation checks the state of the Input Boolean and, if it’s on, performs the extra step of turning off the light.

- id: '125'
  alias: "Gate opened"
  trigger:
  - platform: state
    entity_id: binary_sensor.797
    from: "off"
    to: "on"
  condition: []
  action:
  - if: "{{ is_state('light.hue_go_1', 'off') }}"
    then:
      - service: input_boolean.turn_on
        target:
          entity_id: input_boolean.light_off
      - service: light.turn_on
        target:
          entity_id: light.hue_go_1
      - delay:
           milliseconds: 250
  - service: scene.create
    data:
      scene_id: before
      snapshot_entities:
      - light.hue_go_1
  - service: light.turn_on
    target:
      entity_id: light.hue_go_1
    data:
      rgb_color:
        - 255
        - 0
        - 0
      brightness: 255
- id: '126'
  alias: "Gate closed"
  trigger:
  - platform: state
    entity_id: binary_sensor.797
    from: "on"
    to: "off"
  condition: []
  action:
  - service: scene.turn_on
    target:
      entity_id: scene.before
  - if: "{{ is_state('input_boolean.light_off', 'on') }}"
    then:
      - delay:
           milliseconds: 250
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.light_off
      - service: light.turn_off
        target:
          entity_id: light.hue_go_1
1 Like

Thank you so much for all your effort but the light stays on.

Update : I’m sorry but i forgot to create the helper input_boolean.light_off

And now…every works fine !!
Thank you sooooo much for your effort and time :smiley:

And not to forget, i learned a lot today. :smiley:
I was thinking when the light is off, that state is also captured by the snapshot_entity.

1 Like

I ran into a little issue…
I was trying to make that automation for a lightgroup but i didn’t want to annoy my wife and kids with turning off and on the kitchenlights for hours.
So i tried it with one light in my office.

When you gave me the solution i changed the code back to the kitchenlights and guess what ?
It didn’t work anymore.

I had a suspicion that the 250 ms delay was not enough to capture the state of four lights instead of one.
And indeed, setting the delay to 500 ms made it work again :wink:

Suddenly it didn’t work anymore.
I’m not giving up so i changed the code to one of the four kitchenlights instead of the group (the four kitchenlights).
Then something strange happened.
When i opened the gate all four of the lights became red.
For some reason the snapshot_entity is not updated or created again.

I removed all the code that creates the snapshot_entities.
In the automation that closes the gate i put an existing scene that was imported by the Hue-integration en that did the trick. (for now :pray:)

Using a native Hue scene is optimal because Home Assistant only needs to send one command and all four lights know what they need to do (because they know the details).

In contrast, a Home Assistant scene sends commands to each one of the four lights. There’s more communication involved therefore it takes longer for all four lights to receive and comply with the commands (and for Home Assistant to receive acknowledgement when all four lights have complied).

But we added a delay ?
I even tried it with four seconds.