Cannot get snapshot_entities to work

Hi all,

normally, this light is set to a nice blue color. But in case my dryer is ready, it should flash in red a couple of times. So flashing is not a problem, but falling back to the blue color does not work.

What am I missing?

Thanks a lot,
Fridolin

My script:

light1_blink_red:
  alias: Blink red
  mode: single
  sequence:
    - service: scene.create
      data:
        scene_id: before
        snapshot_entities:
          - light.lidl_light1
    - repeat:
        while:
          - condition: template
            value_template: "{{ repeat.index <= 6 }}"
        sequence:
          - service: light.toggle
            data_template:
              entity_id: light.lidl_light1
              color_name: red
          - delay: 1
    - service: scene.turn_on
      target:
        entity_id: scene.before

My code got a little more complex, but I’m still not a single step further.

Better said, in case I execute the script and the light has been switched on before, it works nicely, although the logs are not confirming that.
On the other hand, if the light is off before, it switches on, turns from blue to red and returns to off, but the color remains red.

light1_flash_red:
  alias: flash red
  mode: single
  sequence:
    - service: scene.create
      data:
        scene_id: before
        snapshot_entities:
          - light.light_bulb
    - repeat:
        while:
          - condition: template
            value_template: "{{ repeat.index <= 6 }}"
        sequence:
          - service: light.toggle
            data_template:
              entity_id: light.light_bulb
              color_name: red
          - service: notify.tplogger
            data:
              message: In loop {{state_attr('light.light_bulb','color')}}
          - delay: 1
    - service: notify.tplogger
      data:
        message: After loop {{state_attr('light.light_bulb','color')}}
    - service: script.turn_on
      entity_id: script.restore_my_scene

#    - service: scene.turn_on
#      target:
#        entity_id: scene.before
restore_my_scene:
  alias: Restore state
  mode: single
  sequence:
    - service: notify.tplogger
      data:
        message: Before restore {{state_attr('light.light_bulb','color')}}
    - service: scene.turn_on
      target:
        entity_id: scene.before
    - service: notify.tplogger
      data:
        message: After restore {{state_attr('light.light_bulb','color')}}

If I execute the script having the light on and the chosen color is blue, the following lines are logged:

In loop {'x': 0.137, 'y': 0.065}
In loop {'x': 0.137, 'y': 0.065}
In loop {'x': 0.7006, 'y': 0.2993}
In loop {'x': 0.7006, 'y': 0.2993}
In loop {'x': 0.7006, 'y': 0.2993}
In loop {'x': 0.7006, 'y': 0.2993}
After loop {'x': 0.7006, 'y': 0.2993}
Before restore {'x': 0.7006, 'y': 0.2993}
After restore {'x': 0.7006, 'y': 0.2993}

I dont unterstand, why in the second line, it is still blue and why it has not been restored in the last line, although the physical light shows me otherwise.

If the script is executed starting having the light off, the following is logged:

In loop {'x': 0.137, 'y': 0.065}
In loop {'x': 0.7006, 'y': 0.2993}
In loop {'x': 0.7006, 'y': 0.2993}
In loop {'x': 0.7006, 'y': 0.2993}
In loop {'x': 0.7006, 'y': 0.2993}
In loop {'x': 0.7006, 'y': 0.2993}
After loop {'x': 0.7006, 'y': 0.2993}
Before restore {'x': 0.7006, 'y': 0.2993}
After restore {'x': 0.7006, 'y': 0.2993}

Again the second line is wrong and it does not switch back to blue in the last line, but in this case, also the light stays red.

Any ideas? This is now another light bulb, just to rule out that the device might be the one to be blamed. I might remove all that fancy scene stuff and write a lot of manual steps, but this would be like quitting.

Can it be that scenes only work in case the devices are switched on? I mean, in some way, my lights are always on (powered), but I just created another scene, where the light is set to pink. It just works more or less flawlessly, in case the light is switched on and showing a blue color. In case it is off, it goes off while calling the scene ‘before’ and applying the second scene ‘pink’ is not working at all.

So if the scene created to restore the state of a device finds the device switched off, it will create a correct snapshot, but while restoring the state of the device later on, it will fail, because the device is again switched off and attributes cannot be changed anymore? But what sense would this make?

Re starting from off, I’m not sure the scene.create will capture the color. That might depend on the integration piloting your lights. Does the created scene actually contain the color?
Along those lines, if you restore to an “off” state, it’s not obvious that the color data will be applied, as it conceptually make little sense to apply a color to an off state. If we assume that, behind the scene, the toggle will actually run the turn_off service, it definitely doesn’t.

Re logging, HA being asynchronous, you cannot assume that the color attribute will be updated immediately after triggering the service…

It seems like an overly complicated solution, I just simply wrote a script to flash the lights how I want without bothering with scenes. In my case when I am doing color changes that I do not want to persist into the next turn-on, I set the colors back during the cycle just before the turn-off. That’s to say I flash the bulbs red for X times and before turn off it changes the color back to bright white and then turns off. In some cases I have an automation that does it at some point after the flashing sequence ran, say 5 minutes, to turn on the light to white and turn it off again. I just personally prefer to always have it revert back to “everyday use” so that all automations have the same starting point.

Thank you @koying and @CO_4X4 for your feedback - highly appreciated.

I thought it was the opposite of overly complicated, just simply using the existing function to take a snapshot of the current state, then doing stuff and at the end, returning to the previous state.

It seems that the color value is part of the snapshot, because the light returns to blue in case it stays switched on all the time. I would buy the explanation that while switching it off, the color state is not set anymore.

Unfortunately, this makes things more complex. I had in mind to let a couple of lights signaling that the dryer is ready, but that would mean, I need to first figure out, which state they currently have, switched on or not, color, brightness, … and based on that I need to individually restore those values.

Some kind of default state does not make sense for me, just because during the day, those lights are off, but chances are good that the dryer is running and I’d like to be informed, once it is ready. Then, a couple of hours later, it turned dark outside, inside all lights have been switched on and now the second dryer is ready. I cannot let some lights flash and then switch them off, as I might have done during the day.

So I first need to figure out the state, store those values and return to them after everyone in the house has been informed to take care of the dryer :wink:

Thanks a lot,
Fridolin

Actually what would be super cool is if Apple AirTags could be popped into the load of clothes and then target the specific individual whose clothes are done to go get them out of the dryer! Of course it would probably destroy an AirTag but now I want to do this - not because I need to, there are only two of us and she does all the laundry, but because I CAN (maybe)! :smiley:

1 Like

You don’t really need to know the initial state.

If you start from off, the stored color of the light will be red.
Just be sure to set the initial color of your other routines that uses the light.

Personally, my “evening routine” just set a scene, with colors and brighnesses that I expect for the evening. The red would just be overwritten.

For me this is only halfway correct. I have lights turning slowly from faint light to full brightness over time, so there is no given initial state I could restore.

But maybe I could exclude those lights from flashing. I don’t know, whether it’s worth the time to go the extra mile and take care/restore the current state or not.

It could have been so convenient to simply snapshot the current state :wink:

Mmm, but your automation is working if lights are already turned on, doesn’t it?
The only problem is if you start from off, if I got it right.

So, even with your slow brightness automation, if you just set the color at the start, that would solve the problem.

But I might be missing something…

Good point - thanks.

So I would basically need to only decide each time, whether the device is switched on or off. In case it is already in use, I would take the snapshot solution and in case it’s off, it should have a defined state to start with. :+1:

If by “decide”, you mean “test”, not even.

If you lights are initially off, you would be restored to off, which is what you want, although the color of the bulb is now red.
Just be sure to force the colot in your “slow brightness” routine, so that, whether the “alarm” routine has been triggered or not, you don’t get red :wink:

1 Like

I’m facing a similar issue right now, @fridolin could it be that the light that isn’t restoring is a template light?
I have two lights which I snapshot and then turn off when leaving the room, and then restore when I enter the room again. One of the lights (non-template, integrated with Hyperion) works just fine, but my template light doesn’t turn back on.
It looks like neither light.turn_on nor light.set_color is being called.

If scene.turn_on simply replaces the current state of the light without calling any service, I have no clue how to handle this state change for my template light and perform the right actions…

Did you ever get this working?

@Chaphasilor Sorry, but I gave up. The weather outside got better and I needed to take care of other things. The snapshot feature seemed unfinished to me, so I stopped. But now, half a year passed by and it’s getting colder outside. So it might be about time warming this up again. If you’ve figured something out, please don’t hesitate to post it here :wink:

It’s your hardware, the snapshot works perfect and does what it’s supposed to do. I use it everywhere for something similar to this. I would wager that the light is taking time for it to reach the red state, so your second line is in fact true because the light has not fully transitioned to red. Use this instead:

light1_flash_red:
  alias: flash red
  mode: single
  sequence:
    - service: scene.create
      data:
        scene_id: before
        snapshot_entities:
          - light.light_bulb
    - repeat:
        while:
          - condition: template
            value_template: "{{ repeat.index <= 6 }}"
        sequence:
          - service: light.toggle
            data_template:
              entity_id: light.light_bulb
              color_name: red
          - wait_for_trigger:
            - platform: template
              value_template: "{{ state_attr('light.light_bulb','color') == {'x': 0.7006, 'y': 0.2993} }}"
          - service: notify.tplogger
            data:
              message: In loop {{state_attr('light.light_bulb','color')}}
          - delay: 1
    - service: notify.tplogger
      data:
        message: After loop {{state_attr('light.light_bulb','color')}}
    - service: script.turn_on
      entity_id: script.restore_my_scene

This will wait until the light is red before moving on.

EDIT: You might have to adjust the template because I’m not sure if color is the correct attribute anymore. I would assume it would be xy_color, however your original automation did not use that.

1 Like

Hi @petro,

Thanks for adding the ‘wait_for_trigger’ aspect. It took me a while to get back into topic, but after I’ve adapted the script, unfortunately this wait_for only helps to get the log line corrected.

The script adapted like suggested above stops after the first iteration and I guess this is because the light is now red and there is no second trigger event to wait for. Therefore I added the following line to continue either way:

timeout: "00:00:15"

This slows down the execution of the script, but besides the corrected log line, the result is still the same:

  • the light is turned on with the color it had before (better would be to directly blink red and not ‘blue’ the first time)
  • then is switches to red
  • it blinks a couple of times
  • it turns back off

But still the most important point, it does not restore the previous state. The created scene before should contain the state from before, let’s say the light was switched to ‘blue’. But when this script is done, the lights are off and if I turn it on again, it is still red.
The log lines show as well that those values aren’t changed:

In loop {'x': 0.7006, 'y': 0.2993}
After loop {'x': 0.7006, 'y': 0.2993}
Before restore {'x': 0.7006, 'y': 0.2993}
After restore {'x': 0.7006, 'y': 0.2993}

I don’t know, what is happening after calling restore and if it makes sense to add a delay here as well, but from my point either the old state is restored properly or I should be confronted with an error I could work with.

So this is unfortunately the point, where I gave up, because I didn’t know what I should do next. The idea of scenes is pretty nice, but in my system it is not working.

Thanks,
Fridolin

(For me the attribute color works fine and changing it to xy_color does not work. I’m using the most recent version of core 2021.10.6 and OS 6.5)

Your loop isn’t changing colors, you’re only turning on and off. That’s what toggle does and toggle does not accept colors, so the color is ignored. If you want the colors to change, it should be using light.turn_on for all instances, unless you actually want it to turn off. EDIT: Apparently toggle accepts these values now.

I think you have a miss conception about store state, and I now see why you do. When a light is off, it does not have a color associated with it. If you store that state, that’s what’s stored. If you turn the light on, the light will resume to whatever the hardware wants to resume to.

So if you want to have a light blink red. You need to turn the light on. Store the state. Blink the lights red. Restore the state. Turn the light off.

Hi @petro

thanks for your answer, but even after spending a couple of hours to get it done, it still does not work. I accepted what you wrote, but either me, the HA or my devices are simply not smart enough to get the job done. I only want three lights to blink in case an event occurred and then return to the state they have been in before this happend. If they were off, they should go off again but remember the color they had before. If they were on, they should blink and then turn back to the color they had before.

If the HA is only able to read (and store) the state in case the devices are powered on, I changed my script to first figure out, whether the device is on or not and if not, it needs to be turned on before.

Actually, this is a bad solution, because if the light was green before, it would now turn on in green, then switch to red, blink a couple of times, switch back to green and then go off again. And all, just because the light should blink five times in red :frowning:

But either way, I cannot even manage to do this. I understood that it might take some time until the device accepts a new signal, but when I start the initial script from down below, the results are pretty awesome. …but never identical. I can run this script like 10 times and each time something different happens. Sometimes it changes color, but does not blink. Sometimes it remains switched on, sometimes it goes off…a lot of trial&errors later, I don’t even know where I should add more "delay"s. It’s just annoying and even worse, because if I would be sure that the device is to be blamed, I could throw it away and buy something else. But maybe it’s something else or it simply cannot be done like that…

lets_test_conds:
  alias: AAA
  mode: single
  sequence:
    - choose:
        - conditions: "{{is_state('light.e14_col', 'on')}}"
          sequence:
            - service: notify.tplogger
              data:
                message: OnItIs
            - service: script.blink_green_if_on
        - conditions: "{{is_state('light.e14_col', 'off')}}"
          sequence:
            - service: notify.tplogger
              data:
                message: OffItIS
            - service: script.blink_red_if_off

blink_red_if_off:
  alias: 0a
  mode: single
  sequence:
    - service: light.toggle
      data_template:
        entity_id: light.e14_col
    - delay: 1
    - service: scene.create
      data:
        scene_id: before
        snapshot_entities:
          - light.e14_col
    - delay: 1
    - repeat:
        while:
          - condition: template
            value_template: "{{ repeat.index <= 10}}"
        sequence:
          - service: light.toggle
            data_template:
              entity_id: light.e14_col
              color_name: red
          - service: notify.tplogger
            data:
              message: "{{ repeat.index }}"
    - delay: 5
    - service: scene.turn_on
      target:
        entity_id: scene.before
    - delay: 2
    - service: homeassistant.turn_off
      data_template:
        entity_id: light.e14_col

blink_green_if_on:
  alias: 0b
  mode: single
  sequence:
    - service: light.toggle
      data_template:
        entity_id: light.e14_col
    - delay: 1
    - service: scene.create
      data:
        scene_id: before
        snapshot_entities:
          - light.e14_col
    - delay: 1
    - repeat:
        while:
          - condition: template
            value_template: "{{ repeat.index <= 10 }}"
        sequence:
          - service: light.toggle
            data_template:
              entity_id: light.e14_col
              color_name: green
              delay: 1
    - delay: 1
    - service: scene.turn_on
      target:
        entity_id: scene.before
    - delay: 2
    - service: homeassistant.turn_off
      data_template:
        entity_id: light.e14_col

Any updates on this topic? I also got everything working nicely for my whole apartment for all lights. Every state is restored but not the color. The next time I turn on a light, it is red. And when I took the snapshot it was purple but off. It does not work.

When the light is on instead, it works like a charm. Any idea how to fix this?

Edit: I found it is not possible. You can just turn the lights on and snapshot them again before the automation/script. Then after the automation/script you restore this snapshot and afterwards the snapshot where the lights were off.