I have made a LUA script to switch my lights based on the light intensity measured by two Fibaro motion sensors.
You need two dummy switches "Automatic Light" (in mijn script "Automatisch Licht") and "Home" ("Thuis").
enjoy!!
Code: Select all
-- -- ~/domoticz/scripts/lua/script_device_AutomatischLicht.lua
-- script to switch your room lights based on LUX
-- Declaratiof variables
local schakeltijd = 0
local Hoog = uservariables["HoogLux"] --uservariable for upper LUX-level
local Middel = uservariables["MiddelLux"] --uservariable for medium LUX-level
local Laag = uservariables["LaagLux"] --uservariable for low LUX-level
local uur = tonumber(os.date("%H"));
local min = tonumber(os.date("%M"));
local LuxAchter = tonumber(otherdevices_svalues['Lux Achter']) --LUX in backroom
local LuxVoor = tonumber(otherdevices_svalues['Lux Voor']) --LUX in frontroom
local timeon1 = uservariables["Tijdschakel3"] -- uservarable for delaytime
local zononder = timeofday['SunsetInMinutes']+30
local nu = uur*60+min
local verschil = zononder - nu
local dag = tostring(os.date("%a"));
-- Functions to set delaytime for switching on High, Medium or Low LUX
function Htimedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
DifferenceH = os.difftime (t1, t2)
return DifferenceH
end
function Mtimedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
DifferenceM = os.difftime (t1, t2)
return DifferenceM
end
function Ltimedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
DifferenceL = os.difftime (t1, t2)
return DifferenceL
end
-- Delaytime is being triggerd bij lastupdate of a lamp in the section
-- Delaytime is set to prevent lights switching on and off when lux is within
-- range of High, Medium or Low levels.
DifferenceH = Htimedifference(otherdevices_lastupdate['KastVerlichting'])
DifferenceM = Mtimedifference(otherdevices_lastupdate['Sidetable'])
DifferenceLA = Ltimedifference(otherdevices_lastupdate['Lampen Achter (Raam)'])
DifferenceLV = Ltimedifference(otherdevices_lastupdate['Lampen Voor (Muur)'])
DifferenceLVdag = Ltimedifference(otherdevices_lastupdate['Dressoir'])
commandArray = {}
-- determine whether it is later then 7:00 and still before sunset+30 min.
-- if true then schakeltijd = 1 else it will be 0
if (uur > 7 and verschil>=0 and schakeltijd==0) then
schakeltijd=1
else
schakeltijd=0
end
-- if dummyswitch Home (Thuis) is on and it is later then 7:00 and still before
-- sunset+30 min and dummyswitch Automatic Lights (Automatisch Licht) is on then
-- the lights will be switched based on LUX-levels
if (otherdevices['Automatisch Licht']=='On' and schakeltijd==1 and otherdevices['Thuis']=='On') then
if (LuxAchter <= Hoog and DifferenceH > timeon1 and otherdevices['KastVerlichting']=='Off') then
commandArray['KastVerlichting']='On'
end
if (LuxAchter > Hoog+10 and DifferenceH > timeon1 and otherdevices['KastVerlichting']=='On') then
commandArray['KastVerlichting']='Off'
end
if (LuxAchter <= Middel and DifferenceM > timeon1 and otherdevices['Sidetable']=='Off') then
commandArray['Sidetable']='On'
end
if (LuxAchter > Middel+10 and DifferenceM > timeon1 and otherdevices['Sidetable']=='On' ) then
commandArray['Sidetable']='Off'
end
if (LuxAchter <= Laag and DifferenceLA > timeon1 and otherdevices['Lampen Achter (Raam)']=='Off') then
commandArray['Lampen Achter (Raam)']='On'
commandArray['Lamp Achter (Kast)']= 'On AFTER 300'
end
if (LuxAchter > Laag+10 and DifferenceLA > timeon1 and otherdevices['Lampen Achter (Raam)']=='On') then
commandArray['Lampen Achter (Raam)']='Off'
commandArray['Lamp Achter (Kast)']= 'Off AFTER 60'
end
-- Light on dressoir on when it is becoming dark during daytime
if (LuxVoor <= Laag and DifferenceLVdag > timeon1 and uur < 15 and otherdevices['Dressoir']=='Off') then
commandArray['Dressoir']='On'
end
-- and being switched of when it is becoming lighter.
if (LuxVoor > Laag+35 and DifferenceLVdag > timeon1 and otherdevices['Dressoir']=='On') then
commandArray['Dressoir']='Off'
end
-- Lights in front room on when it is becoming dark after 15:00 hrs.
if (LuxVoor <= Laag and DifferenceLV > timeon1 and uur >= 15 and otherdevices['Dressoir']=='Off') then
commandArray['Dressoir']='On'
commandArray['Lampen Voor (Muur)']='On AFTER 180'
commandArray['Lampen Voor (Raam)']= 'On AFTER 360'
end
-- als het na 15:00 uur donker wordt en de dressoir lampen zijn al aan dan ook de rest aan doen.
if (LuxVoor <= Laag and DifferenceLV > timeon1 and uur >= 15 and otherdevices['Dressoir']=='On' and otherdevices['Lampen Voor (Muur)']=='Off') then
commandArray['Lampen Voor (Muur)']='On'
commandArray['Lampen Voor (Raam)']= 'On AFTER 360'
end
if (LuxVoor > Laag+35 and DifferenceLV > timeon1 and uur >= 15 and otherdevices['Lampen (Voor Muur)']=='On') then
commandArray['Lampen Voor (Muur)']='Off'
commandArray['Dressoir']='Off'
commandArray['Lampen Voor (Raam)']= 'Off'
end
end
-- speakerlamp on when we are at home midweeks at 7:00 weekends at 8:00
if ((dag=='Mon' or dag=='Tue' or dag=='Wed' or dag=='Thu' or dag=='Fri')and uur >=7 and uur <=16 and otherdevices['Speaker']=='Off' and otherdevices['Thuis']=='On' ) then
commandArray['Speaker']='On'
end
if ((dag=='Sat'or dag=='Sun') and uur>=8 and uur <=16 and otherdevices['Speaker']=='Off' and otherdevices['Thuis']=='On') then
commandArray['Speaker']='On'
end
--Switching some light when we are away.
if (schakeltijd==0 and otherdevices['Thuis']=='Off' and otherdevices['Lampen Voor (Muur)']=='Off') then
commandArray['Lampen Voor (Muur)'] = 'On'
commandArray['Sidetable'] = 'On AFTER 300'
commandArray['Hal'] = 'On AFTER 150'
end
-- switching off the lights
if (uur==0 and min==5 and otherdevices['Thuis']=='Off'and otherdevices['Lampen Voor (Muur)']=='On') then
commandArray['Lampen Voor (Muur)'] = 'Off'
commandArray['Sidetable']='Off AFTER 300'
commandArray['Hal'] = 'Off AFTER 150'
end
return commandArray