Page 5 of 8

Re: Lua script for controlling humidity in the bathroom

Posted: Monday 13 June 2016 14:24
by dannybloe
toups wrote:
dannybloe wrote:I will make a version in the coming days that will support multiple devices. It will be based on dzVents though because that allows me to check previous readings more easily. And it is much easier to support multiple 'rooms/devices'.
could you pls confirm if dzVents could run directly on Windows ?
I'm sorry, I cannot. It uses some linux specifics. But hopefully it will soon.

Re: Lua script for controlling humidity in the bathroom

Posted: Thursday 07 July 2016 9:29
by jorns
dannybloe wrote:I will make a version in the coming days that will support multiple devices. It will be based on dzVents though because that allows me to check previous readings more easily. And it is much easier to support multiple 'rooms/devices'.

Hi.

Any progress on the new version?

Re: Lua script for controlling humidity in the bathroom

Posted: Thursday 07 July 2016 9:34
by dannybloe
Ah... you got me ;-)
Didn't have the time yet.. will do in the coming days. Sorry :(

Re: Lua script for controlling humidity in the bathroom

Posted: Sunday 04 September 2016 14:00
by JustME125
dannybloe wrote:Ah... you got me ;-)
Didn't have the time yet.. will do in the coming days. Sorry :(
Hi Danny,

Any luck on this one?
I am hoping to spend my holidays to transfer to dZVents and this would be a nice starting point.

I have seen some comments on using dZVents in the internal lua editor (the one in the browser) but can't seem to find it anymore, any clue?

Thanks for all the hard work on this one!

BR,
Bram

Re: Lua script for controlling humidity in the bathroom

Posted: Thursday 15 September 2016 8:13
by dannybloe
JustME125 wrote:
dannybloe wrote:Ah... you got me ;-)
Didn't have the time yet.. will do in the coming days. Sorry :(
Hi Danny,

Any luck on this one?
I am hoping to spend my holidays to transfer to dZVents and this would be a nice starting point.

I have seen some comments on using dZVents in the internal lua editor (the one in the browser) but can't seem to find it anymore, any clue?

Thanks for all the hard work on this one!

BR,
Bram
:mrgreen:
Mmm.. summer and vacation drove me out of the house and away from the computer and my home systems. Sorry about that.
The internal editor doesn't work with dzVents unfortunately.

Re: RE: Re: Lua script for controlling humidity in the bathroom

Posted: Thursday 15 September 2016 8:27
by JustME125
dannybloe wrote:
JustME125 wrote:
dannybloe wrote:Ah... you got me ;-)
Didn't have the time yet.. will do in the coming days. Sorry :(
Hi Danny,

Any luck on this one?
I am hoping to spend my holidays to transfer to dZVents and this would be a nice starting point.

I have seen some comments on using dZVents in the internal lua editor (the one in the browser) but can't seem to find it anymore, any clue?

Thanks for all the hard work on this one!

BR,
Bram
:mrgreen:
Mmm.. summer and vacation drove me out of the house and away from the computer and my home systems. Sorry about that.
The internal editor doesn't work with dzVents unfortunately.
No problem, Invested some time in it last week and wrote it myself. Nice piece of work by the way :D

Verstuurd vanaf mijn HTC One_M8 met Tapatalk

Re: Lua script for controlling humidity in the bathroom

Posted: Monday 10 October 2016 14:33
by Lange1975
Hi Danny,

First of all thank you for your humidity control script.

But the script is behaving a bit strange. The user variable “humCounter” is refreshing every second and not every minute as you mention.

Also the fan stops within 5 minutes even when the humidity is still increasing.

Is there something that I might overlook?

Thank you in advance.

Re: Lua script for controlling humidity in the bathroom

Posted: Monday 10 October 2016 22:22
by Lange1975
It works perfect. But somehow after a couple of minutes it all stops. And I don't know why
franzelare wrote:I based my script on the script in the wiki, the difference is that I want 2 trip levels for humidity change, one for when someone takes a bath and one for the shower.
I have a 3 stage fanbox for my central exhaust system (always running on 1st stage) plus one additional build in pipe fan inside the chanel to the bathroom (because that used to stay moist with the normal fan or my kitchen exhaust flow was to low to get rid of all smells)
when someone takes a bath I only want to start the fanbox at stage 3, but when a shower also needs to trigger the additional fan.

this is where I ended up with, and is working fine for a few months now

Code: Select all

commandArray = {}
 
-- declare some constants
-- adjust to your specific situation
SAMPLE_INTERVAL = 5                 -- time in minutes when a the script logic will happen
FAN_DELTA_TRIGGER = 3               -- rise in humidity needed for a bath that will trigger the fan
FAN_DELTA_TRIGGER2 = 6               -- rise in humidity needed for a shower that will trigger the fan
FAN_MAX_TIME = 20                   --  maximum amount of sample cycles the fan can be on, 
                                    -- in case we never reach the target humidity
TARGET_OFFSET = 2                   -- ventilator goes off if target+offset is reached 
FAN_NAME = 'Ventilator 2'           -- exact device name of the switch turning on/off the ventilator 2 (itho fan box stand 2)
FAN_NAME2 = 'Ventilator 3'    	    -- exact device name of the switch turning on/off the ventilator 3 (itho fan box stand 3)
FAN_NAME3 = 'Ventilator 4'    	    -- exact device name of the switch turning on/off the ventilator 4 (additional bathroom fan)
SENSOR_NAME = 'Badkamer'     -- exact device name of the humidity sensor

TEST_MODE = false                   -- when true TEST_MODE_HUMVAR is used instead of the real sensor
TEST_MODE_HUMVAR = 'testHumidity'   -- fake humidity value, give it a test value in domoticz/uservars
PRINT_MODE = false					-- when true wil print output to log and send notifications
 
if PRINT_MODE == true then
print('Fan control')
end
 
-- get the global variables:
-- this script runs every minute, humCounter is used to create SAMPLE_INTERVAL periods
humCounter = tonumber(uservariables['humCounter'])
humidityTmin5 = tonumber(uservariables['humidityTmin5'])                -- youngest reading
humidityTmin10 = tonumber(uservariables['humidityTmin10'])              -- oldest reading
targetFanOffHumidity = tonumber(uservariables['targetFanOffHumidity'])  -- target humidity
fanMaxTimer = tonumber(uservariables['fanMaxTimer'])
fanFollowsProgram = tonumber(uservariables['fanFollowsProgram'])        -- marker indicating that the programm is runing for a bath
fan3FollowsProgram = tonumber(uservariables['fan3FollowsProgram'])        -- marker indicating that the  programm is runing for a shower
 
target = 0 -- will hold the target humidity when the program starts
 
-- get the current humidity value
if (TEST_MODE) 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 is 0 or nil. Skipping this reading')
    return commandArray
end
 
if PRINT_MODE == true then
        print('Current humidity:' .. current)
		print('targetFanOffHumidity:' .. targetFanOffHumidity)
		print('humidityTmin5: ' .. humidityTmin5)
		print('humidityTmin10: ' .. humidityTmin10)
		print('fanMaxTimer: ' .. fanMaxTimer)
		print('humCounter:' .. humCounter)
		print('fanFollowsProgram:' .. fanFollowsProgram)
		print('fan3FollowsProgram:' .. fan3FollowsProgram)
end
 
-- increase cycle counter
humCounter = humCounter + 1
 
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)
if PRINT_MODE == true then
	print('Delta: ' .. delta)
end
 
    -- pick the lowest history value
    target = math.min(humidityTmin10, humidityTmin5) + TARGET_OFFSET
 
    -- shift the previous measurements
    humidityTmin10 = humidityTmin5
    -- and store the current
    humidityTmin5 = current
 
    if (otherdevices[FAN_NAME]=='Off' or (otherdevices[FAN_NAME]=='On' and fanFollowsProgram==0)) then
        -- 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 (fanFollowsProgram == 1 and otherdevices[FAN_NAME]=='Off') then
            -- likely someone turned off the ventilator while the program was running
            fanFollowsProgram = 0
        end
 
       -- see if we have to turn it on for the shower
        if (delta >= FAN_DELTA_TRIGGER2) then
            -- time to start the extra fan
            commandArray[FAN_NAME] = 'On'
            commandArray[FAN_NAME2] = 'On'
            commandArray[FAN_NAME3] = 'On'
            targetFanOffHumidity = target
 
            fanFollowsProgram = 1
            fan3FollowsProgram = 1
 
            -- set the safety stop
            fanMaxTimer = FAN_MAX_TIME
 
			if PRINT_MODE == true then
            print('Large rise in humidity. Turning on the exra vent. Delta: ' .. delta)
            print('Target humidity for turning the ventilator: ' ..targetFanOffHumidity)
            commandArray['SendNotification'] = 'Ventilator is on#The extra ventilator was activated at humidity level ' .. current .. '#0'
			end
        end


        -- see if we have to turn it on for the bath
        if (delta >= FAN_DELTA_TRIGGER) then
            -- time to start the fan
            commandArray[FAN_NAME] = 'On'
            commandArray[FAN_NAME2] = 'On'
            targetFanOffHumidity = target
 
            fanFollowsProgram = 1
 
            -- set the safety stop
            fanMaxTimer = FAN_MAX_TIME
 
			if PRINT_MODE == true then
            print('Rise in humidity. Turning on the vents. Delta: ' .. delta)
            print('Target humidity for turning the ventilator: ' ..targetFanOffHumidity)
            commandArray['SendNotification'] = 'Ventilator is on#The ventilator was activated at humidity level ' .. current .. '#0'
			end
        end

 
    else
        if (fanMaxTimer > 0) then
            -- possible that someone started the ventialator manually
            fanMaxTimer = fanMaxTimer - 1
        end
 
 
        if (fanFollowsProgram == 1) then -- not manually started
 
            if (delta >= FAN_DELTA_TRIGGER) then
                -- 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 due to the extra humidity
                if PRINT_MODE == true then
				print('Another large increase detected, resetting max timer. Delta: ' .. delta)
				end
                fanMaxTimer = FAN_MAX_TIME
            end
 
            -- first see if it can be turned off
            if (current <= targetFanOffHumidity or fanMaxTimer==0) then
                commandArray[FAN_NAME] = 'Off'
                commandArray[FAN_NAME2] = 'Off'
                commandArray[FAN_NAME3] = 'Off' 
                msg = ''
 
                if (fanMaxTimer == 0 and current > targetFanOffHumidity) then
                    msg = 'Target not reached but safety time-out is triggered.'
                    if PRINT_MODE == true then
					print(msg)
					end
                else
                    msg = 'Target humidity reached'
                    if PRINT_MODE == true then
					print(msg)
					end
                end
 
				if PRINT_MODE == true then
                print('Turning off the ventilator')
                msg = msg .. '\nTurning off the ventilator'
				end
 
                targetFanOffHumidity = 0
                fanMaxTimer = 0
                fanFollowsProgram = 0
                fan3FollowsProgram = 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
				commandArray['SendNotification'] = 'Ventilator is off#' .. msg .. '#0'
				end
 
            else
                -- we haven't reached the target yet
               if PRINT_MODE == true then
			   print('Humidity delta: ' .. delta)
			   end
            end
        end
    end
 
if PRINT_MODE == true then
    print('New values >>>>>>>>>>>')
    print('humidityTmin5: ' .. humidityTmin5)
    print('humidityTmin10: ' .. humidityTmin10)
    print('fanMaxTimer: ' .. fanMaxTimer)
    print('humCounter:' .. humCounter)
    print('fanFollowsProgram:' .. fanFollowsProgram)
    print('fan3FollowsProgram:' .. fan3FollowsProgram)
    print('------ target: ' .. targetFanOffHumidity)
end
 
end
 
-- save the globals
commandArray['Variable:humCounter'] = tostring(humCounter)
commandArray['Variable:humidityTmin10'] = tostring(humidityTmin10)
commandArray['Variable:humidityTmin5'] = tostring(humidityTmin5)
commandArray['Variable:targetFanOffHumidity'] = tostring(targetFanOffHumidity)
commandArray['Variable:fanMaxTimer'] = tostring(fanMaxTimer)
commandArray['Variable:fanFollowsProgram'] = tostring(fanFollowsProgram)
commandArray['Variable:fan3FollowsProgram'] = tostring(fan3FollowsProgram)
 
return commandArray

Re: RE: Re: RE: Re: Lua script for controlling humidity in the bathroom

Posted: Tuesday 11 October 2016 2:19
by JustME125
JustME125 wrote:
dannybloe wrote:
JustME125 wrote:
Hi Danny,

Any luck on this one?
I am hoping to spend my holidays to transfer to dZVents and this would be a nice starting point.

I have seen some comments on using dZVents in the internal lua editor (the one in the browser) but can't seem to find it anymore, any clue?

Thanks for all the hard work on this one!

BR,
Bram
:mrgreen:
Mmm.. summer and vacation drove me out of the house and away from the computer and my home systems. Sorry about that.
The internal editor doesn't work with dzVents unfortunately.
No problem, Invested some time in it last week and wrote it myself. Nice piece of work by the way :D

Verstuurd vanaf mijn HTC One_M8 met Tapatalk
This is my code, please note that it is modified because I have more programs on my ventilation system running so this is part of a bigger picture.

Code: Select all

--[[ 
This script checks the humidity level in the bathtroom to detect showering activity
When the humidify program is running, it sets the fanProgram variable to 1 (0 for manual and 2 for automatic mode)

This script runs every 5 minutes and compares it against the previous reading, when the delta is exceeds a threshold we can assume somebody is taking a shower	
--]]
return{
	active = true,
	on = {['timer'] = 'every 5 minutes'},
	data = { humidity = { history = true, maxItems = 2 }, targetFanOffHumidity = { initial = 100}, previousVentilationStatus = {initial = 99} }, -- we now have the t-5 and t-10 values in the history 
	execute = function(domoticz)
		-- devices
		local sensor = domoticz.devices['Badkamer']
		local fan = domoticz.devices['$Ventilation Control']
		local ventilation = domoticz.devices['Ventilation']
		
		-- variables
		local humTmin5 = domoticz.data.humidity.get(1) -- get the data with timestamp
		local humTmin10 = domoticz.data.humidity.get(2) -- get the data with timestamp		
		humTmin5 = humTmin5.data
		humTmin10 = humTmin10.data
		local currentHumidity
		local delta
		local target
		local fanMode = domoticz.variables['fanMode']
				
		-- constants
		TARGET_OFFSET = 2
		FAN_DELTA_TRIGGER = 3
		TEST_MODE = false
		TEST_MODE_HUMVAR = 10
		
		-- TEST mode
		if(TEST_MODE) then
			currentHumidity = TEST_MODE_HUMVAR
			domoticz.log("Test mode active", domoticz.LOG_INFO)
		else
			currentHumidity = sensor.humidity 
		end
		if(humTmin5 == 0 or humTmin5 == nil) then -- initialization, assume this is the first time
			humTmin5 = currentHumidity
			humTmin10 = currentHumidity
		end
		
		-- 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 = currentHumidity - math.min(humTmin10,humTmin5)
		
		-- pick the lowest history value
		target = math.min(humTmin10, humTmin5) + TARGET_OFFSET
		
		-- write the history, this pushes the current into history 1, which will become t-5 in the next run
		-- and pushes the current history 1 to history 2 which will become t-10 in the next run
		domoticz.data.humidity.add(currentHumidity)
		
		-- Now we have figured out the delta humidity so we can decide what to do
		if(fan.state == 'Off' or (fan.state ~= 'Off' and fanMode.value == 0) or (fan.state ~= 'Off' and fanMode.value == 2)) then
			-- Either the fan is off or the fan is on but the humidity program is not running (manual mode or automatic mode)
			-- in the last two cases we start with the humidity 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 ;-)
			
			-- see if we need to turn on the fan
			if(delta >= FAN_DELTA_TRIGGER) then
				-- turn it on and set the fanMode
				domoticz.data.targetFanOffHumidity = target
				
				
				domoticz.data.previousVentilationStatus = ventilation.state -- save the current control state before changing so we can give control back when ready
				fan.switchSelector(30)
				domoticz.variables['fanMode'].set(1)
				
				if (fanMode.value == 1) then
					domoticz.log("Ventilator was already on but we start the de-humidifying program", domoticz.LOG_INFO)
				end
				
				domoticz.log("Rise in humidity. Turning on the fan. Delta: " .. delta, domoticz.LOG_INFO)
				domoticz.log("Target humidity for turning the ventilator off: " ..domoticz.data.targetFanOffHumidity, domoticz.LOG_INFO)
        	
			end
		else
			if (fanMode.value == 1) then -- not manually started
				if(delta >= FAN_DELTA_TRIGGER) then
					-- ok, another rise in humidity detected
					-- when this happens we restart the whole program
					-- so we have new 2 hours time frame
					fan.switchSelector(30)
					domoticz.log("Another large increase detected, resetting max timer. Delta: " .. delta, domoticz.LOG_INFO)
					domoticz.variables['fanMode'].set(1)
				end
				-- first check if we can turn off the fan
				if(currentHumidity <= domoticz.data.targetFanOffHumidity or fan.lastUpdate.minutesAgo > 120) then
					-- we can, so we revert to the state before the program started
					if(domoticz.data.previousVentilationStatus == 'Off') then
						ventilation.switchSelector(0)
					end
					if(domoticz.data.previousVentilationStatus == 'Low') then
						ventilation.switchSelector(10)
					end
					if(domoticz.data.previousVentilationStatus == 'Medium') then
						ventilation.switchSelector(20)
					end
					if(domoticz.data.previousVentilationStatus == 'High') then
						ventilation.switchSelector(30)
					end
					if(domoticz.data.previousVentilationStatus == 'Automatic') then
						ventilation.switchSelector(40)
					end
					
					-- now we want to log what happened
					if(fan.lastUpdate.minutesAgo > 120 and currentHumidity > domoticz.data.targetFanOffHumidity) then
						domoticz.log("Target not reached but safety time-out is triggered", domoticz.LOG_INFO)
					else
						domoticz.log("Target humidity reached", domoticz.LOG_INFO)
					end
					domoticz.log("Reverting back to: " ..domoticz.data.previousVentilationStatus .. " mode", domoticz.LOG_INFO)
					
					domoticz.data.targetFanOffHumidity = 0
					-- reset history in this case.. we start all over
					-- Tmin10 is still in the 'ventilator=On'-zone
					humidityTmin10 = humidityTmin5
				else
					-- we haven't reached the target yet
					domoticz.log("Humidity delta: " ..delta .."%", domoticz.LOG_INFO)
				end
			end
		end
	end
}
Verstuurd vanaf mijn HTC One_M8 met Tapatalk

Re: RE: Re: RE: Re: Lua script for controlling humidity in the bathroom

Posted: Tuesday 11 October 2016 8:14
by dannybloe
JustME125 wrote:
JustME125 wrote:
dannybloe wrote:
:mrgreen:
Mmm.. summer and vacation drove me out of the house and away from the computer and my home systems. Sorry about that.
The internal editor doesn't work with dzVents unfortunately.
No problem, Invested some time in it last week and wrote it myself. Nice piece of work by the way :D

Verstuurd vanaf mijn HTC One_M8 met Tapatalk
This is my code, please note that it is modified because I have more programs on my ventilation system running so this is part of a bigger picture.

Code: Select all

--[[ 
This script checks the humidity level in the bathtroom to detect showering activity
When the humidify program is running, it sets the fanProgram variable to 1 (0 for manual and 2 for automatic mode)

This script runs every 5 minutes and compares it against the previous reading, when the delta is exceeds a threshold we can assume somebody is taking a shower	
--]]
return{
	active = true,
	on = {['timer'] = 'every 5 minutes'},
	data = { humidity = { history = true, maxItems = 2 }, targetFanOffHumidity = { initial = 100}, previousVentilationStatus = {initial = 99} }, -- we now have the t-5 and t-10 values in the history 
	execute = function(domoticz)
		-- devices
		local sensor = domoticz.devices['Badkamer']
		local fan = domoticz.devices['$Ventilation Control']
		local ventilation = domoticz.devices['Ventilation']
		
		-- variables
		local humTmin5 = domoticz.data.humidity.get(1) -- get the data with timestamp
		local humTmin10 = domoticz.data.humidity.get(2) -- get the data with timestamp		
		humTmin5 = humTmin5.data
		humTmin10 = humTmin10.data
		local currentHumidity
		local delta
		local target
		local fanMode = domoticz.variables['fanMode']
				
		-- constants
		TARGET_OFFSET = 2
		FAN_DELTA_TRIGGER = 3
		TEST_MODE = false
		TEST_MODE_HUMVAR = 10
		
		-- TEST mode
		if(TEST_MODE) then
			currentHumidity = TEST_MODE_HUMVAR
			domoticz.log("Test mode active", domoticz.LOG_INFO)
		else
			currentHumidity = sensor.humidity 
		end
		if(humTmin5 == 0 or humTmin5 == nil) then -- initialization, assume this is the first time
			humTmin5 = currentHumidity
			humTmin10 = currentHumidity
		end
		
		-- 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 = currentHumidity - math.min(humTmin10,humTmin5)
		
		-- pick the lowest history value
		target = math.min(humTmin10, humTmin5) + TARGET_OFFSET
		
		-- write the history, this pushes the current into history 1, which will become t-5 in the next run
		-- and pushes the current history 1 to history 2 which will become t-10 in the next run
		domoticz.data.humidity.add(currentHumidity)
		
		-- Now we have figured out the delta humidity so we can decide what to do
		if(fan.state == 'Off' or (fan.state ~= 'Off' and fanMode.value == 0) or (fan.state ~= 'Off' and fanMode.value == 2)) then
			-- Either the fan is off or the fan is on but the humidity program is not running (manual mode or automatic mode)
			-- in the last two cases we start with the humidity 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 ;-)
			
			-- see if we need to turn on the fan
			if(delta >= FAN_DELTA_TRIGGER) then
				-- turn it on and set the fanMode
				domoticz.data.targetFanOffHumidity = target
				
				
				domoticz.data.previousVentilationStatus = ventilation.state -- save the current control state before changing so we can give control back when ready
				fan.switchSelector(30)
				domoticz.variables['fanMode'].set(1)
				
				if (fanMode.value == 1) then
					domoticz.log("Ventilator was already on but we start the de-humidifying program", domoticz.LOG_INFO)
				end
				
				domoticz.log("Rise in humidity. Turning on the fan. Delta: " .. delta, domoticz.LOG_INFO)
				domoticz.log("Target humidity for turning the ventilator off: " ..domoticz.data.targetFanOffHumidity, domoticz.LOG_INFO)
        	
			end
		else
			if (fanMode.value == 1) then -- not manually started
				if(delta >= FAN_DELTA_TRIGGER) then
					-- ok, another rise in humidity detected
					-- when this happens we restart the whole program
					-- so we have new 2 hours time frame
					fan.switchSelector(30)
					domoticz.log("Another large increase detected, resetting max timer. Delta: " .. delta, domoticz.LOG_INFO)
					domoticz.variables['fanMode'].set(1)
				end
				-- first check if we can turn off the fan
				if(currentHumidity <= domoticz.data.targetFanOffHumidity or fan.lastUpdate.minutesAgo > 120) then
					-- we can, so we revert to the state before the program started
					if(domoticz.data.previousVentilationStatus == 'Off') then
						ventilation.switchSelector(0)
					end
					if(domoticz.data.previousVentilationStatus == 'Low') then
						ventilation.switchSelector(10)
					end
					if(domoticz.data.previousVentilationStatus == 'Medium') then
						ventilation.switchSelector(20)
					end
					if(domoticz.data.previousVentilationStatus == 'High') then
						ventilation.switchSelector(30)
					end
					if(domoticz.data.previousVentilationStatus == 'Automatic') then
						ventilation.switchSelector(40)
					end
					
					-- now we want to log what happened
					if(fan.lastUpdate.minutesAgo > 120 and currentHumidity > domoticz.data.targetFanOffHumidity) then
						domoticz.log("Target not reached but safety time-out is triggered", domoticz.LOG_INFO)
					else
						domoticz.log("Target humidity reached", domoticz.LOG_INFO)
					end
					domoticz.log("Reverting back to: " ..domoticz.data.previousVentilationStatus .. " mode", domoticz.LOG_INFO)
					
					domoticz.data.targetFanOffHumidity = 0
					-- reset history in this case.. we start all over
					-- Tmin10 is still in the 'ventilator=On'-zone
					humidityTmin10 = humidityTmin5
				else
					-- we haven't reached the target yet
					domoticz.log("Humidity delta: " ..delta .."%", domoticz.LOG_INFO)
				end
			end
		end
	end
}
Verstuurd vanaf mijn HTC One_M8 met Tapatalk
Unfortunately I don't have much time lately to look into this very thoroughly but I would be very surprised if the script is run every second as this is not how Domoticz works. Unless you renamed the script and made it an event-based script instead of a time-based script. Make sure the filename of the script if script_time_<name>.lua and not script_device_<name>.lua. In the later case it will be executed everytime some device triggers the eventsystem which can be every second.

Re: RE: Re: RE: Re: RE: Re: Lua script for controlling humidity in the bathroom

Posted: Tuesday 11 October 2016 8:18
by JustME125
dannybloe wrote:
JustME125 wrote:
JustME125 wrote: No problem, Invested some time in it last week and wrote it myself. Nice piece of work by the way :D

Verstuurd vanaf mijn HTC One_M8 met Tapatalk
This is my code, please note that it is modified because I have more programs on my ventilation system running so this is part of a bigger picture.

Code: Select all

--[[ 
This script checks the humidity level in the bathtroom to detect showering activity
When the humidify program is running, it sets the fanProgram variable to 1 (0 for manual and 2 for automatic mode)

This script runs every 5 minutes and compares it against the previous reading, when the delta is exceeds a threshold we can assume somebody is taking a shower	
--]]
return{
	active = true,
	on = {['timer'] = 'every 5 minutes'},
	data = { humidity = { history = true, maxItems = 2 }, targetFanOffHumidity = { initial = 100}, previousVentilationStatus = {initial = 99} }, -- we now have the t-5 and t-10 values in the history 
	execute = function(domoticz)
		-- devices
		local sensor = domoticz.devices['Badkamer']
		local fan = domoticz.devices['$Ventilation Control']
		local ventilation = domoticz.devices['Ventilation']
		
		-- variables
		local humTmin5 = domoticz.data.humidity.get(1) -- get the data with timestamp
		local humTmin10 = domoticz.data.humidity.get(2) -- get the data with timestamp		
		humTmin5 = humTmin5.data
		humTmin10 = humTmin10.data
		local currentHumidity
		local delta
		local target
		local fanMode = domoticz.variables['fanMode']
				
		-- constants
		TARGET_OFFSET = 2
		FAN_DELTA_TRIGGER = 3
		TEST_MODE = false
		TEST_MODE_HUMVAR = 10
		
		-- TEST mode
		if(TEST_MODE) then
			currentHumidity = TEST_MODE_HUMVAR
			domoticz.log("Test mode active", domoticz.LOG_INFO)
		else
			currentHumidity = sensor.humidity 
		end
		if(humTmin5 == 0 or humTmin5 == nil) then -- initialization, assume this is the first time
			humTmin5 = currentHumidity
			humTmin10 = currentHumidity
		end
		
		-- 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 = currentHumidity - math.min(humTmin10,humTmin5)
		
		-- pick the lowest history value
		target = math.min(humTmin10, humTmin5) + TARGET_OFFSET
		
		-- write the history, this pushes the current into history 1, which will become t-5 in the next run
		-- and pushes the current history 1 to history 2 which will become t-10 in the next run
		domoticz.data.humidity.add(currentHumidity)
		
		-- Now we have figured out the delta humidity so we can decide what to do
		if(fan.state == 'Off' or (fan.state ~= 'Off' and fanMode.value == 0) or (fan.state ~= 'Off' and fanMode.value == 2)) then
			-- Either the fan is off or the fan is on but the humidity program is not running (manual mode or automatic mode)
			-- in the last two cases we start with the humidity 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 ;-)
			
			-- see if we need to turn on the fan
			if(delta >= FAN_DELTA_TRIGGER) then
				-- turn it on and set the fanMode
				domoticz.data.targetFanOffHumidity = target
				
				
				domoticz.data.previousVentilationStatus = ventilation.state -- save the current control state before changing so we can give control back when ready
				fan.switchSelector(30)
				domoticz.variables['fanMode'].set(1)
				
				if (fanMode.value == 1) then
					domoticz.log("Ventilator was already on but we start the de-humidifying program", domoticz.LOG_INFO)
				end
				
				domoticz.log("Rise in humidity. Turning on the fan. Delta: " .. delta, domoticz.LOG_INFO)
				domoticz.log("Target humidity for turning the ventilator off: " ..domoticz.data.targetFanOffHumidity, domoticz.LOG_INFO)
        	
			end
		else
			if (fanMode.value == 1) then -- not manually started
				if(delta >= FAN_DELTA_TRIGGER) then
					-- ok, another rise in humidity detected
					-- when this happens we restart the whole program
					-- so we have new 2 hours time frame
					fan.switchSelector(30)
					domoticz.log("Another large increase detected, resetting max timer. Delta: " .. delta, domoticz.LOG_INFO)
					domoticz.variables['fanMode'].set(1)
				end
				-- first check if we can turn off the fan
				if(currentHumidity <= domoticz.data.targetFanOffHumidity or fan.lastUpdate.minutesAgo > 120) then
					-- we can, so we revert to the state before the program started
					if(domoticz.data.previousVentilationStatus == 'Off') then
						ventilation.switchSelector(0)
					end
					if(domoticz.data.previousVentilationStatus == 'Low') then
						ventilation.switchSelector(10)
					end
					if(domoticz.data.previousVentilationStatus == 'Medium') then
						ventilation.switchSelector(20)
					end
					if(domoticz.data.previousVentilationStatus == 'High') then
						ventilation.switchSelector(30)
					end
					if(domoticz.data.previousVentilationStatus == 'Automatic') then
						ventilation.switchSelector(40)
					end
					
					-- now we want to log what happened
					if(fan.lastUpdate.minutesAgo > 120 and currentHumidity > domoticz.data.targetFanOffHumidity) then
						domoticz.log("Target not reached but safety time-out is triggered", domoticz.LOG_INFO)
					else
						domoticz.log("Target humidity reached", domoticz.LOG_INFO)
					end
					domoticz.log("Reverting back to: " ..domoticz.data.previousVentilationStatus .. " mode", domoticz.LOG_INFO)
					
					domoticz.data.targetFanOffHumidity = 0
					-- reset history in this case.. we start all over
					-- Tmin10 is still in the 'ventilator=On'-zone
					humidityTmin10 = humidityTmin5
				else
					-- we haven't reached the target yet
					domoticz.log("Humidity delta: " ..delta .."%", domoticz.LOG_INFO)
				end
			end
		end
	end
}
Verstuurd vanaf mijn HTC One_M8 met Tapatalk
Unfortunately I don't have much time lately to look into this very thoroughly but I would be very surprised if the script is run every second as this is not how Domoticz works. Unless you renamed the script and made it an event-based script instead of a time-based script. Make sure the filename of the script if script_time_<name>.lua and not script_device_<name>.lua. In the later case it will be executed everytime some device triggers the eventsystem which can be every second.
Mine is running every 5 minutes, no issue with it at all. I think you would want to comment on the other question ;). This was just FYI post.

Verstuurd vanaf mijn HTC One_M8 met Tapatalk

Re: RE: Re: RE: Re: RE: Re: Lua script for controlling humidity in the bathroom

Posted: Tuesday 11 October 2016 8:20
by dannybloe
JustME125 wrote:
dannybloe wrote:
JustME125 wrote: This is my code, please note that it is modified because I have more programs on my ventilation system running so this is part of a bigger picture.

Code: Select all

--[[ 
This script checks the humidity level in the bathtroom to detect showering activity
When the humidify program is running, it sets the fanProgram variable to 1 (0 for manual and 2 for automatic mode)

This script runs every 5 minutes and compares it against the previous reading, when the delta is exceeds a threshold we can assume somebody is taking a shower	
--]]
return{
	active = true,
	on = {['timer'] = 'every 5 minutes'},
	data = { humidity = { history = true, maxItems = 2 }, targetFanOffHumidity = { initial = 100}, previousVentilationStatus = {initial = 99} }, -- we now have the t-5 and t-10 values in the history 
	execute = function(domoticz)
		-- devices
		local sensor = domoticz.devices['Badkamer']
		local fan = domoticz.devices['$Ventilation Control']
		local ventilation = domoticz.devices['Ventilation']
		
		-- variables
		local humTmin5 = domoticz.data.humidity.get(1) -- get the data with timestamp
		local humTmin10 = domoticz.data.humidity.get(2) -- get the data with timestamp		
		humTmin5 = humTmin5.data
		humTmin10 = humTmin10.data
		local currentHumidity
		local delta
		local target
		local fanMode = domoticz.variables['fanMode']
				
		-- constants
		TARGET_OFFSET = 2
		FAN_DELTA_TRIGGER = 3
		TEST_MODE = false
		TEST_MODE_HUMVAR = 10
		
		-- TEST mode
		if(TEST_MODE) then
			currentHumidity = TEST_MODE_HUMVAR
			domoticz.log("Test mode active", domoticz.LOG_INFO)
		else
			currentHumidity = sensor.humidity 
		end
		if(humTmin5 == 0 or humTmin5 == nil) then -- initialization, assume this is the first time
			humTmin5 = currentHumidity
			humTmin10 = currentHumidity
		end
		
		-- 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 = currentHumidity - math.min(humTmin10,humTmin5)
		
		-- pick the lowest history value
		target = math.min(humTmin10, humTmin5) + TARGET_OFFSET
		
		-- write the history, this pushes the current into history 1, which will become t-5 in the next run
		-- and pushes the current history 1 to history 2 which will become t-10 in the next run
		domoticz.data.humidity.add(currentHumidity)
		
		-- Now we have figured out the delta humidity so we can decide what to do
		if(fan.state == 'Off' or (fan.state ~= 'Off' and fanMode.value == 0) or (fan.state ~= 'Off' and fanMode.value == 2)) then
			-- Either the fan is off or the fan is on but the humidity program is not running (manual mode or automatic mode)
			-- in the last two cases we start with the humidity 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 ;-)
			
			-- see if we need to turn on the fan
			if(delta >= FAN_DELTA_TRIGGER) then
				-- turn it on and set the fanMode
				domoticz.data.targetFanOffHumidity = target
				
				
				domoticz.data.previousVentilationStatus = ventilation.state -- save the current control state before changing so we can give control back when ready
				fan.switchSelector(30)
				domoticz.variables['fanMode'].set(1)
				
				if (fanMode.value == 1) then
					domoticz.log("Ventilator was already on but we start the de-humidifying program", domoticz.LOG_INFO)
				end
				
				domoticz.log("Rise in humidity. Turning on the fan. Delta: " .. delta, domoticz.LOG_INFO)
				domoticz.log("Target humidity for turning the ventilator off: " ..domoticz.data.targetFanOffHumidity, domoticz.LOG_INFO)
        	
			end
		else
			if (fanMode.value == 1) then -- not manually started
				if(delta >= FAN_DELTA_TRIGGER) then
					-- ok, another rise in humidity detected
					-- when this happens we restart the whole program
					-- so we have new 2 hours time frame
					fan.switchSelector(30)
					domoticz.log("Another large increase detected, resetting max timer. Delta: " .. delta, domoticz.LOG_INFO)
					domoticz.variables['fanMode'].set(1)
				end
				-- first check if we can turn off the fan
				if(currentHumidity <= domoticz.data.targetFanOffHumidity or fan.lastUpdate.minutesAgo > 120) then
					-- we can, so we revert to the state before the program started
					if(domoticz.data.previousVentilationStatus == 'Off') then
						ventilation.switchSelector(0)
					end
					if(domoticz.data.previousVentilationStatus == 'Low') then
						ventilation.switchSelector(10)
					end
					if(domoticz.data.previousVentilationStatus == 'Medium') then
						ventilation.switchSelector(20)
					end
					if(domoticz.data.previousVentilationStatus == 'High') then
						ventilation.switchSelector(30)
					end
					if(domoticz.data.previousVentilationStatus == 'Automatic') then
						ventilation.switchSelector(40)
					end
					
					-- now we want to log what happened
					if(fan.lastUpdate.minutesAgo > 120 and currentHumidity > domoticz.data.targetFanOffHumidity) then
						domoticz.log("Target not reached but safety time-out is triggered", domoticz.LOG_INFO)
					else
						domoticz.log("Target humidity reached", domoticz.LOG_INFO)
					end
					domoticz.log("Reverting back to: " ..domoticz.data.previousVentilationStatus .. " mode", domoticz.LOG_INFO)
					
					domoticz.data.targetFanOffHumidity = 0
					-- reset history in this case.. we start all over
					-- Tmin10 is still in the 'ventilator=On'-zone
					humidityTmin10 = humidityTmin5
				else
					-- we haven't reached the target yet
					domoticz.log("Humidity delta: " ..delta .."%", domoticz.LOG_INFO)
				end
			end
		end
	end
}
Verstuurd vanaf mijn HTC One_M8 met Tapatalk
Unfortunately I don't have much time lately to look into this very thoroughly but I would be very surprised if the script is run every second as this is not how Domoticz works. Unless you renamed the script and made it an event-based script instead of a time-based script. Make sure the filename of the script if script_time_<name>.lua and not script_device_<name>.lua. In the later case it will be executed everytime some device triggers the eventsystem which can be every second.
Mine is running every 5 minutes, no issue with it at all. I think you would want to comment on the other question ;). This was just FYI post.

Verstuurd vanaf mijn HTC One_M8 met Tapatalk
Ah, crap, you're right ;-)

Re: Lua script for controlling humidity in the bathroom

Posted: Tuesday 11 October 2016 8:21
by dannybloe
Lange1975 wrote:Hi Danny,

First of all thank you for your humidity control script.

But the script is behaving a bit strange. The user variable “humCounter” is refreshing every second and not every minute as you mention.

Also the fan stops within 5 minutes even when the humidity is still increasing.

Is there something that I might overlook?

Thank you in advance.
Unfortunately I don't have much time lately to look into this very thoroughly but I would be very surprised if the script is run every second as this is not how Domoticz works. Unless you renamed the script and made it an event-based script instead of a time-based script. Make sure the filename of the script if script_time_<name>.lua and not script_device_<name>.lua. In the later case it will be executed every time some device triggers the event-system which can be every second.

Re: Lua script for controlling humidity in the bathroom

Posted: Wednesday 12 October 2016 5:59
by Lange1975
Danny I Found the problem. It was exact as you described below. It was event-based and NOT time-based.
Thank you for your help.

A minor other question. Do you have experience with Nest Protect V2?
I’m curious to know if Domoticz is able to see/read the CO and CO2 values that the Nest sensors can pick up. If so then there is the option to use the Nest sensors to trigger the fan if the CO2 levels are getting high.

dannybloe wrote:
Lange1975 wrote:Hi Danny,

First of all thank you for your humidity control script.

But the script is behaving a bit strange. The user variable “humCounter” is refreshing every second and not every minute as you mention.

Also the fan stops within 5 minutes even when the humidity is still increasing.

Is there something that I might overlook?

Thank you in advance.
Unfortunately I don't have much time lately to look into this very thoroughly but I would be very surprised if the script is run every second as this is not how Domoticz works. Unless you renamed the script and made it an event-based script instead of a time-based script. Make sure the filename of the script if script_time_<name>.lua and not script_device_<name>.lua. In the later case it will be executed every time some device triggers the event-system which can be every second.

Re: Lua script for controlling humidity in the bathroom

Posted: Wednesday 12 October 2016 7:37
by dannybloe
Lange1975 wrote:Danny I Found the problem. It was exact as you described below. It was event-based and NOT time-based.
Thank you for your help.

A minor other question. Do you have experience with Nest Protect V2?
I’m curious to know if Domoticz is able to see/read the CO and CO2 values that the Nest sensors can pick up. If so then there is the option to use the Nest sensors to trigger the fan if the CO2 levels are getting high.
Good to hear. I was suspecting that. But no, I don't have any nests at home except my home itself ;-)

Re: Lua script for controlling humidity in the bathroom

Posted: Thursday 13 October 2016 12:40
by franzelare
Lange1975 wrote:Danny I Found the problem. It was exact as you described below. It was event-based and NOT time-based.
Thank you for your help.

A minor other question. Do you have experience with Nest Protect V2?
I’m curious to know if Domoticz is able to see/read the CO and CO2 values that the Nest sensors can pick up. If so then there is the option to use the Nest sensors to trigger the fan if the CO2 levels are getting high.

dannybloe wrote:
Lange1975 wrote:Hi Danny,

First of all thank you for your humidity control script.

But the script is behaving a bit strange. The user variable “humCounter” is refreshing every second and not every minute as you mention.

Also the fan stops within 5 minutes even when the humidity is still increasing.

Is there something that I might overlook?

Thank you in advance.
Unfortunately I don't have much time lately to look into this very thoroughly but I would be very surprised if the script is run every second as this is not how Domoticz works. Unless you renamed the script and made it an event-based script instead of a time-based script. Make sure the filename of the script if script_time_<name>.lua and not script_device_<name>.lua. In the later case it will be executed every time some device triggers the event-system which can be every second.
not with nest but working to get NH3 sensing also installed since I have a toilet in the bathroom and want to do some smell control in case needed...
will post that when it is finished, that will be mysensor based for the NH3 and using 2 sensor nodes and some tube fans to control the flow from the toilets/bathroom/kitchen

Re: RE: Re: Lua script for controlling humidity in the bathroom

Posted: Thursday 13 October 2016 16:07
by Lange1975
franzelare wrote:
Lange1975 wrote:Danny I Found the problem. It was exact as you described below. It was event-based and NOT time-based.
Thank you for your help.

A minor other question. Do you have experience with Nest Protect V2?
I’m curious to know if Domoticz is able to see/read the CO and CO2 values that the Nest sensors can pick up. If so then there is the option to use the Nest sensors to trigger the fan if the CO2 levels are getting high.

dannybloe wrote:
Unfortunately I don't have much time lately to look into this very thoroughly but I would be very surprised if the script is run every second as this is not how Domoticz works. Unless you renamed the script and made it an event-based script instead of a time-based script. Make sure the filename of the script if script_time_<name>.lua and not script_device_<name>.lua. In the later case it will be executed every time some device triggers the event-system which can be every second.
not with nest but working to get NH3 sensing also installed since I have a toilet in the bathroom and want to do some smell control in case needed...
will post that when it is finished, that will be mysensor based for the NH3 and using 2 sensor nodes and some tube fans to control the flow from the toilets/bathroom/kitchen
What's a Nh3 season?

Verstuurd vanaf mijn SM-G935F met Tapatalk

Re: RE: Re: Lua script for controlling humidity in the bathroom

Posted: Thursday 13 October 2016 22:45
by franzelare
Lange1975 wrote:What's a Nh3 season?
sorry, methane, was working on an NH3 related issue for my work at the same time ;-)
using an MQ-4 sensor

Re: Lua script for controlling humidity in the bathroom

Posted: Sunday 16 October 2016 16:28
by renerene
The temperature part of sensor is more sensitive than the humidity part. I am thinking of re-writing the code and use the rise of temperature as trigger. Before I go into this: has anybody done this already?
(I don't like the current trigger mechanism. Allthough I narrowed the treshold, the fan start too late or not at all)

Re: Lua script for controlling humidity in the bathroom

Posted: Tuesday 06 June 2017 20:49
by harolds
renerene wrote:The temperature part of sensor is more sensitive than the humidity part. I am thinking of re-writing the code and use the rise of temperature as trigger. Before I go into this: has anybody done this already?
(I don't like the current trigger mechanism. Allthough I narrowed the treshold, the fan start too late or not at all)
I had that thought myselfs too. Maybe you can start with my improved script? See here below.