Adding delay to Xiaomi Switch

First of all I decided to switch on/ off ventilation using Xiaomi switch.
The automation looks like that (and it works flawlessly):

# Załączamy/ Wyłączamy Wentylator w Łazience
- alias: Załącz/ Wyłącz Wentylator w Łazience
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d000373c264
      click_type: single
  action:
    service: switch.toggle
    data:
      entity_id: switch.przelacznik_lazienka_wentylator

Next thing I decided to add is a delay to auto-switch off after 5 minutes if I forget to press a button. I modified previous code:

# Załączamy/ Wyłączamy Wentylator w Łazience
- alias: Załącz/ Wyłącz Wentylator w Łazience
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d000373c264
      click_type: single
  action:
    - delay: '00:05:00'
    - service: switch.off
      entity_id: switch.przelacznik_lazienka_wentylator

I can switch it on/ off via pressing a button but unfortunately the delay doesn’t work. What’s wrong with the code here?

You have 2 automations with the exact same name. Not sure what happens…but I’d guess the 2nd one wont get added due to ‘duplicate entity_id’ error.

Note: The alias is the entity id. It will generate an id of automation.alias (spaces and other characters will be converted to underscores)

But fixing that will cause another issue. If you push the button again, the 2nd automation will skip the delay and turn off the light. Then the first automation will call toggle which will turn it back on.

I’d make this a single automation.

- alias: Załącz/ Wyłącz Wentylator w Łazience
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d000373c264
      click_type: single
  action:
    - service: switch.toggle
      entity_id: switch.przelacznik_lazienka_wentylator
    # Waits for the switch to turn off, or until timeout. Note, if this event happens again,
    # this timeout will be cancelled and it will continue through the rest of the actions.
    # Note, if the toggle turned the switch off, this will skip the delay right away.
    - wait_template: "{{ is_state('switch.przelacznik_lazienka_wentylator', 'off') }}"
      timeout: "00:05:00"
    # If the switch state changed within the last 5 minutes, skip everything else.
    # This will happen if the toggle turned the light off, OR if another event fires 
    # before the timeout is complete.  
    - condition: template
      value_template: "{{ as_timestamp(now()) - as_timestamp(states.switch.outdoor_lights.last_changed) > 5*60 }}"
    # Switch hasn't changed state. Must have been left off. Turn it off.
    - service: switch.turn_off
      entity_id: switch.przelacznik_lazienka_wentylator

Hi!
I think you can solve this adding a timer to your configuration.yaml:

timer:
  timer_ventilation:
    duration: '00:05:00'

Then start your timer when switch triggers and count down 5 minutes, then turn it off:
Add this to your current automation to start the timer:

  - service: timer.start
    entity_id: timer.timer_ventilation

(Add this in your automations.yaml)

- alias: Turn off ventilator when timer is out
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.timer_ventilation
  action:
- service: switch.off
      entity_id: switch.przelacznik_lazienka_wentylator

A timer would work well. Make sure to reset the timer every time the other automation is called. Well, I guess timer.start would do that! So yeah…timer makes more sense here.