I’ve posted a couple of times over the years my bathroom fan / humidity sensor setup. With some of the HA changes, I’ve updated it a bit and wanted to post the updated version.
This one is simplified a bit, using only an statistics sensor, binary sensor and an automation.
Statistics sensor: Calculates the average humidity in the bathroom over the last 24 hours. This helps to account for seasonal changes and smooths the baseline.
- platform: statistics
name: Master Bathroom Humidity Average Linear 24h
entity_id: sensor.master_bathroom_sensor_relative_humidity_measurement
state_characteristic: average_linear
max_age:
hours: 24
Binary Sensor: Determines whether the current humidity is > average humidity - ie. should the fan be on? Worth noting that this can be as complicated or simple as you’d like it to be. The simple one below checks if the current humidity a fixed amount (threshold) greater than the average. [More complicated example at end of post]
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_linear_24h')|int(default=0) %}
{% set thresh = 5 %}
{{ (humid - avg) > thresh }}
Automation: Turns the bathroom fan on/off based on the value of the binary sensor above.
- 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}}
Additional Options:
- Input Number for Threshold. Personally, I use an input_number to hold the threshold value - this makes it changeable w/ out a reboot of HA. Create an input_number:
master_bath_humidity_threshold:
name: Master Bath Humidity Threshold
min: 0
max: 20
initial: 5
mode: box
step: 1
and also, in the binary sensor above, replace
{% set thresh = 5 %}
with
{% set thresh = states('input_number.master_bath_humidity_threshold')|int(default=0) %}
- Advanced Binary Sensor. When the AC is on and my humidity is > 45%, I like to reduce the threshold and make the fan more aggressive to remove more moisture. Here’s my actual binary_sensor, which checks for these and cuts the threshold in half under that scenario.
master_bath_fan_sb:
friendly_name: Master Fan S/B Status
value_template: >-
{% set humid = states('sensor.master_bathroom_sensor_relative_humidity_measurement')|int %}
{% set avg = states('sensor.master_bathroom_humidity_average_linear_24h')|int(default=0) %}
{% set thresh = states('input_number.master_bath_humidity_threshold')|int(default=0) %}
{% if states('climate.6900_master') == 'cool' and avg > 45 %}
{% set thresh = thresh / 2 %}
{% endif %}
{{ (humid - avg) > thresh }}
You can create your own logic and update the binary_sensor accordingly if desired.
Lastly. There are definitely a multitude of other ways to accomplish the desired result, this is only the way that I’m using / works for me.