Page 3 of 8

Re: Lua script for controlling humidity in the bathroom

Posted: Saturday 11 April 2015 3:50
by dannybloe
Did you create all the necessary user variables correctly as stated in the wiki? Without that it will not work.

Re: Lua script for controlling humidity in the bathroom

Posted: Sunday 12 April 2015 9:50
by marten
Copy pasted all the user variables an now it works

Re: Lua script for controlling humidity in the bathroom

Posted: Wednesday 15 April 2015 0:00
by Skippiemanz
Ive edited the script in the wiki with a PRINT_MODE variable.

When set to true it wil print out the data to the log as before.

When set to fals it will stop to print data to the log and stop sending notifications.

Lua script for controlling humidity in the bathroom

Posted: Wednesday 15 April 2015 12:08
by geezerrr
Thanks!!

Re: Lua script for controlling humidity in the bathroom

Posted: Saturday 18 July 2015 10:25
by spaceinvader
cant get a sensor reading with the lua script
2015-07-18 10:17:00.388 LUA: Fan control
2015-07-18 10:17:00.388 LUA: current is 0 or nil. Skipping this reading
However in the #/Temperature page i can get all the readings of my temp/humidity sensors...
sensor= Alecto WS1700

105 RFXCOM AB02 2 Badkamer Temp + Humidity Alecto WS1700 24.9 C, 67 %

Tried to remove and add the sensor without succes. With other alecto ws1700 i have the same problem.
using the latest beta version on raspberry pi 2 with rfxcom

Re: Lua script for controlling humidity in the bathroom

Posted: Wednesday 12 August 2015 23:12
by ReneSalden
Same error in my log file: LUA: current is 0 or nil. Skipping this reading.

The json statement for the hygrometer in my badroom http://192.168.1.29:8080/json.htm?type=devices&rid=459 gives the following output:

{
"ActTime" : 1439407102,
"ServerTime" : "Aug 12 2015 21:18:22",
"Sunrise" : "06:14:00",
"Sunset" : "21:04:00",
"result" : [
{
"BatteryLevel" : 100,
"CustomImage" : 0,
"Data" : "Humidity 72 %",
"Description" : "BS",
"Favorite" : 0,
"HardwareID" : 2,
"HardwareName" : "433 Transiever",
"HaveTimeout" : false,
"Humidity" : 72,
"HumidityStatus" : "Wet",
"ID" : "2300",
"LastUpdate" : "2015-08-12 21:18:05",
"Name" : "BS",
"Notifications" : "false",
"PlanID" : "0",
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : 7,
"SubType" : "LaCrosse TX3",
"Timers" : "false",
"Type" : "Humidity",
"TypeImg" : "temperature",
"Unit" : 1,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "459"
}
],
"status" : "OK",
"title" : "Devices"
}

If I use the dummy switch everyting works OK. Using an other hygrometer (from my Aquarium) works also OK.

Re: Lua script for controlling humidity in the bathroom

Posted: Wednesday 12 August 2015 23:29
by Skippiemanz
Please try turning off/on blockly/Lua in the settings. This fixed things for me

Re: Lua script for controlling humidity in the bathroom

Posted: Wednesday 12 August 2015 23:36
by ReneSalden
I was very skeptical about the tip from Skippiemanz but it solved my problem......

Re: Lua script for controlling humidity in the bathroom

Posted: Thursday 13 August 2015 11:36
by dannybloe
Ah, great that it works for you. I am using this script for a long time now without any problems.

Re: Lua script for controlling humidity in the bathroom

Posted: Sunday 23 August 2015 10:14
by ThinkPad
Does this script need a sensor that only contains humidity? Or will it also work with a sensor that has both temperature and humidity (which most of the sensors have)

Re: Lua script for controlling humidity in the bathroom

Posted: Sunday 23 August 2015 11:35
by Justintime
Also thought about it to make the ventilation smart.
But since my goal is to reduce energy. I think this will only crank the energy bill up.

Re: Lua script for controlling humidity in the bathroom

Posted: Sunday 23 August 2015 11:59
by bizziebis
ThinkPad wrote:Does this script need a sensor that only contains humidity? Or will it also work with a sensor that has both temperature and humidity (which most of the sensors have)
It works fine with combined sensors. It's possible the script needs a small adjustment, at least in my case. I can add it to the wiki when I'm sure it doesn't break anything.

Re: Lua script for controlling humidity in the bathroom

Posted: Sunday 23 August 2015 12:52
by ThinkPad
Justintime wrote:Also thought about it to make the ventilation smart.
But since my goal is to reduce energy. I think this will only crank the energy bill up.
When your bathroom starts looking like this: http://www.diydoctor.org.uk/project_ima ... -walls.jpg i think you will be sorry that you don't have a script like this :mrgreen: :lol:
bizziebis wrote:
ThinkPad wrote:Does this script need a sensor that only contains humidity? Or will it also work with a sensor that has both temperature and humidity (which most of the sensors have)
It works fine with combined sensors. It's possible the script needs a small adjustment, at least in my case. I can add it to the wiki when I'm sure it doesn't break anything.
OK, good to know. Scraping only the temperature from a combined sensor isn't that hard either. Something like this: http://domoticz.com/forum/viewtopic.php ... 869#p25878 will do fine (with the addition to write it to a virtual temperature sensor). I am not going to use the script from dannybloe myself, but was interested in how it works.

Re: Lua script for controlling humidity in the bathroom

Posted: Sunday 23 August 2015 13:19
by Justintime
ThinkPad wrote:
Justintime wrote:Also thought about it to make the ventilation smart.
But since my goal is to reduce energy. I think this will only crank the energy bill up.
When your bathroom starts looking like this: http://www.diydoctor.org.uk/project_ima ... -walls.jpg i think you will be sorry that you don't have a script like this :mrgreen: Image
bizziebis wrote:
ThinkPad wrote:Does this script need a sensor that only contains humidity? Or will it also work with a sensor that has both temperature and humidity (which most of the sensors have)
It works fine with combined sensors. It's possible the script needs a small adjustment, at least in my case. I can add it to the wiki when I'm sure it doesn't break anything.
OK, good to know. Scraping only the temperature from a combined sensor isn't that hard either. Something like this: http://domoticz.com/forum/viewtopic.php ... 869#p25878 will do fine (with the addition to write it to a virtual temperature sensor). I am not going to use the script from dannybloe myself, but was interested in how it works.
LOL that's extreme mold... Good environment for mushrooms.

I have some timers running on the ventilation. Works fine...

But always nice to see those fully automated scripts... Though Image

Re: Lua script for controlling humidity in the bathroom

Posted: Sunday 23 August 2015 16:30
by dannybloe
bizziebis wrote:
ThinkPad wrote:Does this script need a sensor that only contains humidity? Or will it also work with a sensor that has both temperature and humidity (which most of the sensors have)
It works fine with combined sensors. It's possible the script needs a small adjustment, at least in my case. I can add it to the wiki when I'm sure it doesn't break anything.
Well, I have a combined sensor myself and it works like a charm. Afaik

Code: Select all

otherdevices_humidity[SENSOR_NAME]
takes care of this.

Re: Lua script for controlling humidity in the bathroom

Posted: Saturday 29 August 2015 13:00
by franzelare
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: Lua script for controlling humidity in the bathroom

Posted: Saturday 29 August 2015 14:59
by bizziebis
dannybloe wrote:
Well, I have a combined sensor myself and it works like a charm. Afaik

Code: Select all

otherdevices_humidity[SENSOR_NAME]
takes care of this.
Well, for me it worked also.. Until I changed to a slave domoticz for retrieving this temp+humidity sensor. After that I couldn't use that piece of code anymore. Looks like not all sensor types in Domoticz are behaving the same.

Re: Lua script for controlling humidity in the bathroom

Posted: Saturday 29 August 2015 17:27
by ThinkPad
You could scrape the value from that slave sensor on the master Domoticz and put it into a virtual sensor (on the master Domoticz)? I guess it is then seen again by the script.

Re: Lua script for controlling humidity in the bathroom

Posted: Friday 30 October 2015 9:23
by Cybertom
Hi,

I have a question about these 2 if statements

Code: Select all

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
I'm not getting how the second if statement is working.
in my opinion when fanfollowsprogram = 1 and otherdevices[FAN_NAME]=='Off'
the first if statement is not true, so the second if statement never going to be executed.
But it does ?

Did i miss something?

Re: Lua script for controlling humidity in the bathroom

Posted: Friday 30 October 2015 9:30
by dannybloe
Cybertom wrote:Hi,

I have a question about these 2 if statements

Code: Select all

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
I'm not getting how the second if statement is working.
in my opinion when fanfollowsprogram = 1 and otherdevices[FAN_NAME]=='Off'
the first if statement is not true, so the second if statement never going to be executed.
But it does ?

Did i miss something?
I believe you do :). If the fan is off then you will always enter the first if-block (it's an OR statement). So when you enter it, the fanFollowsProgram could be set. This happens when someone turns off the ventilator while the dehumidify program is running. Then it will terminate the program by setting fanFollowsProgram to 0. End of the show.