4 minutes timer for a bathroom's fan

Hi guys. Can somebody help me to fix this code for my fan automation, please? My idea is: When I switch on the bathroom light for more than 1 minute and after this time period I switch off the bathroom light, the bathroom fan will turn on for 4 minutes and turn off after this period.

This is my code:

- alias: "Fan Timer" 
  initial_state: 'on'
  trigger: 
  - platform: state
    entity_id: switch.bathroom
    to: 'on'
    for: "00:01:00"
  - platform: state
    entity_id: switch.bathroom
    to: 'off'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: switch.bathroom
      state: 'on'
      for: "00:01:00"
    - condition: state
      entity_id: switch.bathroom
      state: 'off'
  action: 
  - service: switch.turn_on
    entity_id: switch.fan
  - delay: '00:04:00'  
  - service: switch.turn_off
    entity_id: switch.fan
1 Like

Your conditions contradict each other. How can the switch be on for 1 minute and also off?

So, you have a unique problem. You want to only fire this automation when the light has been on for 1 minute or more. What you need to do is make 2 automations and 1 script.

The first automation will only trigger when the bathroom light has been on for more than 1 minute. When this triggers, that automation will turn on the second automation.

The second automation will only trigger when the bathroom light has been turned off. This automation will cancel the script, then fire the script, then turn off itself. The cancel is done so that the automation is reset. This allows it to do another 4 minutes if you happen to walk in during the 4 minutes and turn the light on for 1 minute.

- alias: Enable Fan Timer Automation
  trigger:
  - platform: state
    entity_id: switch.bathroom
    to: 'on'
    for: "00:01:00"
  action:
  - service: homeassistant.turn_on
    entity_id: automation.fan_timer

- alias: Fan Timer
  trigger:
  - platform: state
    entity_id: switch.bathroom
    to: 'off'
  action:
  - service: script.turn_off
    entity_id: script.fan_timer
  - service: script.turn_on
    entity_id: script.fan_timer
  - service: homeassistant.turn_off
    entity_id: automation.fan_timer

Then your script

fan_timer:
  sequence:
  - service: switch.turn_on
    entity_id: switch.fan
  - delay: '00:04:00'
  - service: switch.turn_off
    entity_id: switch.fan
4 Likes

Hurray. It works like a charm. You are my hero.:slightly_smiling_face:

Thank you very much.

I have tried the automation(s) and script sticking exactly to @petro’s example above.

Unfortunately it does not work as expected but instead of turning On the exhaust fan 1 minute after the light has been switched to On it switches the exhaust fan to ON only after the light switch has already being switched back to Off. Then fan keeps running for 4 minutes as expected and switches itself to OFF after time has been running out.

Shouldn’t it work as the following?

  1. The light switch gets flipped to On.
  2. 1 minute thereafter the exhaust fan is turned On.
  3. After the light switch has been flipped back to Off again, the exhaust fan still continues to run for 4 minutes.
  4. If the light switch stays switched Off during those 4 minutes, the exhaust fan switches itself to OFF after the given time span.
    but
    If the light switch is flipped back to ON again before those 4 minutes are over (thus the exhaust fan is still On/running) the fan should keep running as long as the lights are in state On and switches itself to Off only after 4 minutes after the light switch has been switched to Off.

Any help is much appreciated.