Bathroom dehumidifying using a floating humidity threshold

Blog: Bathroom dehumidifying using two sensors and a floating humidity threshold.

Link to the original blog post with a nice text/pictures layout.

The problem:

The bathroom fan is running too long if it’s raining outside.

A year ago I automated the dehumidifying of the bathroom using an extraction fan, Sonoff mini switch, Xiaomi Mi BLE thermometer/humidity sensor and HomeAssistant.

It works great, except in humid and rainy weather. In such cases, the fan runs too long, because it can not reach the manually set humidity threshold.

The solution:

A moving humidity threshold using a second humidity sensor.

HW parts:


The process:

1. I resolved how to connect Sonoff ZBMINI to the fan. The fan has 4 connectors: 1. ground, 2. neutral, 3. Phase 1 (power), 4. Phase 2 (a signal to turn it on). Until I connected the Sonoff switch, I used a physical switch to turn the fan on.

I connected the Sonoff switch in parallel to the physical switch. This way I can still use the physical switch to turn the fan on (in case Home Assistant is not working).

The schematics/doodles:

zbmini switch and the wiring schematics for extractor fan

The fan enclosure (without Sonoff):

the extractor fan, cover removed

The Sonoff switch is so small it nicely fits the fan enclosure:

the  extractor fan without cover, sonoff zbmini connected

3. I connected Sonoff to my Zigbee network. I had some issues with the range because the bathroom is 10m away from the Zigbee router. Then I added some zigbee smart plugs (routers) nearby to strengthen the mesh.

4. Then I set up Xiaomi Mi BLE sensors (one in the bathroom, one in the hallway) and ESP32 BLE proxy. Will skip the steps here, maybe this will be another blog post. Nevertheless, any humidity sensor connected to Home Assistant would work.

Home Assistant

5. I’ve set up a generic hygrostat entity:

home assistant generic hygrostat screenshot

 The code for it (it should be defined manually in configuration.yaml):

6. I’ve added the automation for setting a floating humidity threshold:

The automation works like this: It has 2 triggers. The 1st trigger fires if the state of the humidity sensor located in the hallway changes. The 2nd trigger (which is probably not needed) fires every 30 minutes (just to be sure).

Then the target humidity in generic hygrostat (HA) is set to: hallway humidity + 8%.

Why 8% difference? Don’t know, I set it using trial-and-error and it works ok.

home assistant screenshot, automation of the floating humidity threshold

The code for target humidity needs to be added manually to the automation yaml:


Here’s the example of automation in action. The left trigger (humidity sensor state change) fired:

home assistant log of the automation execution

The result:

The result: a comparison of the fixed vs. floating humidity threshold can be observed on the chart below.

If the generic hygrostat entity in HA is set to a fixed value (e. g. 60%), the fan runs unnecessarily long and it can’t reach the target humidity, see red (1) on the chart below.

If the target humidity adjusts according to the general humidity in the house, it runs less time (2).

The chart below shows the switch from the fixed humidity threshold to floating threshold (21 May around 18:00). See the blue horizontal line (threshold) and purple line (current bathroom humidity). Light blue vertical bars represent when the fan is running:

home assistant chart of bathroom humidity, calculated humidity threshold and indication when the fan is running

Conclusions/Key takeaways

  • it took me more than a year to fix this annoying issue of the fan running at night,
  • at the end it was easier to set up the automation than I thought.


The links to the products are not affiliate links and I don’t receive any compensation for linking.

The code and the ideas are mostly from HomeAssistant and ESPHome community forums.

Hashtags: #esphome #esp32 #homeassistant #diy



, , , , ,



2 responses to “Bathroom dehumidifying using a floating humidity threshold”

  1. Full Metal Archeopterix Avatar

    Very nice! I could use something like this in my house… too many people using one shower

    Of, course, my current fan is underpowered for the job, so I really should fix that first 😕

    #HomeAssistant #ESP32Home #esphome #esp32 #homeassistant #diy #humidity #bathroom #bathroom #humidity

    1. Tomi the Slav and 1024 others Avatar

      @DelilahTech @tomi Thank you! The contractor drilled a 15 cm (diameter) hole through the wall for the fan and it is sufficient.

Leave a Reply

Your email address will not be published. Required fields are marked *