Domoticz - Handle your exhaust speed according to humidity level


dropcap domoticz

Nowadays, a Mechanical Controlled Ventilation (VMC in French) is a de-facto equipment in a bathroom.

Most of these Controlled Ventilation can run in two different speeds : low speed and high speed. VMC runs low speed any time and can be switched to high speed with a manual switch.

Thanks to home automation, it is now possible to automatically manage your Controlled Ventilation speed according to humidity level in the room.

This article explains how to automatically manage a Controlled Ventilation speed according to humidity level in a bathroom with the help of :

  • a wireless humidity sensor
  • a wireless switch
  • a wireless gateway
  • Domoticz, a very nice Open Source Home Automation Software

This setup has been tested on a Debian 8 Jessie server with a RfxTrx433 gateway, an Oregon Scientific THGR 122 Temperature/Humidity sensor and a DiO 54755 switch. All RF devices are working on 433 MHz frequency. But any other similar setup and devices should work as long as they are compatible with Domotiz.

1. Principle

The automation setup can be described like this :

domoticz schema

Basic principle of this setup is very simple :

  • Controlled Ventilation runs all the time in low speed
  • When humidity level reaches a maximum acceptable level, Controlled Ventilation is switched to high speed
  • When humidity level goes down to a minimum acceptable level, Controlled Ventilation is switched back to low speed

Domoticz is in charge of periodically collecting humidity level and switching on/off Controled Ventilation high speed according to pre-defined levels.

To avoid permanent switching, both minimum and maximum acceptable humidity levels should be differents to provide a proper hysteresis.

2. Domoticz

In Domoticz, Mechanical Controlled Ventilation speed will be managed by a simple LUA script.

This script will use some User variables to set target humidity levels.

2.1. Humidity levels

First step is to define Minimum and Maximum acceptable humidity levels as User variables in Domoticz.

You'll then be able to change levels straight from Domoticz web interface without need to modify the script.

Both levels are defined in % and should have a minimum difference of 2% for a proper hysteresis effect :

  • Bathroom Humidity Min = 48
  • Bathroom Humidity Max = 52

I propose 48% and 52% levels, but they must be adjusted.

They may depend on your location and its average humidity level.

domoticz variable humidity

2.2. LUA script

Main control job will be done by a LUA script.

This script should belong to the time controlled script family to be triggered every minute by Domoticz daemon.

It is in charge of few actions :

  1. Retrieve current min and max acceptable humidity levels from User variables
  2. Read current hygrometer level
  3. Log actual condition
  4. Switch on VMC high speed switch when humidity level is above maximum acceptable level
  5. Switch off VMC high speed switch when humidity level is below minimun acceptable level

In a typical Domoticz setup, LUA script are placed under /opt/domoticz/scripts/lua directory.

As script is a time controlled script, it should follow script_time_whatevernameyouwant.lua naming convention.

-- Script to manage an exhaust FAN according to hygrometer level
-- Devices are :
--  * DEVICE_HYGROMETER : humidity meter
--  * DEVICE_SWITCH     : switch to control VMC high speed
-- Variables are :
--  * USER_TARGET       : Target humidity level (in %, around 50)
--  * USER_HYSTERESIS   : Hysteresis to switch on/off (in %, usually 2)

commandArray = {}

-- declarations

-- devices
local ROOM_NAME         = 'Bathroom'
local DEVICE_HYGROMETER = 'Bathroom Sensor'
local DEVICE_SWITCH     = 'Bathroom VMC'

-- user variables
local USER_TARGET       = 'Bathroom Humidity level'
local USER_HYSTERESIS   = 'Bathroom Humidity hysteresis'

-- acquisition

-- hygrometer
hygro_level = otherdevices_humidity[DEVICE_HYGROMETER]

-- vmc
vmc_state = otherdevices[DEVICE_SWITCH]

-- action

-- check if the sensor is on or has some weird reading
if (hygro_level == 0 or hygro_level == nil) then

    -- error
    print(DEVICE_HYGROMETER .. ' - Skipping reading as level is 0 or nil')


    -- read target levels
    hygro_target = tonumber(uservariables[USER_TARGET])
    hygro_hysteresis = tonumber(uservariables[USER_HYSTERESIS])

    -- calculate min and max levels
    hygro_target_on = hygro_target + hygro_hysteresis
    hygro_target_off = hygro_target - hygro_hysteresis

    -- log
    print(ROOM_NAME .. ' - Humidity ' .. hygro_level .. '% and VMC ' .. vmc_state .. ' (target ' .. hygro_target .. '%)')

    -- if VMC should start
    if (vmc_state == 'Off' and hygro_level >= hygro_target_on) then

        -- log
        print(DEVICE_SWITCH .. ' - Switching On [max humidity ' .. hygro_target_on .. '% reached]')

        -- switch on
        commandArray[DEVICE_SWITCH] = 'On'

    -- else, if VMC should stop
    elseif (vmc_state == 'On' and hygro_level <= hygro_target_off) then

        -- log
        print(DEVICE_SWITCH .. ' - Switching Off [min humidity ' .. hygro_target_off .. '% reached]')

        -- switch off
        commandArray[DEVICE_SWITCH] = 'Off'


return commandArray

To install the script, you can simply download it from my Github repository.

# wget -O /opt/domoticz/scripts/lua/script_time_whatevernameyouwant.lua
# chmod +x /opt/domoticz/scripts/lua/script_time_whatevernameyouwant.lua

You should next configure both DEVICE_HYGROMETER and DEVICE_SWITCH according to their name in your Domoticz setup.

2.3. Restart Domoticz

Everything is now ready for Domoticz to control your bathroom humidity level.

You just need to restart Domoticz service.

# service restart domoticz

You will start to see some logs from your new LUA script.

2017-03-09 23:42:00.162 LUA: SdB Front Sensor level is 49%
2017-03-09 23:42:00.164 LUA: SdB Back Sensor level is 57%
2017-03-09 23:43:00.187 LUA: SdB Front Sensor level is 49%
2017-03-09 23:43:00.190 LUA: SdB Back Sensor level is 57%
2017-03-09 23:44:00.215 LUA: SdB Front Sensor level is 48%
2017-03-09 23:44:00.215 LUA: Switching SdB Front VMC OFF as humidity level down to 48% [min set to 48%]
2017-03-09 23:44:00.215 EventSystem: Script event triggered: /opt/domoticz/scripts/lua/script_time_hygro_bathroom_front.lua
2017-03-09 23:44:00.218 LUA: SdB Back Sensor level is 57%
2017-03-09 23:45:00.241 LUA: SdB Front Sensor level is 48%

Your bathroom humidity level is now under control !


Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus