Lua script for controlling humidity in the bathroom Topic is solved

Moderator: leecollings

harolds
Posts: 12
Joined: Saturday 22 April 2017 13:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by harolds »

I made an improved version of the basic script. It can be used on more then one bathroom at the same time and supports two sizes of bathrooms.

I hope you like it.

Code: Select all

-------------------------------------------------------------------------------
-- This script keeps track of the humidity in a bathroom and will active the
-- fan(s) when there was a rise in humidity within the first 5 minutes.
--
-- It is fully configurable by using the constantes below. It should not be
-- needed to change anything to the code itselfs.
--
-- It can be used for multiple bathrooms on one Domoticz server because
-- it uses 'variable' user variables blocks per bathroom. And it can be used
-- on small and large bathrooms. See SMALL_ROOM constante here below.
-- 
-- To set it up add the following user variables to your Domoticz:
-- * humCounter[SENSOR_NAME] (e.g.: humCounterBathroom)
-- * humidityTmin5[SENSOR_NAME]
-- * humidityTmin10[SENSOR_NAME]
-- * targetFanOffHumidity[SENSOR_NAME]
-- * fanMaxTimer[SENSOR_NAME]
-- * fanFollowsProgram[SENSOR_NAME]
-- 
-- Create one Lua - Time script per bathroom (e.g. 'house_fans', 'shower_fan')
-- Testing is done by setting the testHumidity[SENSOR_NAME] to a fake humidity value
-------------------------------------------------------------------------------

--
-- declare some constants and settings
--
-- true for little bath room, false for normal bathrooms
SMALL_ROOM = true
-- start the fans as soon as possible
DIRECT_START = false

if SMALL_ROOM then
    -- small bathroom (like 2x2 meter)
    -- time in minutes when a the script logic will happen
    SAMPLE_INTERVAL = 5                 
    -- rise in humidity needed for a bath that will trigger the fan
    FAN_DELTA_TRIGGER = 2               
    -- rise in humidity needed for a shower that will trigger the second fan
    FAN_DELTA_TRIGGER2 = 5              
    -- maximum amount of minutes the fan can be on, in case we never reach the target humidity
    FAN_MAX_TIME = 15                   
    -- ventilator goes off if target+offset is reached
    TARGET_OFFSET = 3                   
    -- exact device name of the switch turning on/off the ventilator 2 (itho fan box stand 2)
    FAN_NAME = 'Ventilator douche'      
    -- exact device name of the switch turning on/off the ventilator 2 (itho fan box stand 2)
    --FAN_NAME = 'Ventilator medium'      
    -- exact device name of the switch turning on/off the ventilator 3 (itho fan box stand 3)
    --FAN_NAME2 = 'Ventilator max'      
    -- exact device name of the humidity sensor
    SENSOR_NAME = 'Douche'              
else
    -- normal bathroom (like 4x4 meter)
    -- time in minutes when a the script logic will happen
    SAMPLE_INTERVAL = 5                 
    -- rise in humidity needed for a bath that will trigger the fan
    FAN_DELTA_TRIGGER = 3               
    -- rise in humidity needed for a shower that will trigger the second fan
    FAN_DELTA_TRIGGER2 = 5              
    -- maximum amount of minues the fans can be on, in case we never reach the target humidity
    FAN_MAX_TIME = 15                   
    -- ventilator goes off if target+offset is reached
    TARGET_OFFSET = 2                
    -- exact device name of the switch turning on/off the ventilator 2 (itho fan box stand 2)
    FAN_NAME = 'Ventilator max'      
    -- exact device name of the switch turning on/off the ventilator 2 (itho fan box stand 2)
    --FAN_NAME = 'Ventilator medium'      
    -- exact device name of the switch turning on/off the ventilator 3 (itho fan box stand 3)
    --FAN_NAME2 = 'Ventilator max'      
    -- exact device name of the humidity sensor 
    SENSOR_NAME = 'Badkamer'            
end    

-------------------------------------------------------------------------------
-- No changes needed below this lines
-------------------------------------------------------------------------------

commandArray = {}

-- adding info to the log
function addMsg(txt)
    if txt == '' or txt == nil then
        return
    end    
    if msg ~= '' then
        msg = msg .. '\n'
    end
    msg = msg .. txt .. '. '
end    

-- send a notification
function sendNotication(txt)
    if txt == '' or txt == nil then
        return
    end    
    commandArray['SendNotification'] = txt
end

-- switch on a device (with retry)
function switchOn(device, msg)
    if (otherdevices[device] == 'Off') then
        commandArray[#commandArray+1] = {[device] = "On"}
        -- just to be sure, wait 30s and try again
        commandArray[#commandArray+1] = {[device] = "On AFTER 30"}
        -- and again after 60s
        commandArray[#commandArray+1] = {[device] = "On AFTER 60"}
        if (msg ~= '') then
            addMsg(msg)
        end    
    end    
end    

-- switch off a device (with retry)
function switchOff(device, msg)
    if otherdevices[device] == 'On' then
        commandArray[#commandArray+1] = {[device] = "Off"}
        -- just to be sure, wait 30s and try again
        commandArray[#commandArray+1] = {[device] = "Off AFTER 30"}
        -- and again after 60s
        commandArray[#commandArray+1] = {[device] = "Off AFTER 60"}
        if (msg ~= '') then
            addMsg(msg)
        end    
    end    
end    

TEST_MODE_HUMVAR = 'testHumidity' .. SENSOR_NAME  -- fake humidity value, give it a test value in domoticz/uservars
PRINT_MODE = true                  -- when true wil print output to log and send notifications
 
-- get the global variables:
-- this script runs every minute, humCounter is used to create SAMPLE_INTERVAL periods
humCounter = tonumber(uservariables['humCounter' .. SENSOR_NAME])
humidityTmin5 = tonumber(uservariables['humidityTmin5' .. SENSOR_NAME])                -- youngest reading
humidityTmin10 = tonumber(uservariables['humidityTmin10' .. SENSOR_NAME])              -- oldest reading
targetFanOffHumidity = tonumber(uservariables['targetFanOffHumidity' .. SENSOR_NAME])  -- target humidity
fanMaxTimer = tonumber(uservariables['fanMaxTimer' .. SENSOR_NAME])
fanFollowsProgram = tonumber(uservariables['fanFollowsProgram' .. SENSOR_NAME])        -- marker indicating that the programm is runing for a bath

target = 0 -- will hold the target humidity when the program starts
 
msg = '' -- extra info to show in debug/print mode

-- get the current humidity value
if (tonumber(uservariables[TEST_MODE_HUMVAR]) > 0) then
    current = tonumber(uservariables[TEST_MODE_HUMVAR])
else
    current = otherdevices_humidity[SENSOR_NAME]
end
 
-- check if the sensor is on or has some weird reading
if (current == 0 or current == nil) then
    print('Current humidity is 0 or nil. Skipping this reading')
    return commandArray
end
 
if (fanFollowsProgram == 0 and DIRECT_START) then
    humCounter = SAMPLE_INTERVAL    
    addMsg('Direct start is active')
else
    -- increase cycle counter (one run per minute)
    humCounter = humCounter + 1
end    
 
-- start the checks after the sample interval was reached (or direct start was enabled)
if (humCounter >= SAMPLE_INTERVAL) then
 
    if (humidityTmin5 == 0) then
        -- initialization, assume this is the first time
        humidityTmin5 = current
        humidityTmin10 = current
    end
 
    humCounter = 0 -- reset the cycle counter
 
    -- pick the lowest history value to calculate the delta
    -- this also makes sure that two relative small deltas in the past 2*interval minutes are treated as one larger rise
    -- and therefore will still trigger the ventilator
    -- I don't want to use a longer interval instead because I want the ventilator to start as soon as possible
    -- (so rather after 5 minutes instead of after 15 minutes because the mirrors in the bathroom become kinda useless ;-)
    delta = current - math.min(humidityTmin10, humidityTmin5)

    -- pick the lowest history value
    target = math.min(humidityTmin10, humidityTmin5) + TARGET_OFFSET
 
    -- shift the previous measurements
    humidityTmin10 = humidityTmin5
    -- and store the current
    humidityTmin5 = current
 
    -- either the fan is off or it is on but the decrease program has not started
    -- in that latter case we start the program anyway. This could happen if someone turns on the ventilator
    -- manually because he/she is about to take a shower and doesn't like damp mirrors.
    -- I don't do this because the ventilator removes heat from the bathroom and I want this to happen
    -- as late as possible ;-)
    if (otherdevices[FAN_NAME] == 'Off' or (otherdevices[FAN_NAME] == 'On' and fanFollowsProgram == 0)) then
 
        -- likely someone turned off the ventilator while the program was running
        if (fanFollowsProgram == 1 and otherdevices[FAN_NAME] == 'Off') then
            fanFollowsProgram = 0
        end
 
        -- see if we have to turn it on
        if (delta >= FAN_DELTA_TRIGGER) or (delta >= FAN_DELTA_TRIGGER2) then
            -- time to start the fan
            switchOn(FAN_NAME, 'Rise in humidity, turning on the fan, delta ' .. delta)

            -- we need to turn the second fan on for the shower (or large bath :)
            if (delta >= FAN_DELTA_TRIGGER2) then
                switchOn(FAN_NAME2, 'Large rise in humidity, turning on the second fan, delta ' .. delta)
            end
    
            targetFanOffHumidity = target
            fanFollowsProgram = 1
 
            -- set the safety stop
            fanMaxTimer = math.floor(FAN_MAX_TIME / SAMPLE_INTERVAL) - 1
 
            addMsg('Target humidity for switching off the fans: ' .. targetFanOffHumidity)
            sendNotication(FAN_NAME .. ' fan is on#The fan is activated at humidity ' .. current .. ', targetHumidity ' .. targetFanOffHumidity .. ', delta ' .. delta .. '#0')
        end

    else
    
        if (fanMaxTimer > 0) then
            -- possible that someone started the fan manually
            fanMaxTimer = fanMaxTimer - 1
        end
 
        if (fanFollowsProgram == 1) then -- not manually started
 
            -- ok, there is another FAN_DELTA_TRIGGER rise in humidity
            -- when this happen we reset the fanMaxTimer to a new count down
            -- because we have to ventilate a bit longer (and faster) due to the extra humidity
            if (delta >= FAN_DELTA_TRIGGER) then
                addMsg('Another large increase detected, resetting max timer, delta: ' .. delta)
                fanMaxTimer = math.floor(FAN_MAX_TIME / SAMPLE_INTERVAL) - 1
                -- and start the second fan
                switchOff(FAN_NAME2)
            end
        
            -- if humidity delta decrease is more then second trigger we can
            -- switch off the second fan
            if (delta < -FAN_DELTA_TRIGGER2) then
                switchOff(FAN_NAME2)
            end
 
            -- first see if it can be turned off
            if (current <= targetFanOffHumidity or fanMaxTimer == 0) then
                if (otherdevices[FAN_NAME2] == 'On') then
                    addMsg('The fans are deactivated at humidity ' .. current) 
                else
                    addMsg('The fan is deactivated at humidity ' .. current) 
                end

                switchOff(FAN_NAME)
                switchOff(FAN_NAME2)

                if (fanMaxTimer == 0 and current > targetFanOffHumidity) then
                    addMsg(', target humidity ' .. targetFanOffHumidity .. 
                        ' not reached. Safety time-out activated (' .. FAN_MAX_TIME .. '-' .. FAN_MAX_TIME + SAMPLE_INTERVAL .. ' min)')
                else
                    addMsg(', target humidity ' .. targetFanOffHumidity .. ' reached')
                end

                targetFanOffHumidity = 0
                fanMaxTimer = 0
                fanFollowsProgram = 0
                -- reset history in this case.. we start all over
                -- Tmin10 is still in the 'ventilator=On'-zone
                humidityTmin10 = humidityTmin5
                if PRINT_MODE == true then
                    sendNotication(FAN_NAME .. ' fan is off#' .. msg .. '#0')
                end
 
            else
                -- we haven't reached the target yet
                addMsg('Humidity delta: ' .. delta)
            end
        end
    end
    
    if PRINT_MODE == true then
        print('---')
        print('Checking humidity in ' .. SENSOR_NAME .. '...')
        print('Current humidity : ' .. current)
        print('humidityTmin5 : ' .. humidityTmin5)
        print('humidityTmin10 : ' .. humidityTmin10)
        print('fanMaxTimer : ' .. fanMaxTimer)
        print('humCounter : ' .. humCounter)
        print('targetFanOffHumidity : ' .. targetFanOffHumidity)
        print('fanFollowsProgram : ' .. fanFollowsProgram)
        print('fan 1 : ' .. FAN_NAME .. ' = ' .. otherdevices[FAN_NAME])
        --print('fan 2 : ' .. FAN_NAME2 .. ' = ' .. otherdevices[FAN_NAME2])
        
        if msg ~= '' then
            print('Extra info\n---\n' .. msg .. '\n---')
        end
    end

end


-- save the globals
commandArray['Variable:humCounter' .. SENSOR_NAME] = tostring(humCounter)
commandArray['Variable:humidityTmin5' .. SENSOR_NAME] = tostring(humidityTmin5)
commandArray['Variable:humidityTmin10' .. SENSOR_NAME] = tostring(humidityTmin10)
commandArray['Variable:targetFanOffHumidity' .. SENSOR_NAME] = tostring(targetFanOffHumidity)
commandArray['Variable:fanMaxTimer' .. SENSOR_NAME] = tostring(fanMaxTimer)
commandArray['Variable:fanFollowsProgram' .. SENSOR_NAME] = tostring(fanFollowsProgram)

return commandArray
Sundown89
Posts: 3
Joined: Saturday 10 June 2017 13:48
Target OS: Linux
Domoticz version: 3.7708
Location: Netherlands
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by Sundown89 »

Hi all,

Really like this script!

However, I control my home ventilation box (Buva Q-stream) with a KaKu ACM-LV10.
This functions like a dimmer, so it uses percentages to control the unit.
I can still turn it on and off, but I always keep it turned on at a low percentage, say 20%.
I already used the search function, but I did not find anybody with a similar setup.

Can someone help me implement these percentages in the script? A couple of lines I can manage, but this script is just too much for me. ;)
Like when de-humidifying is triggered, the box is set to 90-100% instead of the ON switch.
And when the target is reached, let it switch back to "normal", thus 20%.

Thanks in advance!
Domoticz on Ubuntu 14.04
Dashticz V2
Pi-Hole
RFXCom
Slorf
Posts: 26
Joined: Friday 20 December 2013 11:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Voorburg
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by Slorf »

Hi All,

We have 2 bathrooms, but one verntilation box which is also used for the kitchen,

The kitchen would need to have a manual switch, any idea how to get the script working for this setup ?

Thanks !!

Alex
A lot of stuff :[-)

Orange PI
Evohome with HGI-80
SmartMeter USB Cable
Solar panels
RFLINK 433 MHz
AeonLabs usb z-wave plus
My Sensors ( Lan Gateway )
Wemos sensors
Arduino sensors
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by tlpeter »

Hi Alex, I have a humidity sensor in the bathroom (i only have one) and i have two switches in the kitchen which can override the humidity sensor.
I use a switchselector to set the fan to 1,2 or 3 which are the only options for this fan.
So 10% is 1 and 20% is 2 and 30% is 3
I have had some help with this script so credits to Mivo for this script:

Code: Select all

local LOGGING = true

return {
    active = true,
    logging = {
        level = domoticz.LOG_INFO,
    },
    on = {
        timer = {'every minute'}
    },

    execute = function(domoticz)
        local bathroomSensor = domoticz.devices('Badkamer')
        -- store override devices to variables - for better readability
        local override1 = domoticz.devices('Ventilatie schakelaar stand 2').state
        local override2 = domoticz.devices('Ventilatie schakelaar stand 3').state
        -- ventilator device variable - for better readability and easier change device name in one place etc.
        local ventilator = domoticz.devices('Mechanische ventilatie')
        local ventNew = 10 -- variable for new ventilator level, default is 10
        local override = false -- helper variable for logging - set to true later if override active
        local ventCurrent = ventilator.level -- current level

        if override2 == 'On' then
                -- first check override2 - if both switches On, switch2 has higher priority -> higher ventilator
                override = true
                ventNew = 30 -- override to 30
        elseif override1 == 'On' then
                -- status override1 switch
                override = true
                ventNew = 20 -- override to 20
        else
        -- if not override, check humidity

                -- dotn check range 10-75, level defaults to -> 10
                -- if bathroomSensor.humidity >= 10 and bathroomSensor.humidity <= 75 then
                -- ventNew = 10
                if bathroomSensor.humidity >= 76 and bathroomSensor.humidity <= 85 then
                        ventNew = 20
                elseif bathroomSensor.humidity >=86 and bathroomSensor.humidity <= 100 then
                        ventNew = 30
                end
        end

        if LOGGING then
                if override then
                        domoticz.log('Override active to ' .. tostring(ventNew))
                end
        end

        if ventCurrent ~= ventNew then
        -- compare current level with new, and then switch or not
                domoticz.log('Changing level from: ' .. tostring(ventCurrent) .. ' to: ' .. tostring(ventNew))
                ventilator.switchSelector(ventNew)
        else
        -- not switching
                if LOGGING then
                        domoticz.log('Level is the same as before')
                end
        end
   end
}
R0yk3
Posts: 37
Joined: Sunday 24 July 2016 21:51
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by R0yk3 »

Thank You, works right out of the box!!!
Raspberry PI 3, raspbian, ZwaveMe, RFLink
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by tlpeter »

Very nice as everything situation is different.
arnoldg
Posts: 15
Joined: Tuesday 29 September 2015 20:35
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Almelo, netherlands
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by arnoldg »

Hi,

why is my script not running every minute, but every x seconds.

see my log file (Spoiler).
Spoiler: show
2017-09-02 10:47:25.680 LUA: Fan control
2017-09-02 10:47:25.681 LUA: Current humidity:56
2017-09-02 10:47:25.681 LUA: targetFanOffHumidity:0
2017-09-02 10:47:25.681 LUA: humidityTmin5: 56
2017-09-02 10:47:25.681 LUA: humidityTmin10: 56
2017-09-02 10:47:25.682 LUA: fanMaxTimer: 0
2017-09-02 10:47:25.682 LUA: humCounter:1
2017-09-02 10:47:25.682 LUA: fanFollowsProgram:0
2017-09-02 10:47:25.697 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:47:25.700 (Smart meter) P1 Smart Meter (Power)
2017-09-02 10:47:35.701 LUA: Fan control
2017-09-02 10:47:35.701 LUA: Current humidity:56
2017-09-02 10:47:35.701 LUA: targetFanOffHumidity:0
2017-09-02 10:47:35.701 LUA: humidityTmin5: 56
2017-09-02 10:47:35.702 LUA: humidityTmin10: 56
2017-09-02 10:47:35.702 LUA: fanMaxTimer: 0
2017-09-02 10:47:35.702 LUA: humCounter:2
2017-09-02 10:47:35.702 LUA: fanFollowsProgram:0
2017-09-02 10:47:35.717 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:47:35.721 (Smart meter) P1 Smart Meter (Power)
2017-09-02 10:47:45.702 LUA: Fan control
2017-09-02 10:47:45.703 LUA: Current humidity:56
2017-09-02 10:47:45.703 LUA: targetFanOffHumidity:0
2017-09-02 10:47:45.703 LUA: humidityTmin5: 56
2017-09-02 10:47:45.703 LUA: humidityTmin10: 56
2017-09-02 10:47:45.704 LUA: fanMaxTimer: 0
2017-09-02 10:47:45.704 LUA: humCounter:3
2017-09-02 10:47:45.704 LUA: fanFollowsProgram:0
2017-09-02 10:47:45.718 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:47:45.722 (Smart meter) P1 Smart Meter (Power)
2017-09-02 10:47:50.726 (Verwarming) Temp (Control Setpoint)
2017-09-02 10:47:50.730 (Verwarming) General/Percentage (Maximum Relative Modulation Level)
2017-09-02 10:47:51.130 LUA: Fan control
2017-09-02 10:47:51.130 LUA: Current humidity:56
2017-09-02 10:47:51.130 LUA: targetFanOffHumidity:0
2017-09-02 10:47:51.131 LUA: humidityTmin5: 56
2017-09-02 10:47:51.131 LUA: humidityTmin10: 56
2017-09-02 10:47:51.131 LUA: fanMaxTimer: 0
2017-09-02 10:47:51.131 LUA: humCounter:4
2017-09-02 10:47:51.131 LUA: fanFollowsProgram:0
2017-09-02 10:47:51.132 LUA: Delta: 0
2017-09-02 10:47:51.132 LUA: New values >>>>>>>>>>>
2017-09-02 10:47:51.132 LUA: humidityTmin5: 56
2017-09-02 10:47:51.132 LUA: humidityTmin10: 56
2017-09-02 10:47:51.133 LUA: fanMaxTimer: 0
2017-09-02 10:47:51.133 LUA: humCounter:0
2017-09-02 10:47:51.133 LUA: fanFollowsProgram:0
2017-09-02 10:47:51.133 LUA: ------ target: 0
2017-09-02 10:47:51.148 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:47:51.151 (Verwarming) Thermostat (Room Setpoint)
2017-09-02 10:47:51.156 (Verwarming) General/Percentage (Relative modulation level)
2017-09-02 10:47:51.552 LUA: Fan control
2017-09-02 10:47:51.553 LUA: Current humidity:56
2017-09-02 10:47:51.553 LUA: targetFanOffHumidity:0
2017-09-02 10:47:51.553 LUA: humidityTmin5: 56
2017-09-02 10:47:51.554 LUA: humidityTmin10: 56
2017-09-02 10:47:51.554 LUA: fanMaxTimer: 0
2017-09-02 10:47:51.554 LUA: humCounter:0
2017-09-02 10:47:51.554 LUA: fanFollowsProgram:0
2017-09-02 10:47:51.569 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:47:51.573 (Verwarming) Temp (Room Temperature)
2017-09-02 10:47:51.581 (Verwarming) Temp (Boiler Water Temperature)
2017-09-02 10:47:51.587 (Verwarming) Temp (DHW Temperature)
2017-09-02 10:47:51.594 (Verwarming) Temp (Outside Temperature)
2017-09-02 10:47:51.601 (Verwarming) Temp (Return Water Temperature)
2017-09-02 10:47:51.606 (Verwarming) Thermostat (DHW Setpoint)
2017-09-02 10:47:51.611 (Verwarming) Thermostat (Max_CH Water Setpoint)
2017-09-02 10:47:53.818 LUA: Fan control
2017-09-02 10:47:53.822 LUA: Current humidity:56
2017-09-02 10:47:53.822 LUA: targetFanOffHumidity:0
2017-09-02 10:47:53.822 LUA: humidityTmin5: 56
2017-09-02 10:47:53.823 LUA: humidityTmin10: 56
2017-09-02 10:47:53.823 LUA: fanMaxTimer: 0
2017-09-02 10:47:53.823 LUA: humCounter:1
2017-09-02 10:47:53.823 LUA: fanFollowsProgram:0
2017-09-02 10:47:53.838 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:47:55.722 LUA: Fan control
2017-09-02 10:47:55.722 LUA: Current humidity:56
2017-09-02 10:47:55.722 LUA: targetFanOffHumidity:0
2017-09-02 10:47:55.723 LUA: humidityTmin5: 56
2017-09-02 10:47:55.723 LUA: humidityTmin10: 56
2017-09-02 10:47:55.723 LUA: fanMaxTimer: 0
2017-09-02 10:47:55.723 LUA: humCounter:2
2017-09-02 10:47:55.723 LUA: fanFollowsProgram:0
2017-09-02 10:47:55.741 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:47:55.744 (Smart meter) P1 Smart Meter (Power)
2017-09-02 10:48:00.256 LUA: Fan control
2017-09-02 10:48:00.258 LUA: Current humidity:56
2017-09-02 10:48:00.258 LUA: targetFanOffHumidity:0
2017-09-02 10:48:00.258 LUA: humidityTmin5: 56
2017-09-02 10:48:00.259 LUA: humidityTmin10: 56
2017-09-02 10:48:00.259 LUA: fanMaxTimer: 0
2017-09-02 10:48:00.259 LUA: humCounter:3
2017-09-02 10:48:00.259 LUA: fanFollowsProgram:0
2017-09-02 10:48:00.274 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:05.737 LUA: Fan control
2017-09-02 10:48:05.737 LUA: Current humidity:56
2017-09-02 10:48:05.737 LUA: targetFanOffHumidity:0
2017-09-02 10:48:05.738 LUA: humidityTmin5: 56
2017-09-02 10:48:05.738 LUA: humidityTmin10: 56
2017-09-02 10:48:05.738 LUA: fanMaxTimer: 0
2017-09-02 10:48:05.738 LUA: humCounter:4
2017-09-02 10:48:05.738 LUA: fanFollowsProgram:0
2017-09-02 10:48:05.739 LUA: Delta: 0
2017-09-02 10:48:05.739 LUA: New values >>>>>>>>>>>
2017-09-02 10:48:05.739 LUA: humidityTmin5: 56
2017-09-02 10:48:05.739 LUA: humidityTmin10: 56
2017-09-02 10:48:05.740 LUA: fanMaxTimer: 0
2017-09-02 10:48:05.740 LUA: humCounter:0
2017-09-02 10:48:05.740 LUA: fanFollowsProgram:0
2017-09-02 10:48:05.740 LUA: ------ target: 0
2017-09-02 10:48:05.755 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:05.758 (Smart meter) P1 Smart Meter (Power)
2017-09-02 10:48:15.755 LUA: Fan control
2017-09-02 10:48:15.756 LUA: Current humidity:56
2017-09-02 10:48:15.756 LUA: targetFanOffHumidity:0
2017-09-02 10:48:15.756 LUA: humidityTmin5: 56
2017-09-02 10:48:15.756 LUA: humidityTmin10: 56
2017-09-02 10:48:15.756 LUA: fanMaxTimer: 0
2017-09-02 10:48:15.757 LUA: humCounter:0
2017-09-02 10:48:15.757 LUA: fanFollowsProgram:0
2017-09-02 10:48:15.771 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:15.774 (Smart meter) P1 Smart Meter (Power)
2017-09-02 10:48:16.172 LUA: Fan control
2017-09-02 10:48:16.173 LUA: Current humidity:56
2017-09-02 10:48:16.173 LUA: targetFanOffHumidity:0
2017-09-02 10:48:16.173 LUA: humidityTmin5: 56
2017-09-02 10:48:16.173 LUA: humidityTmin10: 56
2017-09-02 10:48:16.174 LUA: fanMaxTimer: 0
2017-09-02 10:48:16.174 LUA: humCounter:1
2017-09-02 10:48:16.174 LUA: fanFollowsProgram:0
2017-09-02 10:48:16.189 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:16.192 (Smart meter) P1 Smart Meter (Gas)
2017-09-02 10:48:20.727 (Verwarming) Temp (Control Setpoint)
2017-09-02 10:48:20.736 (Verwarming) General/Percentage (Maximum Relative Modulation Level)
2017-09-02 10:48:21.134 LUA: Fan control
2017-09-02 10:48:21.135 LUA: Current humidity:56
2017-09-02 10:48:21.135 LUA: targetFanOffHumidity:0
2017-09-02 10:48:21.135 LUA: humidityTmin5: 56
2017-09-02 10:48:21.136 LUA: humidityTmin10: 56
2017-09-02 10:48:21.136 LUA: fanMaxTimer: 0
2017-09-02 10:48:21.136 LUA: humCounter:2
2017-09-02 10:48:21.136 LUA: fanFollowsProgram:0
2017-09-02 10:48:21.151 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:21.154 (Verwarming) Thermostat (Room Setpoint)
2017-09-02 10:48:21.159 (Verwarming) General/Percentage (Relative modulation level)
2017-09-02 10:48:21.556 LUA: Fan control
2017-09-02 10:48:21.556 LUA: Current humidity:56
2017-09-02 10:48:21.556 LUA: targetFanOffHumidity:0
2017-09-02 10:48:21.557 LUA: humidityTmin5: 56
2017-09-02 10:48:21.557 LUA: humidityTmin10: 56
2017-09-02 10:48:21.557 LUA: fanMaxTimer: 0
2017-09-02 10:48:21.557 LUA: humCounter:3
2017-09-02 10:48:21.557 LUA: fanFollowsProgram:0
2017-09-02 10:48:21.572 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:21.576 (Verwarming) Temp (Room Temperature)
2017-09-02 10:48:21.583 (Verwarming) Temp (Boiler Water Temperature)
2017-09-02 10:48:21.590 (Verwarming) Temp (DHW Temperature)
2017-09-02 10:48:21.596 (Verwarming) Temp (Outside Temperature)
2017-09-02 10:48:21.603 (Verwarming) Temp (Return Water Temperature)
2017-09-02 10:48:21.608 (Verwarming) Thermostat (DHW Setpoint)
2017-09-02 10:48:21.612 (Verwarming) Thermostat (Max_CH Water Setpoint)
2017-09-02 10:48:25.759 LUA: Fan control
2017-09-02 10:48:25.760 LUA: Current humidity:56
2017-09-02 10:48:25.760 LUA: targetFanOffHumidity:0
2017-09-02 10:48:25.760 LUA: humidityTmin5: 56
2017-09-02 10:48:25.760 LUA: humidityTmin10: 56
2017-09-02 10:48:25.760 LUA: fanMaxTimer: 0
2017-09-02 10:48:25.761 LUA: humCounter:4
2017-09-02 10:48:25.761 LUA: fanFollowsProgram:0
2017-09-02 10:48:25.761 LUA: Delta: 0
2017-09-02 10:48:25.761 LUA: New values >>>>>>>>>>>
2017-09-02 10:48:25.761 LUA: humidityTmin5: 56
2017-09-02 10:48:25.762 LUA: humidityTmin10: 56
2017-09-02 10:48:25.762 LUA: fanMaxTimer: 0
2017-09-02 10:48:25.762 LUA: humCounter:0
2017-09-02 10:48:25.762 LUA: fanFollowsProgram:0
2017-09-02 10:48:25.762 LUA: ------ target: 0
2017-09-02 10:48:25.779 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:25.782 (Smart meter) P1 Smart Meter (Power)
2017-09-02 10:48:35.776 LUA: Fan control
2017-09-02 10:48:35.776 LUA: Current humidity:56
2017-09-02 10:48:35.777 LUA: targetFanOffHumidity:0
2017-09-02 10:48:35.777 LUA: humidityTmin5: 56
2017-09-02 10:48:35.777 LUA: humidityTmin10: 56
2017-09-02 10:48:35.777 LUA: fanMaxTimer: 0
2017-09-02 10:48:35.777 LUA: humCounter:0
2017-09-02 10:48:35.778 LUA: fanFollowsProgram:0
2017-09-02 10:48:35.792 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:35.795 (Smart meter) P1 Smart Meter (Power)
2017-09-02 10:48:45.815 LUA: Fan control
2017-09-02 10:48:45.815 LUA: Current humidity:56
2017-09-02 10:48:45.815 LUA: targetFanOffHumidity:0
2017-09-02 10:48:45.816 LUA: humidityTmin5: 56
2017-09-02 10:48:45.816 LUA: humidityTmin10: 56
2017-09-02 10:48:45.816 LUA: fanMaxTimer: 0
2017-09-02 10:48:45.816 LUA: humCounter:1
2017-09-02 10:48:45.816 LUA: fanFollowsProgram:0
2017-09-02 10:48:45.831 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:45.834 (Smart meter) P1 Smart Meter (Power)
2017-09-02 10:48:50.733 (Verwarming) Temp (Control Setpoint)
2017-09-02 10:48:50.741 (Verwarming) General/Percentage (Maximum Relative Modulation Level)
2017-09-02 10:48:51.139 LUA: Fan control
2017-09-02 10:48:51.139 LUA: Current humidity:56
2017-09-02 10:48:51.140 LUA: targetFanOffHumidity:0
2017-09-02 10:48:51.140 LUA: humidityTmin5: 56
2017-09-02 10:48:51.140 LUA: humidityTmin10: 56
2017-09-02 10:48:51.140 LUA: fanMaxTimer: 0
2017-09-02 10:48:51.140 LUA: humCounter:2
2017-09-02 10:48:51.140 LUA: fanFollowsProgram:0
2017-09-02 10:48:51.155 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:51.158 (Verwarming) Thermostat (Room Setpoint)
2017-09-02 10:48:51.163 (Verwarming) General/Percentage (Relative modulation level)
2017-09-02 10:48:51.563 LUA: Fan control
2017-09-02 10:48:51.564 LUA: Current humidity:56
2017-09-02 10:48:51.564 LUA: targetFanOffHumidity:0
2017-09-02 10:48:51.564 LUA: humidityTmin5: 56
2017-09-02 10:48:51.564 LUA: humidityTmin10: 56
2017-09-02 10:48:51.565 LUA: fanMaxTimer: 0
2017-09-02 10:48:51.565 LUA: humCounter:3
2017-09-02 10:48:51.565 LUA: fanFollowsProgram:0
2017-09-02 10:48:51.580 EventSystem: Script event triggered: script_time_humidity.lua
2017-09-02 10:48:51.584 (Verwarming) Temp (Room Temperature)
2017-09-02 10:48:51.590 (Verwarming) Temp (Boiler Water Temperature)
2017-09-02 10:48:51.597 (Verwarming) Temp (DHW Temperature)
2017-09-02 10:48:51.603 (Verwarming) Temp (Outside Temperature)
2017-09-02 10:48:51.610 (Verwarming) Temp (Return Water Temperature)
2017-09-02 10:48:51.615 (Verwarming) Thermostat (DHW Setpoint)
2017-09-02 10:48:51.619 (Verwarming) Thermostat (Max_CH Water Setpoint)
ekrd
Posts: 1
Joined: Friday 08 September 2017 15:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by ekrd »

Script taken from http://www.domoticz.com/wiki/Humidity_control used to function well, but lately (since Domoticz has been updated to 3.8153 ??) it doesn't.
In the log of Domoticz the following line is printed:

LUA: current is 0 or nil. Skipping this reading

Looks like the variable current is not filled with any value, but I am unable to find the cause of this.
Any suggestions or help will be appreciated!

Regards, Eric
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Lua script for controlling humidity in the bathroom

Post by BakSeeDaa »

ekrd wrote: Friday 08 September 2017 15:32 Script taken from http://www.domoticz.com/wiki/Humidity_control used to function well, but lately (since Domoticz has been updated to 3.8153 ??) it doesn't.
In the log of Domoticz the following line is printed:

LUA: current is 0 or nil. Skipping this reading

Looks like the variable current is not filled with any value, but I am unable to find the cause of this.
Any suggestions or help will be appreciated!

Regards, Eric
Do You have a device named exactly like what you have put in variable SENSOR_NAME ?
Snakfy
Posts: 2
Joined: Wednesday 29 November 2017 8:58
Target OS: Linux
Domoticz version:
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by Snakfy »

Just wanted to say thank you for the script.
That saved my some thinking :-D ;-)
pgas37
Posts: 99
Joined: Wednesday 06 December 2017 19:44
Target OS: -
Domoticz version:
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by pgas37 »

Hello,

I made the nessecary adjustments en tried the script.
user-var after update (originally 0)
IDX VARIABLE NAME VARIABLE TYPE CURRENT VALUE
8 testHumidityDigoo_ch1 Integer 60
7 humCounterDigoo_ch1 Integer 4
6 fanFollowsProgramDigoo_ch1 Integer 0
5 fanMaxTimerDigoo_ch1 Integer 12
4 targetFanOffHumidityDigoo_ch Integer 0
3 humidityTmin10Gigoo_ch1 Integer 0
2 humidityTmin5Digoo_ch1 Integer 60

I got an error :
2017-12-26 16:33:47.668 Error: EventSystem: in Hum-control-lua: [string " ..."]:181: bad argument #1 to 'min' (number expected, got nil)

Can you help me out?

thanks
Paul

Code: Select all

[
-- This script keeps track of the humidity in a bathroom and will active the
-- fan(s) when there was a rise in humidity within the first 5 minutes.
--
-- It is fully configurable by using the constantes below. It should not be
-- needed to change anything to the code itselfs.
--
-- It can be used for multiple bathrooms on one Domoticz server because
-- it uses 'variable' user variables blocks per bathroom. And it can be used
-- on small and large bathrooms. See SMALL_ROOM constante here below.
-- 
-- To set it up add the following user variables to your Domoticz:
-- * humCounter[SENSOR_NAME] (e.g.: humCounterBathroom)
-- * humidityTmin5[SENSOR_NAME]
-- * humidityTmin10[SENSOR_NAME]
-- * targetFanOffHumidity[SENSOR_NAME]
-- * fanMaxTimer[SENSOR_NAME]
-- * fanFollowsProgram[SENSOR_NAME]
-- 
-- Create one Lua - Time script per bathroom (e.g. 'house_fans', 'shower_fan')
-- Testing is done by setting the testHumidity[SENSOR_NAME] to a fake humidity value
-------------------------------------------------------------------------------

--
-- declare some constants and settings
--
-- true for little bath room, false for normal bathrooms
SMALL_ROOM = true
-- start the fans as soon as possible
DIRECT_START = false

if SMALL_ROOM then
    -- small bathroom (like 2x2 meter)
    -- time in minutes when a the script logic will happen
    SAMPLE_INTERVAL = 5                 
    -- rise in humidity needed for a bath that will trigger the fan
    FAN_DELTA_TRIGGER = 2               
    -- rise in humidity needed for a shower that will trigger the second fan
    FAN_DELTA_TRIGGER2 = 5              
    -- maximum amount of minutes the fan can be on, in case we never reach the target humidity
    FAN_MAX_TIME = 15                   
    -- ventilator goes off if target+offset is reached
    TARGET_OFFSET = 3                   
    -- exact device name of the switch turning on/off the ventilator 2 (itho fan box stand 2)
    FAN_NAME = 'on_off'
    -- 'Ventilator douche'      
    -- exact device name of the switch turning on/off the ventilator 2 (itho fan box stand 2)
    --FAN_NAME = 'Ventilator medium'      
    -- exact device name of the switch turning on/off the ventilator 3 (itho fan box stand 3)
    --FAN_NAME2 = 'Ventilator max'      
    -- exact device name of the humidity sensor
    SENSOR_NAME = 'Digoo_ch1'              
else
    -- normal bathroom (like 4x4 meter)
    -- time in minutes when a the script logic will happen
    SAMPLE_INTERVAL = 5                 
    -- rise in humidity needed for a bath that will trigger the fan
    FAN_DELTA_TRIGGER = 3               
    -- rise in humidity needed for a shower that will trigger the second fan
    FAN_DELTA_TRIGGER2 = 5              
    -- maximum amount of minues the fans can be on, in case we never reach the target humidity
    FAN_MAX_TIME = 15                   
    -- ventilator goes off if target+offset is reached
    TARGET_OFFSET = 2                
    -- exact device name of the switch turning on/off the ventilator 2 (itho fan box stand 2)
    FAN_NAME = 'Ventilator max'      
    -- exact device name of the switch turning on/off the ventilator 2 (itho fan box stand 2)
    --FAN_NAME = 'Ventilator medium'      
    -- exact device name of the switch turning on/off the ventilator 3 (itho fan box stand 3)
    --FAN_NAME2 = 'Ventilator max'      
    -- exact device name of the humidity sensor 
    SENSOR_NAME = 'Badkamer'            
end    

-------------------------------------------------------------------------------
-- No changes needed below this lines
-------------------------------------------------------------------------------

commandArray = {}

-- adding info to the log
function addMsg(txt)
    if txt == '' or txt == nil then
        return
    end    
    if msg ~= '' then
        msg = msg .. '\n'
    end
    msg = msg .. txt .. '. '
end    

-- send a notification
function sendNotication(txt)
    if txt == '' or txt == nil then
        return
    end    
    commandArray['SendNotification'] = txt
end

-- switch on a device (with retry)
function switchOn(device, msg)
    if (otherdevices[device] == 'Off') then
        commandArray[#commandArray+1] = {[device] = "On"}
        -- just to be sure, wait 30s and try again
        commandArray[#commandArray+1] = {[device] = "On AFTER 30"}
        -- and again after 60s
        commandArray[#commandArray+1] = {[device] = "On AFTER 60"}
        if (msg ~= '') then
            addMsg(msg)
        end    
    end    
end    

-- switch off a device (with retry)
function switchOff(device, msg)
    if otherdevices[device] == 'On' then
        commandArray[#commandArray+1] = {[device] = "Off"}
        -- just to be sure, wait 30s and try again
        commandArray[#commandArray+1] = {[device] = "Off AFTER 30"}
        -- and again after 60s
        commandArray[#commandArray+1] = {[device] = "Off AFTER 60"}
        if (msg ~= '') then
            addMsg(msg)
        end    
    end    
end    

TEST_MODE_HUMVAR = 'testHumidity' .. SENSOR_NAME  -- fake humidity value, give it a test value in domoticz/uservars
PRINT_MODE = true                  -- when true wil print output to log and send notifications
 
-- get the global variables:
-- this script runs every minute, humCounter is used to create SAMPLE_INTERVAL periods
humCounter = tonumber(uservariables['humCounter' .. SENSOR_NAME])
humidityTmin5 = tonumber(uservariables['humidityTmin5' .. SENSOR_NAME])                -- youngest reading
humidityTmin10 = tonumber(uservariables['humidityTmin10' .. SENSOR_NAME])              -- oldest reading
targetFanOffHumidity = tonumber(uservariables['targetFanOffHumidity' .. SENSOR_NAME])  -- target humidity
fanMaxTimer = tonumber(uservariables['fanMaxTimer' .. SENSOR_NAME])
fanFollowsProgram = tonumber(uservariables['fanFollowsProgram' .. SENSOR_NAME])        -- marker indicating that the programm is runing for a bath

target = 0 -- will hold the target humidity when the program starts
 
msg = '' -- extra info to show in debug/print mode

-- get the current humidity value
if (tonumber(uservariables[TEST_MODE_HUMVAR]) > 0) then
    current = tonumber(uservariables[TEST_MODE_HUMVAR])
else
    current = otherdevices_humidity[SENSOR_NAME]
end
 
-- check if the sensor is on or has some weird reading
if (current == 0 or current == nil) then
    print('Current humidity is 0 or nil. Skipping this reading')
    return commandArray
end
 
if (fanFollowsProgram == 0 and DIRECT_START) then
    humCounter = SAMPLE_INTERVAL    
    addMsg('Direct start is active')
else
    -- increase cycle counter (one run per minute)
    humCounter = humCounter + 1
end    
 
-- start the checks after the sample interval was reached (or direct start was enabled)
if (humCounter >= SAMPLE_INTERVAL) then
 
    if (humidityTmin5 == 0) then
        -- initialization, assume this is the first time
        humidityTmin5 = current
        humidityTmin10 = current
    end
 
    humCounter = 0 -- reset the cycle counter
 
    -- pick the lowest history value to calculate the delta
    -- this also makes sure that two relative small deltas in the past 2*interval minutes are treated as one larger rise
    -- and therefore will still trigger the ventilator
    -- I don't want to use a longer interval instead because I want the ventilator to start as soon as possible
    -- (so rather after 5 minutes instead of after 15 minutes because the mirrors in the bathroom become kinda useless ;-)
    delta = current - math.min(humidityTmin10, humidityTmin5)

    -- pick the lowest history value
    target = math.min(humidityTmin10, humidityTmin5) + TARGET_OFFSET
 
    -- shift the previous measurements
    humidityTmin10 = humidityTmin5
    -- and store the current
    humidityTmin5 = current
 
    -- either the fan is off or it is on but the decrease program has not started
    -- in that latter case we start the program anyway. This could happen if someone turns on the ventilator
    -- manually because he/she is about to take a shower and doesn't like damp mirrors.
    -- I don't do this because the ventilator removes heat from the bathroom and I want this to happen
    -- as late as possible ;-)
    if (otherdevices[FAN_NAME] == 'Off' or (otherdevices[FAN_NAME] == 'On' and fanFollowsProgram == 0)) then
 
        -- likely someone turned off the ventilator while the program was running
        if (fanFollowsProgram == 1 and otherdevices[FAN_NAME] == 'Off') then
            fanFollowsProgram = 0
        end
 
        -- see if we have to turn it on
        if (delta >= FAN_DELTA_TRIGGER) or (delta >= FAN_DELTA_TRIGGER2) then
            -- time to start the fan
            switchOn(FAN_NAME, 'Rise in humidity, turning on the fan, delta ' .. delta)

            -- we need to turn the second fan on for the shower (or large bath :)
            if (delta >= FAN_DELTA_TRIGGER2) then
                switchOn(FAN_NAME2, 'Large rise in humidity, turning on the second fan, delta ' .. delta)
            end
    
            targetFanOffHumidity = target
            fanFollowsProgram = 1
 
            -- set the safety stop
            fanMaxTimer = math.floor(FAN_MAX_TIME / SAMPLE_INTERVAL) - 1
 
            addMsg('Target humidity for switching off the fans: ' .. targetFanOffHumidity)
            sendNotication(FAN_NAME .. ' fan is on#The fan is activated at humidity ' .. current .. ', targetHumidity ' .. targetFanOffHumidity .. ', delta ' .. delta .. '#0')
        end

    else
    
        if (fanMaxTimer > 0) then
            -- possible that someone started the fan manually
            fanMaxTimer = fanMaxTimer - 1
        end
 
        if (fanFollowsProgram == 1) then -- not manually started
 
            -- ok, there is another FAN_DELTA_TRIGGER rise in humidity
            -- when this happen we reset the fanMaxTimer to a new count down
            -- because we have to ventilate a bit longer (and faster) due to the extra humidity
            if (delta >= FAN_DELTA_TRIGGER) then
                addMsg('Another large increase detected, resetting max timer, delta: ' .. delta)
                fanMaxTimer = math.floor(FAN_MAX_TIME / SAMPLE_INTERVAL) - 1
                -- and start the second fan
                switchOff(FAN_NAME2)
            end
        
            -- if humidity delta decrease is more then second trigger we can
            -- switch off the second fan
            if (delta < -FAN_DELTA_TRIGGER2) then
                switchOff(FAN_NAME2)
            end
 
            -- first see if it can be turned off
            if (current <= targetFanOffHumidity or fanMaxTimer == 0) then
                if (otherdevices[FAN_NAME2] == 'On') then
                    addMsg('The fans are deactivated at humidity ' .. current) 
                else
                    addMsg('The fan is deactivated at humidity ' .. current) 
                end

                switchOff(FAN_NAME)
                switchOff(FAN_NAME2)

                if (fanMaxTimer == 0 and current > targetFanOffHumidity) then
                    addMsg(', target humidity ' .. targetFanOffHumidity .. 
                        ' not reached. Safety time-out activated (' .. FAN_MAX_TIME .. '-' .. FAN_MAX_TIME + SAMPLE_INTERVAL .. ' min)')
                else
                    addMsg(', target humidity ' .. targetFanOffHumidity .. ' reached')
                end

                targetFanOffHumidity = 0
                fanMaxTimer = 0
                fanFollowsProgram = 0
                -- reset history in this case.. we start all over
                -- Tmin10 is still in the 'ventilator=On'-zone
                humidityTmin10 = humidityTmin5
                if PRINT_MODE == true then
                    sendNotication(FAN_NAME .. ' fan is off#' .. msg .. '#0')
                end
 
            else
                -- we haven't reached the target yet
                addMsg('Humidity delta: ' .. delta)
            end
        end
    end
    
    if PRINT_MODE == true then
        print('---')
        print('Checking humidity in ' .. SENSOR_NAME .. '...')
        print('Current humidity : ' .. current)
        print('humidityTmin5 : ' .. humidityTmin5)
        print('humidityTmin10 : ' .. humidityTmin10)
        print('fanMaxTimer : ' .. fanMaxTimer)
        print('humCounter : ' .. humCounter)
        print('targetFanOffHumidity : ' .. targetFanOffHumidity)
        print('fanFollowsProgram : ' .. fanFollowsProgram)
        print('fan 1 : ' .. FAN_NAME .. ' = ' .. otherdevices[FAN_NAME])
        --print('fan 2 : ' .. FAN_NAME2 .. ' = ' .. otherdevices[FAN_NAME2])
        
        if msg ~= '' then
            print('Extra info\n---\n' .. msg .. '\n---')
        end
    end

end


-- save the globals
commandArray['Variable:humCounter' .. SENSOR_NAME] = tostring(humCounter)
commandArray['Variable:humidityTmin5' .. SENSOR_NAME] = tostring(humidityTmin5)
commandArray['Variable:humidityTmin10' .. SENSOR_NAME] = tostring(humidityTmin10)
commandArray['Variable:targetFanOffHumidity' .. SENSOR_NAME] = tostring(targetFanOffHumidity)
commandArray['Variable:fanMaxTimer' .. SENSOR_NAME] = tostring(fanMaxTimer)
commandArray['Variable:fanFollowsProgram' .. SENSOR_NAME] = tostring(fanFollowsProgram)

return commandArray
]
User avatar
Lange1975
Posts: 6
Joined: Tuesday 04 October 2016 22:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by Lange1975 »

Hi all. I would like to adjust my script in such a way that the "targetFanOffHumidity" is always set at a specific value.

Does anyone know how to adjust the scrip for this?

Thanks!
TheeDude
Posts: 46
Joined: Wednesday 07 October 2015 0:43
Target OS: Linux
Domoticz version:
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by TheeDude »

Hello!

I have been using this script for a couple of weeks now, and it works phenomenal!

However, I am using this with a "Fresh Intellivent" fan, and that fan has 3 wiring options.
1. Constant live, neutral.
2. Constant live, neutral and switched leg (which I am using now)
3. Constant live, neutral and a retractive switch.

The problem with #2 that I am using is that the fan automatically then uses a 90 second time delay.
I think that scenario is to vent out smelly odors after a visit.

Lua programming isn´t my strong side, and is it possible to add that start of the fan is in a retractive behaviour, so it should be ON/OFF/ON just to trigger the start of the fan right away.
I can program the fibaro switch to do this, but I guess that the script won´t work as it should when the swtich is just turning off right away.

90 Seconds is a long time to build up moisture/humidity.

The manual of the fan is here, and wiring instructions are at page 7, but it is written in either SE, NO, DK or FI.
https://www.delat.ws/download/INTELLIVENT.pdf

Thanks in advance!
User avatar
Thuis
Posts: 267
Joined: Tuesday 11 September 2018 11:36
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by Thuis »

With my not so good scripting skills, i try to understand it all.
I have used the testHumidity and put it on 55, before the script runs.
Then i run the script, and it says 0 so going to next reading.
Then i wait, then after 2 reading i put in, i set the testHumidity on 67
and between every reading i lower it, until 55 after several readings.
And in the spoiler it works very well.

Spoiler: show
2018-10-08 15:54:00.622 Status: LUA: Fan control
2018-10-08 15:54:00.622 Status: LUA: Current humidity:55
2018-10-08 15:54:00.623 Status: LUA: targetFanOffHumidity:0
2018-10-08 15:54:00.623 Status: LUA: humidityTmin5: 0
2018-10-08 15:54:00.624 Status: LUA: humidityTmin10: 0
2018-10-08 15:54:00.624 Status: LUA: fanMaxTimer: 0
2018-10-08 15:54:00.624 Status: LUA: humCounter:0
2018-10-08 15:54:00.625 Status: LUA: fanFollowsProgram:0
2018-10-08 15:54:00.682 Status: EventSystem: Script event triggered: script_time_Humidity_control

2018-10-08 15:55:01.168 Status: LUA: Fan control
2018-10-08 15:55:01.168 Status: LUA: Current humidity:55
2018-10-08 15:55:01.169 Status: LUA: targetFanOffHumidity:0
2018-10-08 15:55:01.169 Status: LUA: humidityTmin5: 0
2018-10-08 15:55:01.169 Status: LUA: humidityTmin10: 0
2018-10-08 15:55:01.170 Status: LUA: fanMaxTimer: 0
2018-10-08 15:55:01.170 Status: LUA: humCounter:1
2018-10-08 15:55:01.170 Status: LUA: fanFollowsProgram:0
2018-10-08 15:55:01.171 Status: LUA: Delta: 0
2018-10-08 15:55:01.171 Status: LUA: New values >>>>>>>>>>>
2018-10-08 15:55:01.172 Status: LUA: humidityTmin5: 55
2018-10-08 15:55:01.172 Status: LUA: humidityTmin10: 55
2018-10-08 15:55:01.172 Status: LUA: fanMaxTimer: 0
2018-10-08 15:55:01.173 Status: LUA: humCounter:0
2018-10-08 15:55:01.173 Status: LUA: fanFollowsProgram:0
2018-10-08 15:55:01.174 Status: LUA: ------ target: 0
2018-10-08 15:55:01.229 Status: EventSystem: Script event triggered: script_time_Humidity_control

2018-10-08 15:56:00.858 Status: LUA: Fan control
2018-10-08 15:56:00.858 Status: LUA: Current humidity:55
2018-10-08 15:56:00.859 Status: LUA: targetFanOffHumidity:0
2018-10-08 15:56:00.859 Status: LUA: humidityTmin5: 55
2018-10-08 15:56:00.859 Status: LUA: humidityTmin10: 55
2018-10-08 15:56:00.860 Status: LUA: fanMaxTimer: 0
2018-10-08 15:56:00.860 Status: LUA: humCounter:0
2018-10-08 15:56:00.861 Status: LUA: fanFollowsProgram:0
2018-10-08 15:56:00.925 Status: EventSystem: Script event triggered: script_time_Humidity_control



2018-10-08 15:58:00.955 Status: LUA: Fan control
2018-10-08 15:58:00.955 Status: LUA: Current humidity:67
2018-10-08 15:58:00.956 Status: LUA: targetFanOffHumidity:57
2018-10-08 15:58:00.957 Status: LUA: humidityTmin5: 67
2018-10-08 15:58:00.958 Status: LUA: humidityTmin10: 55
2018-10-08 15:58:00.958 Status: LUA: fanMaxTimer: 24
2018-10-08 15:58:00.958 Status: LUA: humCounter:0
2018-10-08 15:58:00.959 Status: LUA: fanFollowsProgram:1

2018-10-08 15:59:00.641 Status: LUA: Fan control
2018-10-08 15:59:00.642 Status: LUA: Current humidity:65
2018-10-08 15:59:00.642 Status: LUA: targetFanOffHumidity:57
2018-10-08 15:59:00.642 Status: LUA: humidityTmin5: 67
2018-10-08 15:59:00.643 Status: LUA: humidityTmin10: 55
2018-10-08 15:59:00.643 Status: LUA: fanMaxTimer: 24
2018-10-08 15:59:00.644 Status: LUA: humCounter:1
2018-10-08 15:59:00.644 Status: LUA: fanFollowsProgram:1
2018-10-08 15:59:00.644 Status: LUA: Delta: 10
2018-10-08 15:59:00.645 Status: LUA: Another large increase detected, resetting max timer. Delta: 10
2018-10-08 15:59:00.645 Status: LUA: Humidity delta: 10
2018-10-08 15:59:00.646 Status: LUA: New values >>>>>>>>>>>
2018-10-08 15:59:00.646 Status: LUA: humidityTmin5: 65
2018-10-08 15:59:00.646 Status: LUA: humidityTmin10: 67
2018-10-08 15:59:00.647 Status: LUA: fanMaxTimer: 24
2018-10-08 15:59:00.647 Status: LUA: humCounter:0
2018-10-08 15:59:00.648 Status: LUA: fanFollowsProgram:1
2018-10-08 15:59:00.648 Status: LUA: ------ target: 57
2018-10-08 15:59:00.704 Status: EventSystem: Script event triggered: script_time_Humidity_control


2018-10-08 16:00:01.201 Status: LUA: Fan control
2018-10-08 16:00:01.202 Status: LUA: Current humidity:65
2018-10-08 16:00:01.202 Status: LUA: targetFanOffHumidity:57
2018-10-08 16:00:01.202 Status: LUA: humidityTmin5: 65
2018-10-08 16:00:01.203 Status: LUA: humidityTmin10: 67
2018-10-08 16:00:01.203 Status: LUA: fanMaxTimer: 24
2018-10-08 16:00:01.204 Status: LUA: humCounter:0
2018-10-08 16:00:01.204 Status: LUA: fanFollowsProgram:1
2018-10-08 16:00:01.208 Status: Ending automatic database backup procedure...
2018-10-08 16:00:01.262 Status: EventSystem: Script event triggered: script_time_Humidity_control

2018-10-08 16:01:00.830 Status: LUA: Fan control
2018-10-08 16:01:00.830 Status: LUA: Current humidity:63
2018-10-08 16:01:00.833 Status: LUA: targetFanOffHumidity:57
2018-10-08 16:01:00.833 Status: LUA: humidityTmin5: 65
2018-10-08 16:01:00.833 Status: LUA: humidityTmin10: 67
2018-10-08 16:01:00.834 Status: LUA: fanMaxTimer: 24
2018-10-08 16:01:00.834 Status: LUA: humCounter:1
2018-10-08 16:01:00.834 Status: LUA: fanFollowsProgram:1
2018-10-08 16:01:00.835 Status: LUA: Delta: -2
2018-10-08 16:01:00.835 Status: LUA: Humidity delta: -2
2018-10-08 16:01:00.837 Status: LUA: New values >>>>>>>>>>>
2018-10-08 16:01:00.838 Status: LUA: humidityTmin5: 63
2018-10-08 16:01:00.838 Status: LUA: humidityTmin10: 65
2018-10-08 16:01:00.839 Status: LUA: fanMaxTimer: 23
2018-10-08 16:01:00.839 Status: LUA: humCounter:0
2018-10-08 16:01:00.839 Status: LUA: fanFollowsProgram:1
2018-10-08 16:01:00.840 Status: LUA: ------ target: 57
2018-10-08 16:01:00.898 Status: EventSystem: Script event triggered: script_time_Humidity_control

2018-10-08 16:02:00.672 Status: LUA: Fan control
2018-10-08 16:02:00.672 Status: LUA: Current humidity:59
2018-10-08 16:02:00.673 Status: LUA: targetFanOffHumidity:57
2018-10-08 16:02:00.673 Status: LUA: humidityTmin5: 63
2018-10-08 16:02:00.674 Status: LUA: humidityTmin10: 65
2018-10-08 16:02:00.674 Status: LUA: fanMaxTimer: 23
2018-10-08 16:02:00.674 Status: LUA: humCounter:0
2018-10-08 16:02:00.675 Status: LUA: fanFollowsProgram:1
2018-10-08 16:02:00.732 Status: EventSystem: Script event triggered: script_time_Humidity_control

2018-10-08 16:03:01.018 Status: LUA: Fan control
2018-10-08 16:03:01.018 Status: LUA: Current humidity:55
2018-10-08 16:03:01.019 Status: LUA: targetFanOffHumidity:57
2018-10-08 16:03:01.019 Status: LUA: humidityTmin5: 63
2018-10-08 16:03:01.019 Status: LUA: humidityTmin10: 65
2018-10-08 16:03:01.020 Status: LUA: fanMaxTimer: 23
2018-10-08 16:03:01.020 Status: LUA: humCounter:1
2018-10-08 16:03:01.021 Status: LUA: fanFollowsProgram:1
2018-10-08 16:03:01.021 Status: LUA: Delta: -8
2018-10-08 16:03:01.021 Status: LUA: Target humidity reached
2018-10-08 16:03:01.022 Status: LUA: Turning off the ventilator
2018-10-08 16:03:01.022 Status: LUA: New values >>>>>>>>>>>
2018-10-08 16:03:01.023 Status: LUA: humidityTmin5: 55
2018-10-08 16:03:01.023 Status: LUA: humidityTmin10: 55
2018-10-08 16:03:01.023 Status: LUA: fanMaxTimer: 0
2018-10-08 16:03:01.024 Status: LUA: humCounter:0
2018-10-08 16:03:01.024 Status: LUA: fanFollowsProgram:0
2018-10-08 16:03:01.025 Status: LUA: ------ target: 0
2018-10-08 16:03:01.082 Status: EventSystem: Script event triggered: script_time_Humidity_control
2018-10-08 16:03:01.158 Status: Notification: Ventilator is off

My question is, do i have to start the script, when my sensor is 55% and set the offset to 0? Because that is my target for a good humidity.
Otherwise it would go from some start point and adds 2 every time the script starts and stops. I mean, if i start the script and the sensor says it is already 70% then it will never get 55%. And if it is 55% and the offset will be 2, then after a while it will be over 100%, adding 2 every time. Or do i miss read the reality of things? 55+2 57+2 59+2 etc. Because the fan stops at 57, 59,61, every time after a script run? I do not have the hardware yet, but as i simulate things it looks like it keeps adding up. Is there not a way to fix 55% and always go to 55% ?
I Love Domoticz ! And the community around it :-)
ensingg
Posts: 65
Joined: Saturday 22 April 2017 17:35
Target OS: Windows
Domoticz version:
Contact:

Manual Start

Post by ensingg »

Whats the best method to use a switch to switch on manually and after that switch of with the program?
So, when I have a device Schakelaar = ON, what to do ?
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by nigels0 »

Fantastic piece of code - saved me many hours - and I probably wouldn’t have made it so comprehensive.

I’ve made a change to the wiki as the test mode was not working correctly, discrepancy between a local value and a user variable.
allagamma
Posts: 2
Joined: Wednesday 02 October 2019 22:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by allagamma »

I installed the Original script but keep getting this error:

Error: EventSystem: in script_time_humidity.lua: [string "commandArray = {} ..."]:51: attempt to concatenate global 'humidityTmin10' (a nil value)

Who can help?
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by nigels0 »

Try setting up the variables in Domoticz and set a value of 0. The error is potentially because you haven’t initialised the variable.
allagamma
Posts: 2
Joined: Wednesday 02 October 2019 22:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by allagamma »

I removed the variables and created them new.
Now it works ;) .
Can you explain this?
BarryT
Posts: 367
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Lua script for controlling humidity in the bathroom

Post by BarryT »

I'm getting this since today, after updated to latest beta:

2019-11-07 19:32:00.359 Error: EventSystem: Error updating variable humidityTmin10: Not a valid integer
2019-11-07 19:32:00.360 Error: EventSystem: Error updating variable humidityTmin5: Not a valid integer

I deleted the variable, and added it again but it didnt help...
What is going wrong here?

* Edit:
I fixed it by changing the integer to string, now its working again.
I think the xiaomi has done an update where the humidity sensor is changed to a value with a .decimal

Anyway, its working again.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest