I have a virtual switch that's set saving(besparing) on/off (or saving or manual)
my wish:
when nodody is home some lights must off.
when somebody is home some lights must not on for longer then x
when it's in the middle off the day some lights but never go on.
I have tried the code below and it's working. but it's not really smart and efficient.
My question is if other people have tips for making the code smaller and efficient. and also more modular so other users can obtain it more easy (rename some variables instead of lots of hardcoded names.)
Code: Select all
-- Save money
--------------------------------------------------------------------------------
commandArray = {}
package.path = package.path .. ';' .. '/home/pi/domoticz/scripts/lua/?.lua'
My = require('MyFunc')
-- Variable
Script = 'Besparing' --
Version = 1 --
Notifications = uservariables["Notifications"] -- On/Off
minuten_toegestaan = 15 -- aantal minuten 180 = 3 uur
StateDevice = 'Iemand_thuis'
TH_Hour1 = 8 -- Treshold hour
TH_Hour2 = 10 -- Treshold hour
Nu_Hour =tonumber(os.date('%H',time))
LastChange_Gang =otherdevices_lastupdate['Gang']
LastChange_Slaapkamer =otherdevices_lastupdate['Slaapkamer']
LastChange_Trapgat =otherdevices_lastupdate['Trapgat']
Debug = 'Y'
if ((Nu_Hour >= TH_Hour1) or (Nu_Hour <= TH_Hour2)) then Res_Hour=1 else Res_Hour=0 end
if (otherdevices['Slaapkamer']) == 'On' then Slaapkamer=1 else Slaapkamer=0 end -- omgedraait omdat het ook DO set dimmer level to 80% kan hebben ipv on.
if (otherdevices['Gang']) == 'On' then Gang=1 else Gang=0 end
if (otherdevices['Trapgat']) == 'On' then Trapgat=1 else Trapgat=0 end
if (otherdevices['Iemand_thuis']) == 'On' then Aanwezig=1 else Aanwezig=0 end
TDiffGang = My.Round(My.TimeDiff(os.time(),LastChange_Gang)/60,0)
TDiffSlaapkamer = My.Round(My.TimeDiff(os.time(),LastChange_Slaapkamer)/60,0)
TDiffTrapgat = My.Round(My.TimeDiff(os.time(),LastChange_Trapgat)/60,0)
--logging
print ('______________________________________________________________________')
print('<b style="color:Blue"> '.. Script .. ".......v" .. Version .. "..Debugging= " .. Debug .. '</b>')
if (otherdevices['Bespaar_modus']) == 'Off' then print (". Besparing wordt niet toegepast. ") goto done end
if Debug == 'Y' then
print ( ". " .. Aanwezig .. " - Iemand thuis? " )
print ( ". " .. Slaapkamer .. " - Slaapkamer " )
print ( ". " .. Gang .. " - Gang" )
print ( ". " .. Trapgat .. " - Trapgat" )
print ( ". " .. Res_Hour .. " - Tijd : " .. Nu_Hour .. " <= " .. TH_Hour1 .. " and " .. Nu_Hour .. " >= " .. TH_Hour2 )
print ( ". Gang : " .. TDiffGang .. " minuten geleden naar: " .. (otherdevices['Gang']) )
print ( ". Slaapkamer : " .. TDiffSlaapkamer .. " minuten geleden naar: " .. (otherdevices['Slaapkamer']) )
print ( ". Trapgat : " .. TDiffTrapgat .. " minuten geleden naar: " .. (otherdevices['Trapgat']) )
else
end
IetsAan = Slaapkamer + Gang + Trapgat
-- Binnen gestelde tijd
if ((IetsAan >= 1) and (Res_Hour==1)) then
if ((TDiffSlaapkamer > minuten_toegestaan) and (TDiffSlaapkamer < minuten_toegestaan+10)) and (Slaapkamer==1) then
commandArray['SendNotification']='Bespaaralert!#Slaapkamer staat al ' .. minuten_toegestaan .. ' minuten aan!#0#pushover'
print(". Slaapkamer staat al " .. TDiffSlaapkamer .. " minuten aan. Minuten_toegestaan = " .. minuten_toegestaan .. ".")
commandArray['Slaapkamer']='Off'
else
if ((TDiffGang > minuten_toegestaan) and (TDiffGang < minuten_toegestaan+10)) and (Gang==1) then
commandArray['SendNotification']='Bespaaralert!#Gang staat al ' .. minuten_toegestaan .. ' minuten aan!#0#pushover'
print(". Gang staat al " .. TDiffGang .. " minuten aan. Minuten_toegestaan = " .. minuten_toegestaan .. ".")
commandArray['Gang']='Off'
else
if ((TDiffTrapgat> minuten_toegestaan) and (TDiffTrapgat < minuten_toegestaan+10)) and (Trapgat==1) then
commandArray['SendNotification']='Bespaaralert!#Trapgat staat al ' .. minuten_toegestaan .. ' minuten aan!#0#pushover'
print(". Trapgat staat al " .. TDiffTrapgat .. " minuten aan. Minuten_toegestaan = " .. minuten_toegestaan .. ".")
commandArray['Trapgat']='Off'
end
end
end
end
if ((Aanwezig == 0) and (IetsAan >= 1)) then
print(". Niemand thuis terwijl" .. TDiffSlaapkamer .. " aan stond.")
commandArray['SendNotification']='Bespaaralert!#Niemand aanwezig en devices aan!#1#pushover'
commandArray['Alles']='On'
end
::done::
return commandArray
