Switch lights based on LUX

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

Post Reply
JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Switch lights based on LUX

Post 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
Last edited by JuanUil on Wednesday 23 November 2016 11:33, edited 1 time in total.
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
stlaha2007
Posts: 370
Joined: Monday 05 October 2015 10:16
Target OS: -
Domoticz version:
Contact:

Re: RE: Switch lights based on LUX

Post 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.
User avatar
havnegata
Posts: 114
Joined: Wednesday 10 September 2014 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10162
Location: Norway
Contact:

Re: Switch lights based on LUX

Post by havnegata »

Agree on that, would be very helpful
stlaha2007
Posts: 370
Joined: Monday 05 October 2015 10:16
Target OS: -
Domoticz version:
Contact:

Re: RE: Re: Switch lights based on LUX

Post 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 ;-)
User avatar
havnegata
Posts: 114
Joined: Wednesday 10 September 2014 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10162
Location: Norway
Contact:

Re: Switch lights based on LUX

Post by havnegata »

Norway, actually, and yes, I switched to RPi because of the many "limitations" I encountered on Windows :-)
JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: RE: Switch lights based on LUX

Post 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!!
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Switch lights based on LUX

Post 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?
JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: Switch lights based on LUX

Post 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
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Switch lights based on LUX

Post 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...:)
JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: Switch lights based on LUX

Post by JuanUil »

Hi Nautilus,
you are right, makes the program at least a little smaller, thanx for the tip
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Switch lights based on LUX

Post 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 :)
JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: Switch lights based on LUX

Post 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
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
Tamil112
Posts: 10
Joined: Friday 15 November 2019 14:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Switch lights based on LUX

Post 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
JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: Switch lights based on LUX

Post 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?
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
Tamil112
Posts: 10
Joined: Friday 15 November 2019 14:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Switch lights based on LUX

Post 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)
Tamil112
Posts: 10
Joined: Friday 15 November 2019 14:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Switch lights based on LUX

Post 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)
BarryT
Posts: 358
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Switch lights based on LUX

Post 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 :)
Raspberry / ESP Boards / Relais / Milight / Hue / OTGW / P1 / Xiaomi / RFXCom / RFLink / ZWave / Conbee II / Z2M / MQTT / A lot of scripts and many more..
Software: Linux, Android and Windows
Tamil112
Posts: 10
Joined: Friday 15 November 2019 14:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Switch lights based on LUX

Post 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
wiecher
Posts: 1
Joined: Friday 03 July 2020 7:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Switch lights based on LUX

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Switch lights based on LUX

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest