Help with restoring state of bulb after toggling

I have a sliding door which sometimes gets left open. It has contact sensor, so I want to use that to toggle a bulb until the door is closed, if the door has been left open for a period of time. I have an automation which almost does what I want, but I can’t figure out how to restore the original state of the bulb when the door is closed. If the bulb was off when the automation began executing, it should return to off, when the door is closed. Here is what I have:

alias: Patio Door Opened
description: ''
trigger:
  - platform: state
    entity_id: group.patio_sensors
    to: 'on'
    for: '00:01:00'
condition: []
action:
  - repeat:
      while:
        - condition: state
          entity_id: group.patio_sensors
          state: 'on'
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
        - type: toggle
          device_id: 596a12edf624a47d9ffd8e8e4e97f1d7
          entity_id: light.window_bulb
          domain: light
mode: single

How can I restore the bulbs state?
Thanks

1 Like

You will have to store somewhere the bulb’s state, when the automation is triggered.

For example, you can use an input_text helper. Let’s call it input_text.bulb_state

Then the first action of your automation should be

alias: Patio Door Opened
description: ''
trigger:
  - platform: state
    entity_id: group.patio_sensors
    to: 'on'
    for: '00:01:00'
condition: []
action:
  - service: input_text.set_value
    target:
      entity_id: input_text.bulb_state
    data:
      value: |
        {{ states('light.window_bulb') }}
................

Then make another automation for when the door is closed

alias: Patio Door Closed
description: ''
trigger:
  - platform: state
    entity_id: group.patio_sensors
    to: 'off'
condition: []
action:
  - service_template: >
      light.turn_{{ states('input_text.bulb_state') }}  
    entity_id: light.window_bulb

A common way of doing it is by creating a scene on the fly. Look at the last example on the linked page.

Thanks, I decided to go with creating a scene.

1 Like

Just to close the loop on this. I was able to make this into a single automation. Works like a charm. Here’s the yaml. All comments appreciated.

alias: Patio Door Contact Toggled
description: ''
trigger:
  - platform: state
    entity_id: group.patio_sensors
    to: 'on'
    for: '00:10:00'
condition: []
action:
  - service: scene.create
    data:
      scene_id: before
      snapshot_entities: light.window_bulb
  - repeat:
      while:
        - condition: state
          entity_id: group.patio_sensors
          state: 'on'
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
        - type: toggle
          device_id: 596a12edf624a47d9ffd8e8e4e97f1d7
          entity_id: light.window_bulb
          domain: light
  - scene: scene.before
mode: single

Check if whatever physical light that represents light.window_bulb supports the flash option.

flash Tell light to flash, can be either value short or long.

If it does, then you may not need repeat-while but a simple wait_template.

Thanks, but the bulb doesn’t support flash (GoControl LBZ0-1).