Love @123’s solution here. FWIW. I added a statistics average sensor to calc the average humidity over the last 24 hours and use this instead of hard-coding the above/below numeric values. Allows for seasonality / rainy days etc. I also use a threshold above the average, so when humidity > (average + threshold ) the fan turns on and then back off when it’s back below the (threshold + buffer).
I created a binary_sensor that triggers my fan toggle automation:
master_bath_fan_sb:
friendly_name: Master Fan S/B Status
value_template: >-
{% set humid = states('sensor.master_bathroom_sensor_relative_humidity_measurement')|int(default=0) %}
{% set avg = states('sensor.master_bathroom_humidity_average')|int(default=0) %}
{% set thresh = states('input_number.master_bath_humidity_threshold')|int(default=0) %}
{% if states('climate.6900_master') == 'cool' and avg > 49 %}
{% set thresh = thresh / 2 %}
{% endif %}
{{ (humid - avg) > thresh }}
Automation:
- id: '1555779101294'
alias: Master Bath Fan - TOGGLE
trigger:
- entity_id: binary_sensor.master_bath_fan_sb
for: 00:00:30
platform: state
condition: []
action:
- data:
entity_id: switch.master_bath_fan
service_template: switch.turn_{{trigger.to_state.state}}
I use an input_number to more easily save / update the threshold and also of not that if my AC is on, then I leave the fan on a little longer to take more humidity out before turning off.