Automation fan struggle only run 30min and low for the night

Hi
Is there a way to let the fan only run for 30 minutes max every time it triggers? It needs some pause I think for like 15 minutes to avoid another trigger run after those 30 minutes.
And when the time hits 7 pm it needs to change back to Low and is not allowed to change again until the next morning on 9 am.

I’m struggling to get it into one automation for weeks now. Or is it best practice to create another automation to control the 30 minutes and 7 pm requirement?

alias: fan
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.xiaomi_humidity
    above: 70
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: medium
  - platform: numeric_state
    entity_id:
      - sensor.xiaomi_humidity
    above: 70
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: low
  - platform: numeric_state
    entity_id:
      - sensor.esp_co2
    above: 1200
    id: medium
  - platform: numeric_state
    entity_id:
      - sensor.esp_co2
    below: 1199
    id: low
  - platform: time
    at: "19:00:00"
    id: low
condition:
  - condition: time
    after: "09:00:00"
    before: "18:59:00"
action:
  - service: fan.set_preset_mode
    metadata: {}
    data:
      preset_mode: "{{ trigger.id }}"
    target:
      entity_id: fan.wtw_ventilation
mode: single

The way you have your condition set up will not allow the 7pm trigger to cause any action. If you use an Or condition you can allow the Time trigger to initiate actions outside of the “working hours” of the Time condition.

Using a timer is more reliable than trying to add extended delay. You will need to create a timer helper.

alias: fan
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.xiaomi_humidity
    above: 70
    for: "00:05:00"
    id: medium
  - platform: numeric_state
    entity_id: sensor.xiaomi_humidity
    above: 70
    for: "00:05:00"
    id: low
  - platform: numeric_state
    entity_id: sensor.esp_co2
    above: 1200
    id: medium
  - platform: numeric_state
    entity_id: sensor.esp_co2
    below: 1199
    id: low
  - platform: time
    at: "19:00:00"
    id: low
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.wtw_ventilation
    id: 'off'
condition:
  - or:
      - condition: time
        after: "09:00:00"
        before: "18:59:00"
      - condition: template
        value_template: "{{ trigger.platform == 'time'}}"
action:
  - if:
      - condition: trigger
        id: 'off' 
    then:
      - service: fan.turn_off
        target:
          entity_id: fan.wtw_ventilation
    else:
      - service: fan.set_preset_mode
        metadata: {}
        data:
          preset_mode: "{{ trigger.id }}"
        target:
          entity_id: fan.wtw_ventilation
      - condition: template
        value_template: "{{ trigger.platform != 'time'}}"
      - service: timer.start
        target:
          entity_id: timer.wtw_ventilation
        data:
          duration: "00:30:00"
mode: single
1 Like

Thx for the suggestion! never thought of using a timer

Is this correct?
"{{ trigger.platform != 'time'}}"
Or does it need to be like this?
"{{ trigger.platform = 'time'}}"

I will test it in the upcoming days!

Yes, != means “not equal to”. That template condition prevents running the “turn off” timer when the automation is triggered at 7pm.

1 Like

One question before I test. I’m using Spook and it gives me this message:

This automation refers to the following entities, which are unknown to Home Assistant:
timer.wtw_ventilation

Spook 👻 Your homie.

Do I need to create the timer first? Before using it in an automation?

Yes you need to create the timer. It doesn’t matter if you do it before or after editing the automation as long as the entity IDs match. If you are on 2024.5 you can actually create the timer while editing the automation.