Return light to previous state?

Hi, when I activate my button
( input_boolean.tema_romantico ) I would like automation to start taking a snapshot of the following lights:
light.yeelight_colorc_0x1b0ffc9a
light.yeelight_colorc_0x18906fb6
light.yeelight_colorc_0x189042ff
light.faretti_camera_da_bed
light.suprema_smart_controller_rgbww_4
to then start a script already created that changes the brightness and color of the lights in question. When the input_boolean.tema_romantico is turned off I would like it to load the snapshot made previously.

I tried to search the internet for how to do it, but I only found this piece of code that could help me but I don’t know how to complete it:

  alias: Camera da letto snapshot
  description: ''
  trigger:
  - entity_id: input_boolean.tema_romantico
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      snapshot_entities: light.yeelight_colorc_0x1b0ffc9a light.yeelight_colorc_0x18906fb6
        light.yeelight_colorc_0x189042ff light.faretti_camera_da_letto light.suprema_smart_controller_rgbww_4
    service: scene.create
  - service: script.1643407098523
  - delay: 00:00:04
2 Likes

There’s an example in the Scene Documentation.

https://www.home-assistant.io/integrations/scene/#creating-scenes-on-the-fly

When the input_boolean is turned on, the automation creates a snapshot scene of the five lights, named scene.lights_before, and then executes your script.

When the input_boolean is turned off, the automation turns on scene.lights_before thereby restoring the previous state of the five lights.

  alias: Camera da letto snapshot
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.tema_romantico
  condition: []
  action:
  - choose:
    - conditions: "{{ trigger.to_state.state == 'on' }}"
      sequence:
      - service: scene.create
        data:
          scene_id: lights_before
          snapshot_entities:
          - light.yeelight_colorc_0x1b0ffc9a
          - light.yeelight_colorc_0x18906fb6
          - light.yeelight_colorc_0x189042ff
          - light.faretti_camera_da_letto
          - light.suprema_smart_controller_rgbww_4
      - service: script.1643407098523
    default:
    - service: scene.turn_on
      target:
        entity_id: scene.lights_before

EDIT

Correction. Replace template’s outer single-quotes with double-quotes.

5 Likes

When I enter " ‘{{ trigger.to_state.state == ‘on’ }}’ " in automations.yaml show me an error, says:

That’s due a small but significant error I made in the original example. Replace the template’s outer single-quotes with double-quotes (I have already corrected the example posted above).

Change this:

'{{ trigger.to_state.state == 'on' }}'

to this:

"{{ trigger.to_state.state == 'on' }}"

The rule is that the template’s outer quotes must not be identical to its inner quotes.

For some strange reason it doesn’t work, I keep putting the input_boolean on but nothing …

What’s wrong?

configuration.yaml

input_boolean:
  tema_romantico:
    name: pulsante tema romantico
    initial: off
    icon: mdi:toggle-switch-outline

automations.yaml

  alias: Snapshot per camera da letto
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.tema_romantico
  condition: []
  action:
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.state == ''on'' }}'
      sequence:
      - service: scene.create
        data:
          snapshot_entities:
          - light.yeelight_colorc_0x1b0ffc9a
          - light.yeelight_colorc_0x18906fb6
          - light.yeelight_colorc_0x189042ff
          - light.faretti_camera_da_letto
          - light.suprema_smart_controller_rgbww_4
      - service: script.1643407098523
      - service: light.turn_on
        target:
          entity_id: light.yeelight_colorc_0x1b0ffc9a
        data:
          color_name: pink
    default:
    - service: scene.turn_on
      target:
        entity_id: scene.lights_before
  mode: single

scripts.yaml

  alias: Romantico
  sequence:
  - service: light.turn_on
    data:
      brightness_pct: 44
      color_name: purple
    target:
      entity_id:
      - light.yeelight_colorc_0x1b0ffc9a
  - service: light.turn_on
    target:
      entity_id: light.suprema_smart_controller_rgbww_4
    data:
      brightness_pct: 35
      color_name: red
  - service: light.turn_off
    target:
      entity_id: light.faretti_camera_da_letto
  - service: light.turn_on
    target:
      entity_id:
      - light.yeelight_colorc_0x189042ff
      - light.yeelight_colorc_0x18906fb6
    data:
      color_name: red
      brightness_pct: 33
  mode: single

If you notice the automation is slightly different because when I opened it in the graphical interface it always appeared to me “Save”, clicking on it added this line of code:

- conditions:
      - condition: template
        value_template: '{{ trigger.to_state.state == ''on'' }}'

This is your line:

- conditions: "{{ trigger.to_state.state == 'on' }}"

obviously I tried them both but it doesn’t work anyway :frowning:

Your version is missing this line:

          scene_id: lights_before

in the service call to create the scene:

      - service: scene.create
        data:
          scene_id: lights_before
          snapshot_entities:
          - light.yeelight_colorc_0x1b0ffc9a
          - light.yeelight_colorc_0x18906fb6
          - light.yeelight_colorc_0x189042ff
          - light.faretti_camera_da_letto
          - light.suprema_smart_controller_rgbww_4
1 Like

Thanks a lot, it worked! sorry if I thank you late but I have been away in this week :smile:

1 Like

This works so well and is really simple and temporary, Thanks a lot!

1 Like