To throw another way to accomplish this out there. I use the Average Sensor (available via HACS [also here: GitHub - Limych/ha-average: Average Sensor for Home Assistant]).
- platform: average
name: Master Bathroom Humidity Average
duration:
days: 1
entities:
- sensor.master_bathroom_sensor_relative_humidity_measurement
I then created an input_number which saves a threshold. This is used so I can easily update this threshold without having to restart HA.
master_bath_humidity_threshold:
name: Master Bath Humidity Threshold
min: 0
max: 20
initial: 9
mode: box
step: 1
I use a binary_sensor to check if the current humidity minus the average humidity is greater than the input_number threshold.
master_bath_fan_sb:
friendly_name: Master Fan S/B Status
value_template: "{{ (states('sensor.master_bathroom_sensor_relative_humidity_measurement')|int - states('sensor.master_bathroom_humidity_average')|int) > states('input_number.master_bath_humidity_threshold')|int }}"
This automation then works to turn the fan on/off based on the value of the binary_sensor changing.
- 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}}
Really all you need is the average sensor and the automation - I just use the input_number to allow me to change the ‘test’ without restarting / editing the automation. Same with the binary_sensor - this could be done in the automation itself with a trigger template and/or by breaking into two automations. Anyhow, more than one way to skin a cat, as always, with HA.