Scene snapshot with lights and restore the previous state

Hey,

I am trying to setup an cleaning mode where I press a button and it turns on all the lights in the house and set brightness up. But when I turn the cleaning mode off, I would want to all the lights go back to the way they were.

I am now testing this with one light and creating a scene in an automation. Currently I need to turn on the light as I do not know whether it has been on or not, then take a snapshot of it and then continue on setting the brightness and this works well. I just need to add a small delay before taking the snapshot so the data is updated. I can then restore this when I turn the swtich to off and it works well.

But the problem is that I would not want all the lights in the house to stay ON unless they were on previously. But if I do not turn the light on before taking a snapshot, then the brightness etc are not recorded and the light will turn off but next time I turn it on, it will be full blast.

Has anyone figured out any way to handle this? Some workaround maybe?

Hi Johan,

Will this help you at all?
automation2/Smoke_A2.yaml.

I had to think about this for a while and tried several things but finally figured out an easy way to do this. The short explanation is have your automation create a scene based on the current snapshot. Follow that with an action that turns on all of the lights but does not adjust their brightness. Now create a second scene. After that is your action that turns all of the lights to full brightness.

So now when you turn off your cleaning mode you will activate the second scene first which will set the prior brightness level for all of the lights. Then you activate the first scene which will set their prior on/off state.

I tried it using this automation and it works.

alias: "Cleaning Mode"
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.cleaning_mode # The toggle helper for cleaning mode
    from: "off"
    to: "on"
    id: "on"
  - platform: state
    entity_id:
      - input_boolean.cleaning_mode # The toggle helper for cleaning mode
    from: "on"
    to: "off"
    id: "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "on"
        sequence:
          - action: scene.create
            metadata: {}
            data:
              scene_id: light_state_before_cleaning_001
              snapshot_entities:
                - light.bcl_local # Add all of your light entities
                - light.kcl_local
                - light.lrcl_local
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id:
                - light.bcl_local # Add all of your light entities
                - light.kcl_all_local
                - light.lrcl_local
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - action: scene.create
            metadata: {}
            data:
              scene_id: light_state_before_cleaning_002
              snapshot_entities:
                - light.bcl_local # Add all of your light entities
                - light.kcl_local
                - light.lrcl_local
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
            target:
              entity_id:
                - light.bcl_local # Add all of your light entities
                - light.kcl_all_local
                - light.lrcl_local
      - conditions:
          - condition: trigger
            id:
              - "off"
        sequence:
          - action: scene.turn_on
            metadata: {}
            target:
              entity_id: scene.light_state_before_cleaning_002
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - action: scene.turn_on
            metadata: {}
            target:
              entity_id: scene.light_state_before_cleaning_001
mode: single

1 Like

I think this is the solution :slightly_smiling_face:

1 Like

This integration is quite useful to manage scenes. https://github.com/hugobloem/stateful_scenes

thank you. This helped me out as well!

For anyone also visiting this topic:
I wanted to avoid having to hardcode the light entities.
This script uses light groups instead:

alias: cleaning mode bathroom
description: ""
triggers:
  - entity_id: input_boolean.cleaning_mode_bathroom
    from: "off"
    to: "on"
    id: "on"
    trigger: state
  - entity_id: input_boolean.cleaning_mode_bathroom
    from: "on"
    to: "off"
    id: "off"
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: "on"
        sequence:
          - variables:
              bathroom_lights: |
                {{ expand('light.lights_bathroom')
                   | map(attribute='entity_id')
                   | list }}
          - data:
              scene_id: light_state_before_cleaning_001
              snapshot_entities: "{{ bathroom_lights }}"
            action: scene.create
          - delay: "00:00:02"
          - target:
              entity_id: "{{ bathroom_lights }}"
            action: light.turn_on
          - delay: "00:00:02"
          - data:
              scene_id: light_state_before_cleaning_002
              snapshot_entities: "{{ bathroom_lights }}"
            action: scene.create
          - data:
              brightness_pct: 100
            target:
              entity_id: "{{ bathroom_lights }}"
            action: light.turn_on
      - conditions:
          - condition: trigger
            id: "off"
        sequence:
          - target:
              entity_id: scene.light_state_before_cleaning_002
            action: scene.turn_on
          - delay: "00:00:02"
          - target:
              entity_id: scene.light_state_before_cleaning_001
            action: scene.turn_on
mode: single

Instead of addressing the light group directly, it splits it into single entities. Otherwise it will address the aggregate of the group and won’t return to the intital state after turning cleaning mode off.