Bathroom fan automation help

I’m using an Aqara HT to trigger a shelly 1L when humidity in my bathroom goes over 75%, run for 20 minutes, and then turn off. I live in a humid climate with crappy exhaust fans, so I’d like it to run for 20 minutes after the humidity goes back below 75%. Any help would be appreciated (and yes, I know about the over the counter solutions. I tried Dewstop and Nutone, but they were either way too sensitive or would fire at 2 in the morning waking me and Mrs. Super up.) I took a stab at it, but the fan now just seems to turn off when it drops below 75%. I have included the current automation setup below:

alias: Primary Bathroom Fan Trigger
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.lumi_lumi_weather_3090e007_humidity
    above: '75'
    below: '100'
condition:
  - condition: not
    conditions:
      - condition: time
        after: '22:30:00'
        before: '06:30:00'
action:
  - type: turn_on
    device_id: 16b047e15e88315d704cc1e7ac0fc396
    entity_id: switch.primary_bathroom_fan_switch
    domain: switch
  - wait_for_trigger:
      - platform: numeric_state
        entity_id: sensor.lumi_lumi_weather_05cae007_humidity
        above: '0'
        below: '74'
    continue_on_timeout: false
  - delay:
      hours: 0
      minutes: 20
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 16b047e15e88315d704cc1e7ac0fc396
    entity_id: switch.primary_bathroom_fan_switch
    domain: switch
mode: restart

Think about that:

before 6:30 and after 22:30 on the same day. That means: Never.

I use it like that, spanning across midnight, as in the docs:

never mind, I mixed it up with the sun before and after conditions, there is before sunset and after sundown not posible :wink:

Have you considered using trigger IDs. because:

The trigger “Fires when the numeric value of an entity’s state (or attribute’s value if using the attribute property, or the calculated value if using the value_template property) crosses (and only when crossing) a given threshold. On state change of a specified entity, attempts to parse the state as a number and fires if the value is changing from above to below or from below to above the given threshold.”

something like this.

alias: Turn on and Off Fan
description: 'Turn on if humidity crosses 78.  Turn off after 20 minutes or humidity below 74 whichever comes first.'
mode: single
trigger:
  - platform: numeric_state
    entity_id: sensor.lumi_lumi_weather_3090e007_humidity
    above: '78'
    id: above
  - platform: numeric_state
    entity_id: sensor.lumi_lumi_weather_3090e007_humidity
    below: '74'
    id: below
  - platform: state
    entity_id: switch.primary_bathroom_fan_switch
    to: on
    for:
      minutes: 20
    id: below
condition: 
  - condition: not
    conditions:
      - condition: time
        after: '22:30:00'
        before: '06:30:00'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: above
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.primary_bathroom_fan_switch
      - conditions:
          - condition: trigger
            id: below
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.primary_bathroom_fan_switch
    default: []          

You can do all of this through the UI

Here’s a post where I show how I do this based on the average humidity (as it changes based on weather).

@BebeMischa, that’s good to know for sun state. I’ve been these conditions and it’s been working fine so far, as in not firing at all when we’re sleeping

@AllHailJ This is awesome, my one request is how can I tweak this to have it continue running the fan for 20 minutes after the humidity crosses back below 75?

Like this:

alias: Turn on and Off Fan
description: 'Turn on if humidity crosses 78.  Turn off after 20 minutes after humidity below 74'
mode: single
trigger:
  - platform: numeric_state
    entity_id: sensor.lumi_lumi_weather_3090e007_humidity
    above: '78'   <-- Leave some room here so it doesn't kick off then kick back on really quickly 
    id: above
  - platform: numeric_state
    entity_id: sensor.lumi_lumi_weather_3090e007_humidity
    below: '74'
    for:
      minutes: 20
    id: below
condition: 
  - condition: not
    conditions:
      - condition: time
        after: '22:30:00'
        before: '06:30:00'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: above
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.primary_bathroom_fan_switch
      - conditions:
          - condition: trigger
            id: below
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.primary_bathroom_fan_switch
    default: []          

Giving this a shot today! Thanks!!!

This is really cool! Thank you for sharing. I’ll try this if the automation loses sensitivity, which might happen over the summer here.

It also occurred to me that you could clean up the condition and take it out of the negative. see below

alias: Turn on and Off Fan
description: 'Turn on if humidity crosses 78.  Turn off after 20 minutes after humidity below 74'
mode: single
trigger:
  - platform: numeric_state
    entity_id: sensor.lumi_lumi_weather_3090e007_humidity
    above: '78'    
    id: above
  - platform: numeric_state
    entity_id: sensor.lumi_lumi_weather_3090e007_humidity
    below: '74'
    for:
      minutes: 20
    id: below
condition: 
  - condition: time
    after: '06:30:00'
    before: '22:30:00'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: above
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.primary_bathroom_fan_switch
      - conditions:
          - condition: trigger
            id: below
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.primary_bathroom_fan_switch
    default: []         

Here’s my final automation. I ended up changing the time limit conditions to only the on trigger because we were showering right before 1030, and the fan would never turn off. Works like a charm! Thank you all, especially @AllHailJ for the help!

alias: Turn on and Off Fan (Duplicate)
description: >-
  Turn on if humidity crosses 75.  Turn off after 20 minutes after humidity
  below 74
trigger:
  - platform: numeric_state
    entity_id: sensor.lumi_lumi_weather_humidity
    above: '75'
    id: above
  - platform: numeric_state
    entity_id: sensor.lumi_lumi_weather_humidity
    below: '74'
    for:
      minutes: 20
    id: below
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: above
          - condition: time
            after: '06:00:00'
            before: '22:30:00'
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.primary_bathroom_fan_switch
      - conditions:
          - condition: trigger
            id: below
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.primary_bathroom_fan_switch
            data: {}
    default: []
mode: restart