Cart 0

Bathroom Fan Control - Detecting Humidity Spikes using PLEG

Posted by LiveHouse Automation on

Do your kids always forget to put the bathroom fan on, resulting in the walls dripping with water after they take a shower? In this blog we show you how to detect a % change in Humidity using PLEG - so you can identify when the shower is in use and activate the bathroom extractor fan.

As the humidity level will naturally vary, you want to be able to see if it changes a lot in a short space of time, rather than simply goes above a threshold level. This rather busy graph from Emoncms shows what we're trying to capture.

The Yellow line is tracking humidity. You can see the two spikes where it suddenly jumps up, peaks and drops at a lower rate. This is the shower being used, twice on this morning. The red line is the light level in the bathroom - it provides a nice cross reference to the use of the heat lamps, indicating when someone turned them on before having a shower and turned them off afterwards.

This is a great example of how a little bit of home automation can eliminate a source of frustration, arguments and having to repaint walls. For this project you will need:

First up, for this project you'll need to power your Multisensor via USB. The reason is we're going to be updating the humidity reading every 60 seconds - this will run down the batteries rather quickly. When your electrician is wiring in the fan relay, get them to put a GPO near to where the sensor will go and you can power it with a USB adaptor. The Recessor bracket for the Multisensor makes this an easy and tidy job.

Tangent: Using the Multisensor you can also control lights based on motion or how dark it is and heat lamps based on temperature.

After you've got everything paired to Vera and wired in, you'll need to set some parameters on the Multisensor so it will send updates of all measurements every 60 seconds. (101 and 111 parameters)

 Over in PLEG you need to set up a few bits of logic. 

  • Automatically turn the fan off after it's been turned on (I let it run for 30 minutes).
  • A schedule to check the Humidity value periodically (3 minutes works well). The schedule is used in the conditions so that they 'run' every 3 minutes.
  • A condition to store the last Humidty value for comparison.
  • A condition to calculate how much the Humidity value has changed.
  • A condition to calculate if that change represents a 5% change since the last calculation, which will trigger the fan.


The PLEG nitty gritty is below. For those who aren't familiar with PLEG, don't be alarmed by what looks to be complex. Setting this up using PLEG's user friendly editor is really easy, mostly point and click and much simpler than trying to write LUA code. Alternately contact us and we can set this up on your Vera remotely for you.

(Thanks to Richard T Schaeffer, author of PLEG for his help with this):


IXL_Fan_On Whenever the IXL Fan is turned on



sBath1_Humidity_Sample Interval 00:03:00 None Interval 00:00:01 None



pBath1_Humidity Bathroom (Humidity)[940] CurrentLevel[urn:micasaverde-com:serviceId:HumiditySensor1]



iXL_Fan_Auto_Off IXL_Fan_On; NOW > 30:00


cBath1_Humidity_DeltaValue pBath1_Humidity > c_Bath1_Humidity_LastValue ? ( pBath1_Humidity - c_Bath1_Humidity_LastValue) : (c_Bath1_Humidity_LastValue - pBath1_Humidity )
cBath1_Humidity_BigChange sBath1_Humidity_Sample and (cBath1_Humidity_DeltaValue > ( pBath1_Humidity * 5 / 100))
c_Bath1_Humidity_LastValue sBath1_Humidity_Sample ? pBath1_Humidity : c_Bath1_Humidity_LastValue


Quick English translation of the above.

DeltaValue - calculates the absolute value difference between the current value, and LastValue which was calculated 3 minutes ago.

BigChange - every 3 minutes, check to see if DeltaValue differs by 5% from the current value.

LastValue: every 3 minutes, update last value to the current Humidity reading, otherwise leave LastValue as is.


Action: cBath1_Humidity_BigChange

Device Actions:

Device Action Arguments
IXL Fan[676] SetTarget newTargetValue=1


Action: iXL_Fan_Auto_Off

Device Actions:

Device Action Arguments
IXL Fan[676] SetTarget newTargetValue=0


Easy huh?

So now you have Vera looking at the Humidty in the bathroom and if it changes by more than 5% in a 3 minute window, it will trigger the extractor fan if it is off. I've been running this for a couple of months now and it very reliably detects the showers being used within a couple of minutes and there have been no false triggers as a result of the humidity levels naturally varying during the day.

Or, for a bit of fun change the logic so that it turns the lights off whenever the significant other is taking a shower. She will laugh and laugh, thinking it's hilarious and you're so clever when she's standing there in the dark getting sprayed with water.

Seriously though, don't do that. Ever.

This method can also be used for rate of change detection in temperature, light and electricity use. I use it for looking at the power consumption from my 3 phase supply, if all phases increase by 1000W inside a minute - it means the ducted airconditioner is running. This lets me track use (and automatically turn it off when people leave and forget it's running).

Happy Automating (instead of cleaning the bathroom walls of runoff and mould).




Share this post

← Older Post Newer Post →

Leave a comment