My dzVents Bathroom Humidity Control script

Slorf
Posts: 64
Joined: Friday 20 December 2013 12:55
Target OS: Raspberry Pi
Domoticz version:
Location: Voorburg
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by Slorf » Sunday 16 July 2017 17:07

Hi,

I am trying to get the script to work, but unsuccessfully :-(

Our fanbox has 3 settings.

Setting 1: no one home ( only use this when we are on vacation, manually )
Setting 2: normal setting
Setting 3: shower of cooking

Plan is to have a remote in every ( 2 ) bathroom and a remote in the kitchen, when a remote is pressed the fanbox goes to setting 3, what I do not understand is how the targetHum can be higher then the Current Humidity, I am using dzVents 1.1

I did not change anything in the script except following lines:

local FAN_DEVICE = 'Ventilatie-3-Virtual' -- Fan device ( this a virtual switch that controls the fanbox settings )
local FORCE_FAN_DEVICE = 'Ventilatie-Remote-Virtual' -- (Optional) ( this is a virtual switch that goes on when I press a remote )
local FORCE_FAN_TIME = 45 -- Minutes to force the fan when button pushed
local HUMIDITY_SENSORS = {'Badkamer', 'Douche'}
local FAN_DELTA_TRIGGER = 5 -- % Rise in humidity that will trigger the fan
local FAN_MAX_TIME = 45 -- Maximum minutes that the fan can be on in case we never reach the target humidity
local TARGET_OFFSET = 1 -- Fan goes off if target + offset is reached
local TEST_MODE_HUMIDITY_READING = 0 -- Set to a value between 1 and 100. Set to 0 to disable test mode
local DEBUG = true
local READING_SAMPLES = 15

When I run the script it starts the fanbox all the time

2017-07-16 17:01:00.466 LUA: =====================================================

2017-07-16 17:01:00.466 LUA: >>> Handler: fanspeed

2017-07-16 17:01:00.466 LUA: .....................................................

2017-07-16 17:01:00.479 LUA: Dehumidification program doesn't run for: Badkamer

2017-07-16 17:01:00.479 LUA: targetHum: 74, Current humidity: 73, humDelta: 0

2017-07-16 17:01:00.480 LUA: Dehumidification program doesn't run for: Douche

2017-07-16 17:01:00.480 LUA: targetHum: 68, Current humidity: 67, humDelta: 0

2017-07-16 17:01:00.480 LUA: Turning the fan On

2017-07-16 17:01:00.485 LUA: .....................................................

2017-07-16 17:01:00.485 LUA: <<< Done

2017-07-16 17:01:00.485 LUA: -----------------------------------------------------

2017-07-16 17:01:00.486 LUA: [1] = Ventilatie-3-Virtual: On

2017-07-16 17:01:00.486 LUA: =====================================================

2017-07-16 17:01:00.487 EventSystem: Script event triggered: /home/domoticz/domoticz/scripts/lua/script_time_main.lua
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

User avatar
BakSeeDaa
Posts: 513
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by BakSeeDaa » Thursday 10 August 2017 16:13

New version. Updated original post. Bathroom Humidity Control script to version 1.3.0.
Slorf wrote:
Sunday 16 July 2017 17:07
When I run the script it starts the fanbox all the time
Hi there! I can't say what's going on really. Can you please upgrade both dzVents and the Bathroom Humidity Control script to latest version. After that, please let me know if you still experience any issues with my script.
Best wishes

// บักสีดา

renerene
Posts: 73
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by renerene » Sunday 20 August 2017 11:17

I have tested your script in the first posting. It works ok, but the functionaliy of the override button is limited.

Can you please add following functionality: force fan off with override button. And stays off for 20 minutes or so. Because sometimes it triggers falsly (humidity rise but no shower) or want it to end because I open the window

User avatar
BakSeeDaa
Posts: 513
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by BakSeeDaa » Monday 21 August 2017 8:37

renerene wrote:
Sunday 20 August 2017 11:17
I have tested your script in the first posting. It works ok, but the functionaliy of the override button is limited.

Can you please add following functionality: force fan off with override button. And stays off for 20 minutes or so. Because sometimes it triggers falsly (humidity rise but no shower) or want it to end because I open the window
To customize the script to fit your needs you can easily add something similar to this:

After:

Code: Select all

			if ((forceFanReadings.getLatest().time.minutesAgo < FAN_MAX_TIME)
			and (forceFanReadings.getLatest().data == 'On')) then fanCmd = 'On' end
Add:

Code: Select all

			local blockingDevice = domoticz.devices('A window or a button')
			if blockingDevice.state == 'On'
			and blockingDevice.lastUpdate.minutesAgo < 20  then
				fanCmd = 'Off'
				domoticz.log('The blocking device prevents the fan from running', domoticz.LOG_INFO)
			end
Cheers!
Best wishes

// บักสีดา

R0yk3
Posts: 39
Joined: Sunday 24 July 2016 21:51
Target OS: Raspberry Pi
Domoticz version: beta
Location: the Netherlands
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by R0yk3 » Wednesday 23 August 2017 14:21

Hi, The script runs every minute, but it does not have to run when you're not at home. I have 2 (dummy) switches who turn on when my wife or me is at home (phone connects to network). So would it be a improvement to only le the script to run when one or both phones are at home, or is that a total waste of characters ;)

i am using this presence detection.
viewtopic.php?f=61&t=15531

User avatar
BakSeeDaa
Posts: 513
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by BakSeeDaa » Wednesday 23 August 2017 19:33

R0yk3 wrote:
Wednesday 23 August 2017 14:21
Hi, The script runs every minute, but it does not have to run when you're not at home. I have 2 (dummy) switches who turn on when my wife or me is at home (phone connects to network). So would it be a improvement to only le the script to run when one or both phones are at home, or is that a total waste of characters ;)

i am using this presence detection.
viewtopic.php?f=61&t=15531
You can easily modify the script to do a "return" if there is no presence detected at home. The script would still have to be invoked though. I believe though that the time you can save is so small so it won't be worth it.

You can easily measure how long time the script takes inside the execute function.
When I measure the script here, the measured time varies between 0.001 to 0.022

You can check your own script by logging dzVents and do some counting or you can do like this

After

Code: Select all

	execute = function(domoticz, device, triggerInfo)
Insert

Code: Select all

		local x = os.clock()
At the very few lines of the script... before

Code: Select all

	end
}
Insert

Code: Select all

domoticz.log(string.format("elapsed time: %.3f\n", os.clock() - x), domoticz.LOG_FORCE)
Best wishes

// บักสีดา

R0yk3
Posts: 39
Joined: Sunday 24 July 2016 21:51
Target OS: Raspberry Pi
Domoticz version: beta
Location: the Netherlands
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by R0yk3 » Thursday 24 August 2017 8:53

BakSeeDaa, the thought behind the question was to have less stress on the rpi. Do not run scripts when it is not necessary to run them. Less running scripts, less logging, less possible errors, les cpu stress etc. etc...
Like put the scripts asleep until triggered, then run every minute until put to sleep..

User avatar
BakSeeDaa
Posts: 513
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by BakSeeDaa » Thursday 24 August 2017 15:18

R0yk3 wrote:
Thursday 24 August 2017 8:53
BakSeeDaa, the thought behind the question was to have less stress on the rpi. Do not run scripts when it is not necessary to run them. Less running scripts, less logging, less possible errors, les cpu stress etc. etc...
Like put the scripts asleep until triggered, then run every minute until put to sleep..
It's beyond the scope of this topic but if you want to prevent the execute part of the script from being executed you can put a function in the active-setting as described in the dzVents Wiki.

However such a function will run every cycle so I think you won't gain something, actually I believe it will be counterproductive.

If you really need to save some CPU cycles on a dzVents script I believe the only way is to have another script rename those scripts you want to put asleep (out of the dzVents scripts folder) during the time they are not needed. That would be effective.

Please start a new topic if you'd like to discuss optimizing dzVents scripts.

EDIT: Maybe just do like this will do:

After:

Code: Select all

execute = function(domoticz, device, triggerInfo)
Add:

Code: Select all

if domoticz.devices('Presence Detected').state == 'On' then return end
This example assumes that you have a Domoticz device named 'Presence Detected' that is on when you are present.
Best wishes

// บักสีดา

YcKe
Posts: 44
Joined: Sunday 03 July 2016 0:53
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by YcKe » Wednesday 06 September 2017 20:09

Hi BakSeeDaa,

thanks a lot for your example script!

I think you made a little error in the script:

Code: Select all

if ((forceFanReadings.getLatest().time.minutesAgo < FAN_MAX_TIME)
         and (forceFanReadings.getLatest().data == 'On')) then fanCmd = 'On' end
FAN_MAX_TIME should be replaced with FORCE_FAN_TIME as we are checking to see if the forced time on is not exceeded.

The if statement eventually should look like this:

Code: Select all

if ((forceFanReadings.getLatest().time.minutesAgo < FORCE_FAN_TIME)
			and (forceFanReadings.getLatest().data == 'On')) then fanCmd = 'On' end
Greetz YcKe

User avatar
BakSeeDaa
Posts: 513
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by BakSeeDaa » Thursday 07 September 2017 15:28

YcKe wrote:
Wednesday 06 September 2017 20:09
Hi BakSeeDaa,

thanks a lot for your example script!

I think you made a little error in the script:

Code: Select all

if ((forceFanReadings.getLatest().time.minutesAgo < FAN_MAX_TIME)
         and (forceFanReadings.getLatest().data == 'On')) then fanCmd = 'On' end
FAN_MAX_TIME should be replaced with FORCE_FAN_TIME as we are checking to see if the forced time on is not exceeded.

The if statement eventually should look like this:

Code: Select all

if ((forceFanReadings.getLatest().time.minutesAgo < FORCE_FAN_TIME)
			and (forceFanReadings.getLatest().data == 'On')) then fanCmd = 'On' end
Greetz YcKe
Thanks YcKe. I will correct that! :D
Best wishes

// บักสีดา

User avatar
BakSeeDaa
Posts: 513
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by BakSeeDaa » Thursday 07 September 2017 15:36

Updated the script to version 1.3.1. Please see the release notices for details.
Best wishes

// บักสีดา

D3LTA
Posts: 34
Joined: Monday 15 July 2013 2:37
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: Twente
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by D3LTA » Tuesday 26 September 2017 9:53

2 questions..

How can i use it with 1 humidity sensor? tried just using 1 but script calculates delta from 2 devices and im not skilled enough to chaneg that.. i fixed it now by just adding the humidity sensor name 2 times..

and what is "READING_SAMPLES = 15" ?

Skippiemanz
Posts: 278
Joined: Sunday 14 July 2013 20:21
Target OS: Raspberry Pi
Domoticz version: 3.5859
Location: Alkmaar, The Netherlands
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by Skippiemanz » Wednesday 15 November 2017 0:01

Slorf wrote:
Sunday 16 July 2017 17:07
Hi,

I am trying to get the script to work, but unsuccessfully :-(

Our fanbox has 3 settings.

Setting 1: no one home ( only use this when we are on vacation, manually )
Setting 2: normal setting
Setting 3: shower of cooking

Plan is to have a remote in every ( 2 ) bathroom and a remote in the kitchen, when a remote is pressed the fanbox goes to setting 3, what I do not understand is how the targetHum can be higher then the Current Humidity, I am using dzVents 1.1

I did not change anything in the script except following lines:

local FAN_DEVICE = 'Ventilatie-3-Virtual' -- Fan device ( this a virtual switch that controls the fanbox settings )
local FORCE_FAN_DEVICE = 'Ventilatie-Remote-Virtual' -- (Optional) ( this is a virtual switch that goes on when I press a remote )
local FORCE_FAN_TIME = 45 -- Minutes to force the fan when button pushed
local HUMIDITY_SENSORS = {'Badkamer', 'Douche'}
local FAN_DELTA_TRIGGER = 5 -- % Rise in humidity that will trigger the fan
local FAN_MAX_TIME = 45 -- Maximum minutes that the fan can be on in case we never reach the target humidity
local TARGET_OFFSET = 1 -- Fan goes off if target + offset is reached
local TEST_MODE_HUMIDITY_READING = 0 -- Set to a value between 1 and 100. Set to 0 to disable test mode
local DEBUG = true
local READING_SAMPLES = 15

When I run the script it starts the fanbox all the time

2017-07-16 17:01:00.466 LUA: =====================================================

2017-07-16 17:01:00.466 LUA: >>> Handler: fanspeed

2017-07-16 17:01:00.466 LUA: .....................................................

2017-07-16 17:01:00.479 LUA: Dehumidification program doesn't run for: Badkamer

2017-07-16 17:01:00.479 LUA: targetHum: 74, Current humidity: 73, humDelta: 0

2017-07-16 17:01:00.480 LUA: Dehumidification program doesn't run for: Douche

2017-07-16 17:01:00.480 LUA: targetHum: 68, Current humidity: 67, humDelta: 0

2017-07-16 17:01:00.480 LUA: Turning the fan On

2017-07-16 17:01:00.485 LUA: .....................................................

2017-07-16 17:01:00.485 LUA: <<< Done

2017-07-16 17:01:00.485 LUA: -----------------------------------------------------

2017-07-16 17:01:00.486 LUA: [1] = Ventilatie-3-Virtual: On

2017-07-16 17:01:00.486 LUA: =====================================================

2017-07-16 17:01:00.487 EventSystem: Script event triggered: /home/domoticz/domoticz/scripts/lua/script_time_main.lua
I'm trying to do the same thing, i have a fan under a selector switch. it has 3 levels, off, medium, maximum

I have the following script, the only thing i cant find out is where to putt the off level?! Maybe @BackSeeDaae knows?:
Spoiler: show

Code: Select all

--[[
bathroomHumControl.lua by BakSeeDaa
Version 1.3.1
	This script controls the humidity in a typical bathroom setting by detecting
	relative rises in humidity in a short period.
--]]

local FAN_DEVICE = 'Badkamer Ventilator' -- Fan device
local FORCE_FAN_DEVICE = 'Badkamer PIR' -- (Optional)
local FORCE_FAN_TIME = 10 -- Minutes to force the fan when button pushed
local HUMIDITY_SENSORS =  {'Badkamer Temp + Hum'}
local FAN_DELTA_TRIGGER = 3 -- % Rise in humidity that will trigger the fan
local FAN_MAX_TIME = 24 -- Maximum minutes that the fan can be on in case we never reach the target humidity
local TARGET_OFFSET = 3 -- Fan goes off if target + offset is reached
local TEST_MODE_HUMIDITY_READING = 0 -- Set to a value between 1 and 100. Set to 0 to disable test mode
local READING_SAMPLES = 15

-- Create the data declarations
local data = {}
for i, device in pairs(HUMIDITY_SENSORS) do
	data[device] = {history = true, maxItems = READING_SAMPLES + 1}
	data['dehumidProgramActive'..i] = {history = true, maxItems = 1} -- Need history to get time stamp
	data['forceFan'] = {history = true, maxItems = 1} -- Need history to get time stamp
	data['targetHum'..i] = {initial=0}
end

return {
	active = true,
	logging = {
		--level = domoticz.LOG_DEBUG, -- Select one of LOG_DEBUG, LOG_INFO, LOG_ERROR, LOG_FORCE to override system log level
		marker = "bathRoom"
	},
	on = {
		devices = {
			FORCE_FAN_DEVICE
		},
		timer = {
			'every minute'
		}
	},
	data = data,
	execute = function(domoticz, device, triggerInfo)
		local forceFanReadings = domoticz.data.forceFan

		if (triggerInfo.type == domoticz.EVENT_TYPE_TIMER) then
			local fanCmd = 'Off'
			for i = 1, #HUMIDITY_SENSORS do
				local humidityReadings = domoticz.data[HUMIDITY_SENSORS[i]]
				-- Store the read value in the persistant data
				for j = 1, (humidityReadings.size == 0 and READING_SAMPLES + 1 or 1) do
					humidityReadings.add((TEST_MODE_HUMIDITY_READING == 0
						and domoticz.devices(HUMIDITY_SENSORS[i]).humidity or TEST_MODE_HUMIDITY_READING))
				end

				-- INIT
				local programActiveReadings = domoticz.data['dehumidProgramActive'..i]
				if (programActiveReadings.size == 0) then
					domoticz.log('programActiveReadings, Initialization was needed', domoticz.LOG_INFO)
					programActiveReadings.add(false)
				end
				local targetHum = domoticz.data['targetHum'..i]
				if (targetHum == nil) then
					domoticz.log('targetHum'..i..', Initialization was needed', domoticz.LOG_INFO)
					domoticz.data['targetHum'..i] = 0
					targetHum = 0
				end
				if (forceFanReadings.size == 0) then
					domoticz.log('forceFanReadings, Initialization was needed', domoticz.LOG_INFO)
					forceFanReadings.add('Init')
				end

				local programActiveState = programActiveReadings.getLatest()
				if (programActiveState.data) then -- The fan control program is active
					-- Has the fan control program timed out or have we reached the target humidity?
					local maxTime = (programActiveState.time.minutesAgo > FAN_MAX_TIME)
					local targetHumReached = (humidityReadings.getLatest().data <= targetHum)
					if (maxTime or targetHumReached) then
						domoticz.log('Dehumidification program stops for: '..HUMIDITY_SENSORS[i], domoticz.LOG_INFO)
						domoticz.log('Reason(s): '..(maxTime and 'Max time. ' or '')..(targetHumReached and 'Target humidity reached.' or ''), domoticz.LOG_INFO)
						programActiveReadings.add(false)
						programActiveState = programActiveReadings.getLatest()
					else
						domoticz.log('Dehumidification program is active for: '..HUMIDITY_SENSORS[i], domoticz.LOG_INFO)
						fanCmd = 'On'
					end
				else -- The fan is currently not running under the control of this program
					-- Has there been a significant rise in humidity lately?
					local humDelta = humidityReadings.getLatest().data - humidityReadings.min(2, READING_SAMPLES + 1)
					-- Calculate a target humidity but never try to push humidity below 40
					targetHum = math.max(humidityReadings.min(2, READING_SAMPLES + 1) + TARGET_OFFSET, 40)
					if (humDelta > FAN_DELTA_TRIGGER and humidityReadings.getLatest().data > targetHum) then
						domoticz.data['targetHum'..i] = targetHum
						programActiveReadings.add(true)
						programActiveState = programActiveReadings.getLatest()
						fanCmd = 'On'
						domoticz.log('Dehumidification program starts as a respond to: '..HUMIDITY_SENSORS[i], domoticz.LOG_INFO)
					else
						domoticz.log('Dehumidification program doesn\'t run  for: '..HUMIDITY_SENSORS[i], domoticz.LOG_INFO)
					end
					domoticz.log('targetHum: '..targetHum..', Current humidity: '..humidityReadings.getLatest().data..', humDelta: '..humDelta, domoticz.LOG_INFO)
				end
			end

			if ((forceFanReadings.getLatest().time.minutesAgo < FORCE_FAN_TIME)
			and (forceFanReadings.getLatest().data == 'On')) then fanCmd = 'On' end

			if (domoticz.devices(FAN_DEVICE).state ~= fanCmd) then
				domoticz.log('Turning the fan '..fanCmd, domoticz.LOG_INFO)
				domoticz.devices(FAN_DEVICE).switchSelector(20)
			end
		else
			-- The script gets executed due to a device-change event
			if (device.name == FORCE_FAN_DEVICE and device.state == 'On') then
				forceFanReadings.add('On')
				domoticz.log('The fan has been forced on for '..FORCE_FAN_TIME..'minutes.', domoticz.LOG_INFO)
				-- domoticz.helpers.speak(domoticz, 'joke sting')
				if (domoticz.devices(FAN_DEVICE).state ~= 'On') then domoticz.devices(FAN_DEVICE).switchSelector(10) end
			end
		end

	end
}

User avatar
BakSeeDaa
Posts: 513
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by BakSeeDaa » Wednesday 15 November 2017 9:06

D3LTA wrote:
Tuesday 26 September 2017 9:53
How can i use it with 1 humidity sensor? tried just using 1 but script calculates delta from 2 devices and im not skilled enough to chaneg that.. i fixed it now by just adding the humidity sensor name 2 times..
Just define a single sensor in the HUMIDITY_SENSORS table.
In the example, there are two sensors. So, remove one of them. E.g.

Code: Select all

local HUMIDITY_SENSORS =  {'My single sensor'}
D3LTA wrote:
Tuesday 26 September 2017 9:53
and what is "READING_SAMPLES = 15" ?
The script compares the current humidity with recent readings to determine if there has been a significant rise in humidity. Those readings will be within the latest 15 minutes if READING_SAMPLES is set to 15 and the script runs every minute.
Best wishes

// บักสีดา

User avatar
BakSeeDaa
Posts: 513
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: My dzVents Bathroom Humidity Control script

Post by BakSeeDaa » Wednesday 15 November 2017 9:19

Skippiemanz wrote:
Wednesday 15 November 2017 0:01
I have the following script, the only thing i cant find out is where to putt the off level?! Maybe @BackSeeDaae knows?:
As you've experienced, the script isn't designed for controlling fans with more than two states (On and Off).

Of course it can be modified to handle several states but I haven't had the time to do so. I could put the source on gitHub if someone care to collaborate to implement that functionality.
Best wishes

// บักสีดา

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests