Use previous state to reset brightness to 100% when lights are turned off and on again quickly

I have hunted though multiple other topics and am doing my best to understand the docs, but just can’t seem to find a simple way of doing this.
Is there a way to have a condition that looks at the PREVIOUS STATE of an entity?

Situation:
I have a few Shelly Dimmer 2’s that control the brightness of our lights. I use scenes and automations to change those brightness for varied reasons, however I would like a simple physical way to reset the brightness back to 100% via the wall switch. The wall switch will toggle the Shelly Dimmer between ON and OFF but uses its last brightness setting.

Desired solution:
An automation that sets the lights to 100% brightness, using a condition that checks that the light WERE only off for 2 (or less) seconds.

automation:
# When the light turns on
  trigger:
  - platform: state
  - entity_id: light.my_light
  - to: "on"
# Light is only off for 2 seconds
- condition: state
  entity_id: light.my_light
  state: 'off'
- condition: not
  conditions:
  - condition: state
    entity_id: light.my_lights
    state: 'off'
    for:
      seconds: 2

Obviously the above won’t work as it contradicts itself. I think I may need templates? But not quite sure how to implement.

Any help would be greatly appreciated.

If I understand you correctly, then you want to carry out the action if the lights were turned on, then off and then back on again is that correct?

If so, I would assume using the wait for trigger with timeout may be the answer, something like this maybe?:

trigger:
  - platform: state
    entity_id:
      - light.bathroom_room
    from: "off"
    to: "on"
condition: []
action:
  - wait_for_trigger:
      - platform: state
        entity_id:
          - light.bathroom_room
        from: "on"
        to: "off"
    continue_on_timeout: false
    timeout:
      seconds: 2
  - wait_for_trigger:
      - platform: state
        entity_id:
          - light.bathroom_room
        from: "off"
        to: "on"
    timeout:
      seconds: 2

Or a variation based on your needs if I have misunderstood!

Absolutely perfect! Thanks so much for your reply.

I have read up on Wait For Triggers and now am able to get the exact result I was after. I tweaked your code slightly, as after testing I realised I only wanted a 2 second wait between off and on, and the initial off timeout wasn’t required. Also 2 seconds was a bit ambitious, so I increased to 4. Here is the complete code for anyone else who finds this useful, and a link to Wait For Trigger docs.

  trigger:
  - platform: device
    type: turned_on
    device_id: #############
    entity_id: light.my_light
    domain: light
  condition:
  action:
  - wait_for_trigger:
    - platform: state
      entity_id: light.my_light
      from: 'on'
      to: 'off'
  - wait_for_trigger:
    - platform: state
      entity_id: light.my_light
      from: 'off'
      to: 'on'
    continue_on_timeout: false
    timeout:
      seconds: 4
  - service: light.turn_on
    data:
      brightness_pct: 100
    target:
      entity_id: light.my_light
1 Like