Page 1 of 1

Switch lights based on LUX

Posted: Sunday 13 November 2016 13:16
by JuanUil
Hi there,

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

Re: RE: Switch lights based on LUX

Posted: Tuesday 22 November 2016 9:25
by stlaha2007
JuanUil wrote:Hi there,

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 om op basis van lichtsterkte binnen lampen aan of uit te schakelen

-- Declaratie van variabelen
local schakeltijd = 0
local Hoog = uservariables["HoogLux"]
local Middel = uservariables["MiddelLux"]

-- de niet thuis verlichting schakelen we 's-nachts ook weer uit
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

Great script....

However apparently we're both native speaking Dutch. So for me, it's well documented/commented within. However to have others read/comment or modify to use your script, i think it's better to change comments "de niet thuis verlichting....." and variables "Hoog... Middel... schakeltijd..." into English.

Re: Switch lights based on LUX

Posted: Tuesday 22 November 2016 16:27
by havnegata
Agree on that, would be very helpful

Re: RE: Re: Switch lights based on LUX

Posted: Tuesday 22 November 2016 17:23
by stlaha2007
havnegata wrote:Agree on that, would be very helpful Image
hi, a scandinavian member (i guess Sweden)....
OT: Just answered another post from you... Just saw you also use the RPi's instead of Windows ;-)

Re: Switch lights based on LUX

Posted: Wednesday 23 November 2016 8:45
by havnegata
Norway, actually, and yes, I switched to RPi because of the many "limitations" I encountered on Windows :-)

Re: RE: Switch lights based on LUX

Posted: Wednesday 23 November 2016 11:36
by JuanUil
stlaha2007 wrote:Hi there,

Great script....

However apparently we're both native speaking Dutch. So for me, it's well documented/commented within. However to have others read/comment or modify to use your script, i think it's better to change comments "de niet thuis verlichting....." and variables "Hoog... Middel... schakeltijd..." into English.
Translated the Dutch into Englisch and added some more comments in Englisch

Enjoy!!

Re: Switch lights based on LUX

Posted: Wednesday 23 November 2016 13:45
by Nautilus
Thanks for sharing! On a quick check, it looks like you've defined the same timedifference function three times :) Or is there something I missed there?

Re: Switch lights based on LUX

Posted: Wednesday 23 November 2016 20:55
by JuanUil
Nautilus wrote:Thanks for sharing! On a quick check, it looks like you've defined the same timedifference function three times :) Or is there something I missed there?
Correct, one for High, one for medium ND 1 FOR LOW LUX

Re: Switch lights based on LUX

Posted: Wednesday 23 November 2016 21:15
by Nautilus
JuanUil wrote:
Correct, one for High, one for medium ND 1 FOR LOW LUX
Ok, I thought the functions are the same though, the time difference between os.time() and the date parsed from s. So I though you could just use one function, like this:

Code: Select all

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

-- 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 = Htimedifference(otherdevices_lastupdate['Sidetable'])
DifferenceLA = Htimedifference(otherdevices_lastupdate['Lampen Achter (Raam)'])
DifferenceLV = Htimedifference(otherdevices_lastupdate['Lampen Voor (Muur)'])
DifferenceLVdag = Htimedifference(otherdevices_lastupdate['Dressoir'])
Sorry if I missed something and sure it works ok with separate functions as well...:)

Re: Switch lights based on LUX

Posted: Thursday 24 November 2016 10:28
by JuanUil
Hi Nautilus,
you are right, makes the program at least a little smaller, thanx for the tip

Re: Switch lights based on LUX

Posted: Thursday 24 November 2016 10:32
by Nautilus
JuanUil wrote:Hi Nautilus,
you are right, makes the program at least a little smaller, thanx for the tip
No problem, I was also curious if there was some special purpose for the functions :)

Re: Switch lights based on LUX

Posted: Friday 25 November 2016 16:37
by JuanUil
Hi There all,

found a big problem for switching light on and off when we are not at home.
At midnight + 5 minutes lights should be switched off.
But the light switch on immidiatly because of this mistake.
in below code the problem is fixed. Alteration is in Switching some light when we are away. nearly at the bottom Where I have added "and uur>17"
sorry for any inconvienience...

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 uur > 17 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

Re: Switch lights based on LUX

Posted: Friday 15 November 2019 22:45
by Tamil112
hi,

is it also possible to work. with motion and based on lux ?


JuanUil wrote: Sunday 13 November 2016 13:16 Hi there,

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

Re: Switch lights based on LUX

Posted: Monday 18 November 2019 17:12
by JuanUil
hi,

is it also possible to work. with motion and based on lux ?
don't exactly know what you mean. What do you want to do?

Re: Switch lights based on LUX

Posted: Tuesday 19 November 2019 18:41
by Tamil112
This scripts works only based on LUX . but i would like to add a Motion sensor also

so it will work based on lux and motion
so when there is motion and lux is to low then light will turn on
and stay active so long the lux is to low
and turn of the light when no motion is deteced and lux is to high

and turn of the lights when there is no motion for x time and lux is to low..

and one more quistion is it also possible to work with scene/groups? instead of devices( lights)

Re: Switch lights based on LUX

Posted: Tuesday 19 November 2019 21:33
by Tamil112
Never mind i got it working... with motion

another question when turning off lights is it possible it will decrease brightness ( like the philips hue does it with when its working with motion)

Re: Switch lights based on LUX

Posted: Wednesday 15 January 2020 20:13
by BarryT
I do not use the lux due some reasons..
For example, when the sun is down and there is coming a big rain and its getting a little darker and my girlfriend is walking trough the house/garage/whatever, the lights goes on.
So, what i did is just a "when its dark" virtual switch and let that choose the lights goes on on motion or stays off.
This is working perfectly for some years :)

Re: Switch lights based on LUX

Posted: Monday 03 February 2020 22:09
by Tamil112
Hi Guys

i was using this script for some time,

but from today i got the fowolling error:
bad argument #1 to 'sub' (string expected, got nil)
on the red marked line


function timedifference(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}
difference = os.difftime (t1, t2)
return difference
end

Re: Switch lights based on LUX

Posted: Sunday 04 October 2020 14:46
by wiecher
Hoi Tamil112,

I also want to use this script but I get the same error message just like you. Have you solved the problem yet? If so what is wrong with the script. Thanks in advance. Wiecher

Re: Switch lights based on LUX

Posted: Sunday 04 October 2020 20:55
by waaren
wiecher wrote: Sunday 04 October 2020 14:46 I also want to use this script but I get the same error message
Can you try it with below script. It will probably not solve the issue but might give a clue why the issue occurs.

Code: Select all

-- -- ~/domoticz/scripts/lua/script_device_AutomatischLicht.lua
-- script to switch your room lights based on LUX

-- Declaration of 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 timeDifference(deviceName)
    local s
    if not otherdevices_lastupdate[deviceName] then
        print('No lastupdate found for ' .. tostring(deviceName))
        return 0
    else
        s =  otherdevices_lastupdate[deviceName]
        print('Lastupdate for ' .. tostring(deviceName) .. ' is: ' .. tostring(s) )
    end
    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}
    return os.difftime (t1, t2)
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 = timedifference('KastVerlichting')
DifferenceM = timedifference('Sidetable')
DifferenceLA = timedifference('Lampen Achter (Raam)')
DifferenceLV = timedifference('Lampen Voor (Muur)')
DifferenceLVdag = timedifference('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