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

Hi, I hope the original author doesn’t mind, but I modified the original blueprint.

His blueprint doesn’t take into account user intervention in the case of a manual fan button. With the addition of a helper input_boolean, I was able to make it so that the automation will only turn off the fan if the fan was turned on by the automation.

Essentially, what was happening is that my fan was turning on and off based on humidity, but if I went in the bathroom later and turned on the fan, it would shut off a short while later because the humidity differential wasn’t present. Now when the automation activates, it sets an input_boolean to true, then when the automation is ready to shut off, it checks that the input_boolean is true, if it is, it shuts off the fan and changes the input_boolean to false. Then if I come along later and turn the fan back on, the automation will try to shut it off because the humidity of the two rooms is the same, but it can’t because it won’t pass the input_boolean conditional.

If you’d like to use this blueprint you need to setup a helper under Configuration → Helpers → + Add Helper → Toggle. You’ll need to select that toggle when creating an automation from this blueprint.

4 Likes

Thanks for this input. I have been out of this space for the spring and summer as I use my automated grow space for Fall/Winter due to the climate. So I am just getting ready to get set back up for this season and came across this. You make a really good point. I think I am going to add another sensor for the intake air. I found this post on doing the conversion in javascipt so I may look into that in my code and update it.

2 Likes

That sounds like a really easy thing to setup if you already have a sensor for air quality and a way to control the fan speed. Let me know if this is still an issue for you and I can try and assist with it.

Sounds good let us know if you create a BP based on this and I will definitely use it for my attic fans. Winter will soon be here and I need a good BP for controlling the humidity in the attic.

I stumbled across your post some time ago. I have a similar project going on: reduce humidity in our basement by using a fan. I use the openweathermap integration to get the outside dew point data and I use a “D1mini”, Humidity-Sensor and a relay to get inside dew point and switch on/off the fan.
At the beginning, I wanted to use rednote as well, but was unable to get my head araound HomeAssistant and RedNote hat the same time… :slight_smile: I decided to find a HomeAssistant-only solution.
I was struggling with the automation, but here you can have a look at my “work in progress”, if you are interested: Wait_template does not work

Great automation. Is there a way to add a delay to turn the fan off? Let’s say after 10 minutes or so.

1 Like

Great idea for an automation. I my opinion it would be better to base the decision on the absolute humidity. e.g. If you want to vent your cellar or garage because of they are too humid, you only want to turn the fan on when the outside absolute humidity is lower that in your cellar. Thats because of the temperature difference. It could be that it is quite warm outside and a medium rel. humidity, when you pump that air into the cellar, it cools down and the rel. humidity increases (cool air can hold less water than warm air) and worst case you introduce even more humidity into the cellar.

Absolute humidity can easily be calculated from temperature and rel. humidity. See GitHub - dolezsa/thermal_comfort: Thermal Comfort sensor for HA (absolute humidity, heat index, dew point, thermal perception)

Somebody has already done the work :slight_smile: It works perfect for me.

2 Likes

@Hagan, @dondochaka, @mattbruman & @viper33802 -
Maybe I am late to the party, but wouldn’t the built-in Mold Indicator - Home Assistant (home-assistant.io) does essentially the same thing?

The setup accounted for indoor humidity, reference (outdoor) humidity, the temperature, an adjustable tolerance factor, and then gets you a %.

You can then use the values to turn on/off fans or vents or dehumidifiers, etc. via simple automation, however you like, with whatever other conditions (eg in the evening only) you might have.

2 Likes

Hi

Answering after half year - I’m a hero :smiley:

Yes, I can try and test it of course if you are able to modify?

Hi trying to make this blueprint work for me. To turn my fan on I use the service fan.turn_on

I have tried to adjust the blueprint to work with my fan_switch.

Any idea what I missing?

I new to Hass so still learning.

edited blueprint

trigger:
- entity_id: !input 'humidity_sensor'
  platform: state
- entity_id: !input 'reference_humidity_sensor'
  platform: state
condition:
- condition: template
  value_template: '{{ mode != fan_switch }}'
action:
- service: fan.turn_{{mode}}
  entity_id: !input 'fan_switch'
variables:
  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 fan_switch == ''off'' and difference|float > rising_threshold|float
    %}on{% elif fan_switch == ''on'' and difference|float > falling_threshold|float
    %}on{% else %}off{% endif %}'
mode: single

log says

2022-08-02 13:21:21 ERROR (MainThread) [homeassistant.components.automation] Blueprint Humidity Management generated invalid automation with inputs OrderedDict([(‘reference_humidity’, ‘60’), (‘fan_switch’, ‘binary_sensor.fan.forcering’), (‘humidity_sensor’, ‘sensor.netatmo_stro_vardagsrum_badrum_humidity’), (‘reference_humidity_sensor’, ‘sensor.netatmo_stro_vardagsrum_humidity’), (‘rising_threshold’, 8), (‘falling_threshold’, 5)]): not a valid value for dictionary value @ data[‘action’][0][‘entity_id’]. Got None

If you ever make any updates to this blueprint in the future, could you add an additional output domain. My fan switch is a mini smart switch, but the domain that the manufacturers assigned it to was “light”. I was able to get the blueprint to work by editing out “switch” and replacing it with “light” in two places, but every time there is a future update, the blueprint will be broken for me, and i will have to go in and re-edit it. image

1 Like

i made chat GPT make these edits to make the original blueprint work for 2 bathrooms with both each own

blueprint:
  name: Humidity Management
  description: Turn a fan on and off based on the difference between humidity sensors and a baseline
  domain: automation
  input:
    humidity_sensor_bathroom1:
      name: Humidity Sensor for Bathroom 1
      description: A sensor that measures the humidity of the first bathroom
      selector:
        entity:
          domain: sensor
          multiple: false
    humidity_sensor_bathroom2:
      name: Humidity Sensor for Bathroom 2
      description: A sensor that measures the humidity of the second bathroom
      selector:
        entity:
          domain: sensor
          multiple: false
    reference_humidity:
      name: Reference Humidity
      description: A percentage point value that indicates the baseline humidity if there is no reference sensor available
      default: 60
    reference_humidity_sensor:
      name: Reference Humidity Sensor
      description: A sensor that indicates the baseline humidity of the location
      selector:
        entity:
          domain: sensor
          multiple: false
      default: []
    fan_switch:
      name: Fan Switch
      description: A switch that turns the fan on and off
      selector:
        entity:
          domain: switch
          multiple: false
    rising_threshold:
      name: Rising Threshold
      description: How many percentage points above the reference humidity the sensor can rise before the fan is turned on
      selector:
        number:
          min: 0.0
          max: 100.0
          step: 1.0
          mode: slider
      default: 8
    falling_threshold:
      name: Falling Threshold
      description: How many percentage points above the reference humidity the sensor must fall to before the fan is turned off
      selector:
        number:
          min: 0.0
          max: 100.0
          step: 1.0
          mode: slider
      default: 3
  source_url: https://community.home-assistant.io/t/turn-a-fan-on-and-off-based-on-the-difference-between-a-humidity-sensor-and-a-baseline/255999
trigger:
- platform: state
  entity_id: !input humidity_sensor_bathroom1
- platform: state
  entity_id: !input humidity_sensor_bathroom2
- platform: state
  entity_id: !input reference_humidity_sensor
condition:
- condition: template
  value_template: '{{ mode != switch_state }}'
action:
- service: switch.turn_{{mode}}
  entity_id: !input fan_switch
variables:
  reference_humidity: !input reference_humidity
  humidity_sensor_bathroom1: !input humidity_sensor_bathroom1
  humidity_sensor_bathroom2: !input humidity_sensor_bathroom2
  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_bathroom1: '{{ states(humidity_sensor_bathroom1)|float - (states(reference_humidity_sensor)|float or reference_humidity|float) }}'
  difference_bathroom2: '{{ states(humidity_sensor_bathroom2)|float - (states(reference_humidity_sensor)|float or reference_humidity|float) }}'
  mode: >
    {% set differences = [difference_bathroom1|float, difference_bathroom2|float] %}
    {% if differences[0] > rising_threshold and differences[1] > rising_threshold %}
      on
    {% elif differences[0] < falling_threshold and differences[1] < falling_threshold %}
      off
    {% elif differences[0] > differences[1] %}
      on
    {% elif differences[0] < differences[1] %}
      off
    {% else %}
      {{ states(fan_switch) }}
    {% endif %}


these are the edits it made

  1. Added a new input for the second humidity sensor:
humidity_sensor_bathroom2:
  name: Humidity Sensor for Bathroom 2
  description: A sensor that measures the humidity of the second bathroom
  selector:
    entity:
      domain: sensor
      multiple: false

  1. Added a new trigger for the second humidity sensor:
- platform: state
  entity_id: !input humidity_sensor_bathroom2
  1. Modified the action section to compute the mode based on the differences of both sensors:
mode: '{% set differences = [difference_bathroom1|float, difference_bathroom2|float] %}
    {% if switch_state == ''off'' and differences|max > rising_threshold|float %}on{% elif switch_state == ''on'' and differences|max <= falling_threshold|float %}off{% else %}{{ switch_state }}{% endif %}'
  1. Modified the variable section to add the second humidity sensor and compute its difference:
humidity_sensor_bathroom2: !input humidity_sensor_bathroom2
difference_bathroom2: '{{ states(humidity_sensor_bathroom2)|float - (states(reference_humidity_sensor)|float or reference_humidity|float) }}'

These changes allow the automation to monitor both humidity sensors and turn the fan on or off based on the higher of the two differences.

3 Likes