Turn a fan on and off based on the difference between a humidity sensor and a baseline

The idea here is for a fan to turn on off based on comparing a humidity sensor’s value to a reference humidity.

For example, this could be the humidity in the house at the thermostat. It could be based on the local weather. You can use this automation to have the humidity in your bathroom trend with the humidity outside of the bathroom (which may change with the weather).

  name: Humidity Management
  description: Turn a fan on and off based on the difference between a humidity sensor and a baseline
  domain: automation
        name: Humidity Sensor
        description: A sensor that measures the humidity of the area
            domain: sensor
        name: Reference Humidity
        description: A percentage point value that indicates the baseline humidity if there is no reference sensor available
        default: 60
        name: Reference Humidity Sensor
        description: A sensor that indicates the baseline humidity of the location
            domain: sensor
        default: []
        name: Fan Switch
        description: A switch that turns the fan on and off
            domain: switch
        name: Rising Threshold
        description: How many percentage points above the reference humidity the sensor can rise before the fan is turned on
            min: 0
            max: 100
        default: 8
        name: Falling Threshold
        description: How many percentage points above the reference humidity the sensor must fall to before the fan is turned off
            min: 0
            max: 100
        default: 3
  - entity_id: !input humidity_sensor
    platform: state
  - entity_id: !input reference_humidity_sensor
    platform: state
  - condition: template
    value_template: '{{ mode != switch_state }}'
  - service: switch.turn_{{mode}}
    entity_id: !input fan_switch
    reference_humidity: !input reference_humidity
    humidity_sensor: !input humidity_sensor
    reference_humidity_sensor: !input reference_humidity_sensor
    fan_switch: !input fan_switch
    switch_state: '{{ states(fan_switch) }}'
    rising_threshold: !input rising_threshold
    falling_threshold: !input falling_threshold
    difference: '{{ states(humidity_sensor)|float - (states(reference_humidity_sensor)|float
      or reference_humidity|float) }}'
    mode: '{% if switch_state == ''off'' and difference|float > rising_threshold|float %}on{%
      elif switch_state == ''on'' and difference|float > falling_threshold|float %}on{% else %}off{%
      endif %}'
mode: single

Nice. You could use the generic hygrostat component for this, but it hasn’t been merged yet, so you’ll have to add it to your custom_components.

Thank you very much for sharing !

I gave it a try and I get this error message :

Logger: homeassistant.components.automation.salle_de_bain_humidity_management
Source: components/automation/init.py:373
Integration: Automation (documentation, issues)
First occurred: 11:05:32 AM (1 occurrences)
Last logged: 11:05:32 AM

Error rendering variables: AttributeError: ‘list’ object has no attribute ‘lower’

This is the yaml automation created :

alias: Salle de Bain - Humidity Management
description: Blueprint
  path: >-
    falling_threshold: '30'
    humidity_sensor: sensor.aqara_sdb_humidity
    reference_humidity: '60'
    rising_threshold: '35'
    fan_switch: switch.4chpror2_4

This seems to perform the same function as the existing Threshold Binary Sensor.

The threshold binary sensor platform observes the state of another sensor. If the value is below ( lower ) or higher ( upper ) than the given threshold then state of the threshold sensor is changed. It support also a range if lower and upper are given.

1 Like

Ah, that is a brilliant solution, I dig it! I was trying to solve this by checking for a sudden increase, but using a reference sensor is even better.

Good day, Thank you for sharing this blueprint. Great work, however I get the following error:

Logger: homeassistant.components.automation.humidity_management
Source: components/automation/__init__.py:373
Integration: Automation (documentation, issues)
First occurred: 3:39:43 AM (122 occurrences)
Last logged: 11:23:48 AM

Error rendering variables: TypeError: '>' not supported between instances of 'float' and 'str'

This is the yaml created:

alias: Main Bathroom Humidity Management
description: Manage the bathroom humidity with the extractor fan.
  path: >-
    falling_threshold: '3'
    humidity_sensor: sensor.sonoff_1000bec77f_humidity
    reference_humidity: '80'
    reference_humidity_sensor: sensor.ble_temperature_a4c138349488
    fan_switch: switch.sonoff_1000bec77f
    rising_threshold: '8'

The numbers shouldn’t be strings ('3' is incorrect, 3 is)

That appears to be the way the input selector’s value is stored. The input selector’s default value is an integer:

default: 3

Yet when stored in the generated automation, it’s a string. Not sure why the system makes that conversion. :thinking:

Maybe they forgot to use a number selector?

falling_threshold is a Number selector.

But reference_humidity isn’t. I understand what you’re saying though, it’s a number selector, it should be stored as a number.

True, that one isn’t a Number selector but its default value is also an integer (60). If you look at Tibuski’s post, the default integer value is, yet again, converted to string.

It seems to ignore the value’s type and always stores it as string. Maybe it’s assuming automatic type conversion will take care of it later, when the value is consumed at runtime. :man_shrugging:

Or it’s a bug …

Hey all, thanks for the heads up. I just fixed the bug by editing the blueprint in the original post.

Although the thresholds are selected as numbers, they become strings in the variable definitions. They must be cast to numbers for the comparisons to work properly.

That part was understood (evident from the error message). What KTibow and I were discussing was a blueprint’s preference to store numeric values as strings (as seen in the automation generated by the blueprint). Seems like a curious design decision.

Good work. I noticed that it does not take into account if its started the fan or not and that I think would make it more versatile. For example I tried to turn my electrical floorheating on when the humidity was raised but that will not work since it will for the moment also turn off the heating during hours when I want it on all the time.

It would also be nice if the reference sensor could be the same sensor but with an averaged value over time (when NOT triggered), then most of us wouldnt need an extra sensor.


but my fan isnt a switch…


    - platform: mqtt
      name: Fan
      unique_id: "b5f92e68-a4b2-463d-8816-26a0fa340d9b"
      command_topic: "itho/cmd"
      speed_command_topic: "itho/cmd"
      state_topic: "itho/state"
      speed_state_topic: "itho/state"
      state_value_template: >
        {% if value_json == 0 -%}0{%- elif value_json > 0 -%}254{%- endif %}
      speed_value_template: "{{value.json}}"
      payload_off: "0"
      payload_on: "254"
      payload_low_speed: "65"
      payload_medium_speed: "127"
      payload_high_speed: "254"

this is the code for my fan, how would i get it working with this script ?

Change the word switch in the action to fan.
That is also a service, fan.turn_off / fan.turn_on under action
Then use your fan entity. It will use the default fan speed. If you want to change fan speeds, you will have to get into a little more coding.
Also change the domain in the top from switch to fan.

tnx ive editted the blueprint in \config\blueprints\automation\dondochaka

edit 2x the word switch to fan, that seemed to work

Thanks, great contribution.

Is it possible to make the Humidity sensor a multi device selector. I have two bathrooms that use the one fan. So I want to turn the fan on if one of the Humidity Sensor matches the criteria.

Can you make this turn on a Fan when the outside temperature is above a certain degree?
Like if its 25c outside, turn on a loft fan?
Turn back off once outside temp comes down to like 22c or something?