Automated Bathroom Exhaust Fan using Humidity Sensor

This post may contain affiliate links. Please read my disclaimer for more info.

The bathroom is a really great place to get started with Home Automation. You can automate your lights to dim when you have a late-night bathroom trip, make sure your curling iron is off when you leave the house, or what I’m doing today: turning on the bathroom exhaust fan when someone is showering.

I recently installed an Aeotec Z-Wave Multisensor in my bathroom (more on that in a future post) and wanted to create some useful automations with it in the bathroom. In today’s article, I’ll walk you through my process of collecting data about the humidity of my bathroom, some of my experimentation with Home Assistant trend sensors, and finally the automation I developed.

Plotting on Grafana

First off, I wanted to plot my humidity in Grafana for a week or so to see how humidity was changing throughout the day. If you follow my tutorials on InfluxDB and Grafana you can get this going in no time. Below is a graph showing humidity readings from the Aeotec Z-Wave Multisensor for about a week.

Grafana Humidity Readings

One thing you’ll notice right off the bat, is that humidify varies day to day. This means you can’t use a simple humidity threshold to trigger your automations. There are lots of variables going into what the humidity in your house is. You don’t want to run the fan nonstop to try and lower the humidity to a threshold you’ll never reach.

From the graph above, you’ll definitely notice some peaks where the shower started running and the humidity really ramped up. Even though that peak changed from day to day, it gave me hope we could successfully trigger off of these events.

Trend Sensors

Enter Home Assistant trend sensors. This binary sensor takes in the values from another sensor and determines the “trend” line for all the data points. So if the humidity is rising quickly, the trend sensor will have a larger gradient. You can think of the trend sensor as trying to calculate the slope of the data for a range of time.

The trend sensor allows you to define a range of times to use for determining the trend. To figure out what range would work best for me, I created some sensors for 1, 3 and 5 minutes. At this point, I didn’t care about the min_gradient I just wanted to start tracking the values Home Assistant calculated.

The trend sensors are just binary sensors that go “on” when the gradient they calculate reaches a threshold. They have a state attribute though that contains the most recent calculation. I wanted to plot this over some real data so I could tell what gradient would make a good threshold for my automations. So I created custom template sensors to use that state attribute.

I also added them to the InfluxDB component so that they were stored in my InfluxDB database which I could then visualizing using Grafana.

Analyzing the Graph

After making these new sensors I plotted the values over a few days. The green line still shows the humidity reading. The yellow and blue lines show the trend calculations for 3 and 5 minutes respectively.

Bathroom Trend LInes

What I’m looking for here is which trend sensor has a threshold most consistent with when are actually showering and the humidity is rising. In the graph above, the yellow line shows the current trend calculation (values are on the right-hand axis). You can see it reaches about 0.008 a few times whenever the humidity is moving up and down. You can see the humidity starts to rapidly rise between 5:55 and 6:10 right after the shower is started. The yellow trend line goes over 0.015 %/s during this rapid increase.

I decided to use the 3-minute trend sensor with a minimum gradient of 0.012 to trigger my automations. The 5 minute (blue line) in the graph also looks promising using a minimum gradient of 0.005 or so.

Automation

For more complex Home Assistant automations, I prefer writing them in AppDaemon. AppDaemon provides a python API to Home Assistant for writing automations. If you already know Python and have a complex idea for an automation that would be hard to express with YAML, give AppDaemon a try. I’ve got a tutorial on using it to write a Telegram bot.

Whenever humidity is rising rapidly in the bathroom (more than likely due to a shower) I want to make sure the fan is running. I want the fan to stop automatically once the humidity level has reached within 10% of the humidity when the fan started. If someone turns on the fan manually, I also want it to follow the same rules. Here’s the AppDaemon app I came up with:

The automation should be pretty straightforward. If someone manually turns on the fan or the trend sensor comes on, we turn on the fan, record the current humidity and start a timer. Whenever the humidity changes, we compare it against the original humidity to see if we get within the threshold. Once we’re back in the threshold we turn off the fan.

I also create a minimum on-time timer, so that if the fan comes on either by someone pressing it manually or by an automation, it at least stays on for 10 minutes before it can be turned back off. I didn’t want people manually turning on the fan just for the automation to turn it off a minute later with a new humidity reading.

And to run the app I pass in the Home Assistant entities it should use.

Conclusion

I hope you enjoyed my walkthrough of how I collected data and created an automated bathroom exhaust fan while people are showering. So far I’ve been happy with the Aeotec Multisensor 6 and this first automation I created using it. I think the bathroom is a great place to start with Home Automation and there are lots of useful automations you can create. I’ll be looking at using the motion sensor in the Aeotec Multisensor in a future article to automate the lighting.

If you liked this article, you might some of these other ones from my blog:

If you found this tutorial helpful, please consider supporting the blog by joining my mailing list, following the blog on social media or directly through Buy Me a Coffee. Thanks for reading!