Skip to content

Instantly share code, notes, and snippets.

@tcastberg
Last active August 2, 2017 08:41
Show Gist options
  • Save tcastberg/804d8f20e764ca55facaae3b3fb7fba6 to your computer and use it in GitHub Desktop.
Save tcastberg/804d8f20e764ca55facaae3b3fb7fba6 to your computer and use it in GitHub Desktop.
Control of extractor fan based on divergence from average humidity of wet-rooms.
--[[
%% properties
231 value
225 value
244 value
%% events
%% globals
--]]
--[[
I'm basing the trigger values on divergence from the average to deal with fluctuations in general air
humidity throughout the year. In winter the humidity will hover around 30% while in summer the humidity
can stay up around 50%. A drawback of this is if humidity is created in all rooms at the same time it
will not turn on the extractor. I'm considering moving the average to a global variable that is calculated
on a 24 hour sliding window using a json document as a queue stored in a variable. That would probably
create a more even and accurate average.
244 = Laundry
231 = Family Bathroom
225 = Master Bathroom
162 = Extraction fan
--]]
-- Set defaults
local deviceModifier = { ["231"] = 0, ["225"] = 0, ["244"] = 0 };
local avgHumidity = 0;
local divHumidity = 0;
local sumHumidity = 0
local countHumidity = 0
local fanSpeed = tonumber(fibaro:getValue(162, "value"));
local newFanSpeed = fanSpeed;
-- Create round function missing in Lua
local function round(val, decimal)
if (decimal) then
return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
else
return math.floor(val+0.5)
end
end
-- Locate all humidity sensors
local data = {type="com.fibaro.humiditySensor"}
local devices = fibaro:getDevicesId(data);
-- Calculate average humidity
for idx, deviceID in ipairs(devices) do
local roomID = fibaro:getRoomID(deviceID);
if (roomID == 9 or roomID == 11 or roomID == 13) then
local roomHumidity = tonumber(fibaro:getValue(deviceID, "value")) - deviceModifier[tostring(deviceID)];
sumHumidity = sumHumidity + tonumber(fibaro:getValue(deviceID, "value"))
countHumidity = countHumidity + 1
end
end
avgHumidity = sumHumidity/countHumidity
-- Find greates divergence from average
for idx, deviceID in ipairs(devices) do
local roomID = fibaro:getRoomID(deviceID);
if (roomID == 9 or roomID == 11 or roomID == 13) then
local roomHumidity = tonumber(fibaro:getValue(deviceID, "value")) - deviceModifier[tostring(deviceID)];
-- fibaro:debug("Device "..deviceID..", value "..roomHumidity..", divergence "..math.abs(roomHumidity - avgHumidity))
if divHumidity < math.abs(roomHumidity - avgHumidity) then
divHumidity = math.abs(roomHumidity - avgHumidity);
end
end
end
-- Determine fan speed depending on amount of divergence.
-- There are two "slack" areas on each side of middle speed to ensure we don't constantly trigger
-- speed changes on the extractor
if (divHumidity < 2.5 and fanSpeed ~= 0 ) then
newFanSpeed = 0
elseif (divHumidity >= 2.5 and divHumidity < 4 and fanSpeed == 99) then
newFanSpeed = 50
elseif (divHumidity >= 4 and divHumidity < 9 and fanSpeed ~= 50) then
newFanSpeed = 50
elseif (divHumidity >= 9 and divHumidity < 11 and fanSpeed == 0) then
newFanSpeed = 50
elseif (divHumidity >= 11 and fanSpeed ~= 99) then
newFanSpeed = 99
end
-- Set the fan speed
if (newFanSpeed ~= fanSpeed) then
fibaro:call(162, "setValue", newFanSpeed)
end
fibaro:debug("Avg humidity: "..round(avgHumidity,2)..", highest divergence: "..round(divHumidity,2)..". Old fan speed: "..fanSpeed..", new fan speed: "..newFanSpeed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment