Hi all,
(sorry for writing a book..
)
I'm new to Domoticz and this Forum. Reason I got interested in Domoticz is that we recently bought a new house and I would like to (over time) turn it into a 'smarter' house.
So, my first projects is the mechanical ventilation. The mechanical ventilation has inlets in the toilet, kitchen and bathroom, but only 1 three-way switch in the kitchen
So I would like to automate this and I had the following setup in mind:
- Control the vent-box on its 0-10v input using Qubino 0-10v dimmer
- Humidity sensor in the bathroom
- PIR sensor in the toilet
I know there are plenty of scripts around that you can copy-paste, but you don't learn from copy-pasting, so I decided to have a look at some examples and dive right in. The script I came up with works pretty well and does what it's supposed to do, but I would like some feedback from the LUA-masters on ways to improve the script, or other tips and tricks with regards to the usage of LUA, for example: I only use 'otherdevices' in my script, could I also use 'devicechanged'? I'm not sure about the difference in usage there. Also, what's the difference in declaring variables 'local' or not?
Here's the script:
I don't have the components yet I went ahead and set everything up using virtual switches/sensors. I used a dimmer to emulate the hum-sensor, so I understand at some point I will have to switch that out for the actual sensor and adjust the way the humidity is extracted...
It uses:
- Dimmer to control the vent-box
- Regular switch to activate a temporary ventilation boost on demand (using the timer function to turn it off automatically after, say, 30 mins)
- Switch that will be triggered by a PIR in the toilet. (using the timer function to turn it off automatically after, say, 15 mins)
- Dimmer to emulate a humidity sensor
Code: Select all
commandArray = {}
-- Variables
local debug = true
local vent_speed_min = 15
local vent_speed_max = 85
local vent_speed_toilet = 50
local vent_speed_boost = 85
local humidity_trigger = 60
local humidity_step = 5
-- Devices
local vent_switch = 'Vent'
local vent_boost_switch = 'VentBoost (v)'
local toilet_visit_switch = 'Toilet bezoek (v)'
local hum_sensor = 'Badkamer hum'
-- Functions 'borrowed' from @jvdz
-- round
function Round(num, idp)
return tonumber(string.format("%." ..(idp or 0).. "f", num))
end
-- debug print
function dprint(text)
if debug then print("@VSM:"..text) end
end
print('VentScript Module')
-- Internal variables
local new_fan_speed = 0
local current_vent_speed = tonumber(otherdevices_svalues[vent_switch])
local bathroom_hum = tonumber(otherdevices_svalues[hum_sensor])
local hum_vent_ratio = (vent_speed_max - vent_speed_min) / (100 - humidity_trigger)
dprint('Calculated vent speed ratio: '.. hum_vent_ratio)
-- If the fan switch is set to Off, do nothing and return
if otherdevices[vent_switch] == "Off" then
return
end
-- If the VentBoost switch is activated
if otherdevices[vent_boost_switch] == "On" then
new_fan_speed = vent_speed_boost
dprint('The VentBoost switch is activated. Setting new fan speed: '..new_fan_speed)
end
-- Someone went to the toilet
if otherdevices[toilet_visit_switch] == "On" and new_fan_speed < vent_speed_toilet then
new_fan_speed = vent_speed_toilet
dprint('Someone on the toilet and fan speed lower than toilet setting. Setting new fan speed: '..new_fan_speed)
end
-- Determine humidity fan speed
if bathroom_hum >= humidity_trigger then
humidity = bathroom_hum
if (bathroom_hum % humidity_step) > 0 then
humidity = bathroom_hum + humidity_step - (bathroom_hum % humidity_step)
-- Calculated humidity can be greater than 100 because of humidity_step size
if humidity > 100 then
humidity = 100
end
end
hum_fan_speed = Round(vent_speed_min + ((humidity-humidity_trigger) * hum_vent_ratio))
dprint('Actual humidity='..bathroom_hum..'; Humidity used for calculation='..humidity)
dprint('Calculated Hum vent speed: '..hum_fan_speed)
if hum_fan_speed > new_fan_speed then
new_fan_speed = hum_fan_speed
dprint('Humidity is higher then '..humidity_trigger..'%. Setting new fan speed: '..new_fan_speed)
end
end
-- No new fan speed has been set based on above criteria, so set new fan speed to minimum/idle speed
if new_fan_speed == 0 then
new_fan_speed = vent_speed_min
dprint('No new fan speed is set. Setting new fan speed to MIN: '..new_fan_speed)
end
-- If the new fan speed not equals current speed, update the speed
if new_fan_speed ~= current_vent_speed then
commandArray[vent_switch]='Set Level: '..tostring(new_fan_speed)..' %'
print('New fan speed not equals current speed. Update vent speed to: '..new_fan_speed)
end
return commandArray
Thanks for any feedback or comments you may have.
Edit:
Updated the script to include a humidity step size, as I probably don't wan't my vent speed to change on every % humidity change (to save battery).