Just create a "Save State" scene if the lights are on

Hi all,
I am trying to create a light notification when someone presses the doorbell. The automation should save the current state of the light, flash it light 2 times and finally reload the initial state.

I wrote this automation:

- id: '1623940987417'
  alias: Flash Office Floor Lamp lights red when doorbell is pressed
  description: Flash Office Floor Lamp lights red 2 times when doorbell is pressed
  trigger:
  - platform: device
    device_id: zzzzzzzzzzzzzzzzzzzzzzzzzzzz
    domain: nest
    type: doorbell_chime
  condition: []
  action:
  - data:
      scene_id: save_state_office_floor_lamp
      snapshot_entities: light.office_floor_lamp_level_light_color_on_off
    service: scene.create
  - scene: scene.office_floor_lamp_red
  - repeat:
      count: '4'
      sequence:
      - type: toggle
        device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        entity_id: light.office_floor_lamp_level_light_color_on_off
        domain: light
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 500
  - data: {}
    service: scene.turn_on
    target:
      entity_id:
      - scene.save_state_office_floor_lamp
  mode: single

The only problem that I am facing with it is that if the light is off, my ā€œsave stateā€ creating a scene doesnā€™t work, as it will create a blank scene and when turn the lamp on ti will be set to the notification color and brightness (red, 100%).

Is it possible to only create the scene if the lamp is on using automation or do I need to learn how to do templates and scripts?

What about adding condition that only when said light is on

As Stephan said, just include a condition for the light being ā€˜onā€™.

I was trying to solve everything in just one automation. However, I got the feeling that the automation guideline is to have multiple simpler ones, for a single use case, than having just a complex one with multiple tests in it.

I ended up duplicating the automation and adding a condition check for light on in one and light of in the other.

Automation when the light is already on:

- id: 'zzzzzzzzzzz1'
  alias: Flash Office Floor Lamp lights red when the doorbell is pressed (Light On)
  description: Flash Office Floor Lamp lights red 2 times when the doorbell is pressed.
    This will save the state of the light that is on.
  trigger:
  - platform: device
    device_id: xxxxxxxxxxxxx
    domain: nest
    type: doorbell_chime
  condition:
  - condition: device
    type: is_on
    device_id: yyyyyyyyyyyyy
    entity_id: light.office_floor_lamp_level_light_color_on_off
    domain: light
  action:
  - data:
      scene_id: save_state_office_floor_lamp
      snapshot_entities: light.office_floor_lamp_level_light_color_on_off
    service: scene.create
  - scene: scene.office_floor_lamp_red
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 750
  - repeat:
      count: '4'
      sequence:
      - type: toggle
        device_id: yyyyyyyyyyyyy
        entity_id: light.office_floor_lamp_level_light_color_on_off
        domain: light
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 500
  - data: {}
    service: scene.turn_on
    target:
      entity_id:
      - scene.save_state_office_floor_lamp
  mode: single

Automation when the light is off:

- id: 'zzzzzzzzzzz2'
  alias: Flash Office Floor Lamp lights red when the doorbell is pressed (Light Off)
  description: Flash Office Floor Lamp lights red 2 times when the doorbell is pressed.
    This doesn't save the last state as the light is off and reused the previously saved scene.
  trigger:
  - platform: device
    device_id: xxxxxxxxxxxxx
    domain: nest
    type: doorbell_chime
  condition:
  - condition: device
    type: is_off
    device_id: yyyyyyyyyyyyy
    entity_id: light.office_floor_lamp_level_light_color_on_off
    domain: light
  action:
  - scene: scene.office_floor_lamp_red
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 750
  - repeat:
      count: '04'
      sequence:
      - type: toggle
        device_id: yyyyyyyyyyyyy
        entity_id: light.office_floor_lamp_level_light_color_on_off
        domain: light
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 500
  - data: {}
    service: scene.turn_on
    target:
      entity_id:
      - scene.save_state_office_floor_lamp
  - type: turn_off
    device_id: yyyyyyyyyyyyy
    entity_id: light.office_floor_lamp_level_light_color_on_off
    domain: light
  mode: single

Why not turn the condition into a choose in the action part of it you only need one.

:sweat_smile: Because I spend hours trying to make it work with a ā€˜Conditionā€™ inside the action, and never saw the most obvious choice: ā€˜Chooseā€™.

@myle: Thanks for the tip!

All good bro my bill is.

You must paste your code here lol

@myle here it goes:

alias: Flash Office Floor Lamp lights red when the doorbell is pressed
description: >-
Flash Office Floor Lamp lights red 2 times when the doorbell is pressed, maintaining the last state off the lamp unchanged.
trigger:
  - platform: device
    device_id: xxxxxxxxxxxxxxxxxxxxxxx
    domain: nest
    type: doorbell_chime
condition: []
action:
  - choose:
      - conditions:
          - condition: device
            type: is_on
            device_id: yyyyyyyyyyyyyyyyyyyy
            entity_id: light.office_floor_lamp_level_light_color_on_off
            domain: light
        sequence:
          - data:
              scene_id: save_state_office_floor_lamp
              snapshot_entities: light.office_floor_lamp_level_light_color_on_off
            service: scene.create
          - scene: scene.office_floor_lamp_red
          - delay:
              hours: 0
              minutes: 0
              seconds: 0
              milliseconds: 750
          - repeat:
              count: '04'
              sequence:
                - type: toggle
                  device_id: yyyyyyyyyyyyyyyyyyyy
                  entity_id: light.office_floor_lamp_level_light_color_on_off
                  domain: light
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 500
          - data: {}
            service: scene.turn_on
            target:
              entity_id:
                - scene.save_state_office_floor_lamp
    default:
      - scene: scene.office_floor_lamp_red
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 750
      - repeat:
          count: '04'
          sequence:
            - type: toggle
              device_id: yyyyyyyyyyyyyyyyyyyy
              entity_id: light.office_floor_lamp_level_light_color_on_off
              domain: light
            - delay:
                hours: 0
                minutes: 0
                seconds: 0
                milliseconds: 500
      - data: {}
        service: scene.turn_on
        target:
          entity_id:
            - scene.save_state_office_floor_lamp
      - type: turn_off
        device_id: yyyyyyyyyyyyyyyyyyyy
        entity_id: light.office_floor_lamp_level_light_color_on_off
        domain: light
mode: single

1 Like

@ gmlupatelli

im back in the land of the living

thanks bro for paying the bill

1 Like