Two competing automations for bathroom exhaust fan - timer switch and hygrostat - how to merge?

My bathroom exhaust fan is connected to a Lutron switch and I have the following automation:

alias: Master Exhaust On from Switch
description: ''
trigger:
  - platform: state
    entity_id:
      - switch.master_exhaust
    to: 'on'
condition: []
action:
  - delay:
      hours: 0
      minutes: 40
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: b85b5ee673371768763dd738e57b844f
    entity_id: switch.master_exhaust
    domain: switch
mode: single

This is great if people in the house remember to click the switch but, as a backup, I also found a great hygrostat sensor template:

- platform: generic_hygrostat
  name: Master Bathroom Hygrostat
  sensor: sensor.master_bath_humidity # Source humidity sensor
  attribute: humidity # Optional use sensor attribute instead of state.
  delta_trigger: 3 # Optional humidity swing to detect. Default = 3
  target_offset: 3 # Optional dehumidification target offset. Default = 3
  min_on_time: 0 # Optional min on time in seconds. Default = 0 seconds
  max_on_time: 7200 # Optional safety max on time in seconds. Default = 7200 seconds
  sample_interval: 300 # Optional time between taking humidity samples in seconds, default 300 seconds
  min_humidity: 30 # Optional minimum humidity to enable dehumidification. Default = 0
  unique_id: master_bathroom_hygrostat # Optional ID that uniquely identifies this sensor. Set this to a unique value to allow customization through the UI.   

and have the following automation for turning it on/off:

alias: Bathroom Hygrostat  - Master Exhaust On
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.master_bathroom_hygrostat
    to: 'on'
condition: []
action:
  - type: turn_on
    device_id: b85b5ee673371768763dd738e57b844f
    entity_id: switch.master_exhaust
    domain: switch
mode: single

I have two issues now:
1 (less common/important). Turn on exhaust fan at switch but time in bathroom approaches 40 minutes so turn off at 30 minutes to ‘reset’ but switch turns off anyway 40 minutes from original time - not sure how to use a condition for this rare need

2 (more important). Exhaust fan turned on manually from switch but turns off 40 minutes later regardless of state of humidity in the bathroom per the hygrostat template. I’m not sure how to use a condition on the switch automation to not turn off if the hygrostat is on and if that might end up in some crazy loop.

Thanks!

First a comment, why have the manual button. I have removed mine and only have the automatic one. My kids were anyway forgetting all the time.

  1. if you make the automation in restart mode, then it will reset the delay when you press on the button after 30 min. A new 40 delay will start.

  2. in the action part, make a condition looking for hydrostat to be off, or just remove it, and only turn off based on the hydrostat? The condition is easy to create in the automation action gui, is called condition😏

Thanks - I think I got confused as to how to use the condition - should it continue only if hygrostat was on? Off?

Off makes more sense now that I see your post.

As to the manual button, there is something reassuring about pressing and there are instances where the hygrostat won’t fire but you might still want the exhaust to run without adding complexity of occupancy, etc

I’d get away from the wait in there because it doesn’t survice a restart or save of a new automation.

I wrote this quickly and might not work completely, but this is how i’d do it (some people may not like this in a single automation shrugs)

alias: Bathroom Hygrostat  - Master Exhaust
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.master_bathroom_hygrostat
    to: 'on'
    id: active
  - platform: state
    entity_id:
      - switch.master_exhaust
    to: 'on'
    id: active
  - platform: state
    entity_id:
      - switch.master_exhaust
    from: 'on'
    to: 'off'
    id: manualoff
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.some_timer
    id: timerfinished
condition: []
action:
  - if:
      - alias: The switch or sensor turns on.  Start the fan and timer
        condition: trigger
        id: active
    then:
      - service: timer.start
        data:
          duration: 1800
        target:
          entity_id: timer.some_timer
      - if:
         - alias: turn the fan on if its off
           condition: state
           entity_id: switch.master_exhaust
           state: 'off'
        then:
          - type: turn_on
            device_id: b85b5ee673371768763dd738e57b844f
            entity_id: switch.master_exhaust
            domain: switch
  - if:
      - alias: Manual off from the switch
        condition: trigger
        id: manualoff
    then:
      - service: timer.cancel
        target:
          entity_id: timer.some_timer
      - type: turn_off
        device_id: b85b5ee673371768763dd738e57b844f
        entity_id: switch.master_exhaust
        domain: switch
  - if:
      - alias: The timer ended, its time to consider turning it off
        condition: trigger
        id: timerfinished
    then:
      - if:
         - alias: extend the timer since sensor is on
           condition: state
           entity_id: binary_sensor.master_bathroom_hygrostat
           state: 'on'
        then:
          - service: timer.start
            data:
              duration: 600
            target:
              entity_id: timer.some_timer
        else:
          - type: turn_off
            device_id: b85b5ee673371768763dd738e57b844f
            entity_id: switch.master_exhaust
            domain: switch
mode: queued

btw, I have no idea what you meant here:

EDIT: oh I see. That isn’t needed anymore. It’ll just stay on with the sensor unless you manuall turn it off or the timer expires.

You could also add a trigger that will fire and turn the fan off earlier if the sensor goes from on to off (meaning it was previously humid and that has cleared so turn off the fan and cancel the timer)

  - platform: state
    entity_id:
      - binary_sensor.master_bathroom_hygrostat
    from: 'on'
    to: 'off'
    id: manualoff
1 Like