Streamline Bathroom Fan Automation

I currently have a bathroom fan automation that will turn the fan on if the delta between the bathroom humidity and house humidity is over a certain %. I have a 2nd one for turning the fan off when the humidity falls below the threshold.

Recently I added a condition to them as the threshold % will be different if the AC is on vs. heat vs. nothing on.

I would like to ultimately see if I can bundle this into 1 automation with all the new enhancements of late, but I am need some expertise.

- id: '1595463904722'
  alias: Master Bathroom - Humidity High (AC)
  description: Only trigger if AC is running
  trigger:
  - above: '7'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ state.state|float - states(''sensor.main_floor_humidity'')|float
      }}'
  condition:
  - condition: state
    entity_id: climate.main_floor
    state: cool
  action:
  - data: {}
    entity_id: switch.master_fan_107
    service: switch.turn_on
  mode: single
- id: '1595464068059'
  alias: Master Bathroom - Humidity Normal (AC)
  description: ''
  trigger:
  - below: '7'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ state.state|float - states(''sensor.main_floor_humidity'')|float
      }}'
  condition:
  - condition: state
    entity_id: climate.main_floor
    state: cool
  action:
  - data: {}
    entity_id: switch.master_fan_107
    service: switch.turn_off
  mode: single

Any suggestions for

  • How to combine into a single automation
  • Is there way to have the trigger basically say
    if heating is on, or system is off AND delta > 15%, turn on fan.
    If AC is ON AND Delta > 7% turn on fan.
    If either of those 2 cases is true and Delta % drops back below thresholds, turn back off fan

I think with the 0.115 release, you can do all of that. Just to spitball some ideas:

You can trigger the automation with each

  • above 7%
  • above 15%
  • below 7%
  • below 15%

Then use a template to call either switch.turn_on or switch.turn_off using both logic and conditions

If HD (humidity delta) < 7 -> call switch.turn_off
Else if HD > 7 and cooling on -> call switch.turn_on
Else if HD < 15 and (AC off or heating on) -> call switch.turn_off
Else if HD > 15 -> call switch.turn_on

This logic can probably be streamlined in the template. To make this somewhat easier and more readable, you could save the humidity delta as a variable in the automation.

Sorry for the formatting, I’m on mobile.

Maybe something like this? There may be a typo or error in there, but this might be a starting point

- id: '1595463904722'
  alias: Master Bathroom - Humidity High
  description: Trigger if difference in humidity crosses either threshold
  variables: 
    humidity_delta: '{{ states(''sensor.mb_humidity_59'')|float - states(''sensor.main_floor_humidity'')|float }}'
    climate: '{{ states(''climate.main_floor'')|string }}'
  trigger:
  - above: '7'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ humidity_delta }}'
  - above: '15'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ humidity_delta }}'
  - below: '7'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ humidity_delta }}'
  - below '15'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ humidity_delta }}'
  action:
  - data: {}
    entity_id: switch.master_fan_107
    service: '{% if (humidity_delta < 7) or (humidity_delta < 15 and (climate == ''off'' or climate == ''heat'')) %}switch.turn_off{% elif (humidity_delta > 15) or (humidity_delta > 7 and climate == ''cool'')%}switch.turn_on{% end if %}'
  mode: single

Thank you so much @Dan_W. I appreciate the assistance.

I will give it a try this weekend.

@Dan_W I was able to get the automation setup (minor issue at end… endif fixed it). It doesn’t appear though to be working though in terms of triggers. I removed the ‘ around the above/below pieces in triggers as VS Code flagged that, but not sure if that is making any difference or not.

I got around to testing it and (besides the endif and data structure typos) I was able to get it working. However, I wasn’t able to use the variables in the trigger value templates. This should work, even if it isn’t the cleanest:

- id: '1595463904722'
  alias: Master Bathroom - Humidity High
  description: Trigger if difference in humidity crosses either threshold
  variables: 
    humidity_delta: '{{ states(''sensor.mb_humidity_59'')|float - states(''sensor.main_floor_humidity'')|float }}'
    climate: '{{ states(''input_select.climate'')|string }}'
  trigger:
  - above: '7'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ states(''sensor.mb_humidity_59'')|float - states(''sensor.main_floor_humidity'')|float }}'
  - above: '15'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ states(''sensor.mb_humidity_59'')|float - states(''sensor.main_floor_humidity'')|float }}'
  - below: '7'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ states(''sensor.mb_humidity_59'')|float - states(''sensor.main_floor_humidity'')|float }}'
  - below: '15'
    entity_id: sensor.mb_humidity_59
    platform: numeric_state
    value_template: '{{ states(''sensor.mb_humidity_59'')|float - states(''sensor.main_floor_humidity'')|float }}'
  action:
  - data:
      entity_id: switch.master_fan_107
    service: '{% if (humidity_delta|float < 7) or (humidity_delta|float < 15 and (climate == ''off'' or climate == ''heat'')) %}switch.turn_off{% elif (humidity_delta|float > 15) or (humidity_delta|float > 7 and climate == ''cool'')%}switch.turn_on{% endif %}'
  mode: single

The two main things I got from the testing is

  • The variable still needed to be filtered as a float
  • I don’t know how to use the variables within value_template (even with |float)

@Dan_W Thanks for all your help so far.

Found out from Paulus that variables are NOT supported for Triggers, so that is answers one of the questions.

I am seeing some issues with the automation code where it is NOT turning off the fan when the humidity drops back down. I believe it is only occurring when Heat/Off states.

Any ideas? I looked over the code and seems like the nesting is correct.

Thanks,
Paul

You are welcome! It was fun learning how (and how not) to use the variables.

As for your issue, I’m not sure. Can you tell if the automation is being triggered and the switch isn’t turning off or if the automation just isn’t being triggered as humidity drops below the threshold?