Make save_energy_script more efficient

Moderator: leecollings

Post Reply
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Make save_energy_script more efficient

Post by remb0 »

I have a script to save some money.
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
:idea:
gerard76
Posts: 53
Joined: Wednesday 22 March 2017 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Make save_energy_script more efficient

Post by gerard76 »

Well, most of the script is prints and debug. I understand this is useful while developing, but when you know the script works you can remove the debug code. The core could be:

Code: Select all

devices = { 'Gang', 'Slaapkamer', 'Trapgat' }

for i, device in pairs(devices) do
  state_duration = My.TimeDiff(os.time(), otherdevices_lastupdate[device]) / 60
  if otherdevices[device] ~= 'Off' and state_duration > max_minutes and state_duration < max_minutes + 10 then
    commandArray['SendNotification'] = 'Bespaaralert!#'..device..' staat al ' .. max_minutes .. ' minuten aan!#0#pushover'
    print(".  "..device.." staat al " .. state_for .. " minuten aan.  Minuten_toegestaan = " .. max_minutes .. ".")
    commandArray[device] = 'Off'
  end
end
Other tips could be:
  • StateDevice - is not referenced from the code
    if ((Nu_Hour >= conserve_from) or (Nu_Hour <= conserve_till)) -- EVERY hour is > 8 OR < 10, I think you mean 'and' in stead of 'or'
    Debug = 'Y' -- you use this as a boolean, so better make it true/false
    goto done -- might as well return, then you know it exits when you read that line in stead of scrolling to ::done::
    Variable naming could be better. StateDevice => somebody_home, TH_Hour1, TH_Hour2 => conserve_from, conserve_till, Nu_Hour => now, etc
    I have just started with lua 2 weeks ago, so I am not sure about convention, but usually its CamelCase or under_scores for variable names, but not both.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest