the script is good enough but can always better

So if you have an idea, you see a bug of something else. let me know.
Code: Select all
--todo:
-- beter explanation
-- pushover integration
-- buienradar als crosscheck (apart script wat de virtuele sensors op 0 tot 255 zet.
-- alert/debug when nill values.
-- making loggin better readable
-- When in debug modus control the same only for a virtual sunscreen/blind
-- use the sombody @ home virtual switch
-- make coding more readable and maybe easier (with functions or something)
-- Questions or ideas? contact : remco84 @ Gmail.com
--------------------------------------------------------------------------------
-- Variable
----------------------------------------------------------------------------------------------------------------
Version = 0.027
Version_Type = 'Beta'
-- config ---------------------------------------------------------
TH_Wind = uservariables["TH_Wind"] + 0 -- Treshold wind
TH_Gust = uservariables["TH_Gust"] + 0 -- Treshold windstoten
TH_Rain = uservariables["TH_Rain"] + 0 -- Treshold regen/neerslag
TH_Uv_Close = uservariables["TH_Uv_Close"] + 0 -- 2.5 -- treshold voor Uv close
TH_Uv_Open = uservariables["TH_Uv_Open"] + 0 -- 1.5 -- treshold voor Uv open
TH_Tp_Close = uservariables["TH_Tp_Close"] + 0 -- 20 -- treshold voor Temperature close
TH_Tp_Open = uservariables["TH_Tp_Open"] + 0 -- 19 -- treshold voor Temperature close
ActionClose = 'On'
ActionOpen = 'Off'
UseVoice = 'N'
ManualTime = uservariables["Zonnescherm_ManualTime"] -- 'D' D=Daytime / A=All
Debugging = uservariables["Zonnescherm_Debugging"] -- 'Y'
Man_to_Auto = uservariables["Man_to_Auto"] + 0 -- 240 Minutes manual override
SwitchTime = uservariables["Zonnescherm_SwitchTime"] + 0 -- 10
LogAction = uservariables["Zonnescherm_LogAction"] -- 'Y' A=All, Y=Only on change
Notifications = uservariables["Zonnescherm_Notifications"] -- N-no Y= enabled
LogFile = '/var/log/Sunscreen.log'
DeviceName = 'Zonnescherm'
DeviceNameDebug = 'ZonneschermDebug'
DeviceManual = 'Zonnescherm - Manual'
IemandThuis = 'IemandThuis'
--TelegramName = uservariables["TelegramName"]
-- Retrieve values from devices
Wu_Dev_Temp = otherdevices_svalues['WU Temperature']
Wu_Dev_Rain = otherdevices_svalues['WU Rain']
Wu_Dev_UvMe = otherdevices_svalues['WU UV']
Wu_Dev_UvMe = otherdevices_svalues['WU UV']
-- config ------------------------
commandArray = {}
print ("______________________________________________________________________")
print (">> Sunscreen LUA Conditions.....v" .. Version .. "....." .. Version_Type)
if Debugging=='Y' then print("Zonnescherm_Debugging = true") DeviceName = DeviceNameDebug else print("Debugging = false") end
if (otherdevices[DeviceName]) == 'Open' then print("Zonnescherm device: " .. DeviceName .. " is open") else print("Zonnescherm device: " .. DeviceName .. " is gesloten") end
-- Split values from devices
Part=1
for match in (Wu_Dev_Temp..';'):gmatch("(.-)"..';') do
if Part==3 then Wu_Wind = tonumber(match) end
if Part==4 then Wu_Gust = tonumber(match) end
if Part==5 then Wu_Temp = tonumber(match) end
Part=Part+1
end
Part=1
for match in (Wu_Dev_Rain..';'):gmatch("(.-)"..';') do
if Part==2 then Wu_Rain = tonumber(match) end
Part=Part+1
end
Part=1
for match in (Wu_Dev_UvMe ..';'):gmatch("(.-)"..';') do
if Part==1 then Wu_Uv = tonumber(match) end
Part=Part+1
end
if LogAction=='Y' or LogAction=='A' then
f=io.open(LogFile,"a")
end
--
-- Calculate pre results
--
if (timeofday['Daytime']) then Res_Dayl=1 else Res_Dayl=0 end
if Wu_Wind <= TH_Wind then Res_Wind=1 else Res_Wind=0 end
if Wu_Gust <= TH_Gust then Res_Gust=1 else Res_Gust=0 end
if Wu_Rain <= TH_Rain then Res_Rain=1 else Res_Rain=0 end
if Wu_Uv >= TH_Uv_Close then Res_UvCl=1 else Res_UvCl=0 end
if Wu_Temp >= TH_Tp_Close then Res_TpCl=1 else Res_TpCl=0 end
if Wu_Uv <= TH_Uv_Open then Res_UvOp=1 else Res_UvOp=0 end
if Wu_Temp <= TH_Tp_Open then Res_TpOp=1 else Res_TpOp=0 end
--
-- Print decision info in the log
--
print ( "These conditions are valid when sunscreen is up: " )
if (timeofday['Daytime']) then
print ( Res_Dayl .. " - Daytime : True" )
else
print ( Res_Dayl .. " - Daytime : False" )
end
print ( Res_Wind .. " - Wind : " .. Wu_Wind .. " <= Th : " .. TH_Wind )
print ( Res_Gust .. " - Gust : " .. Wu_Gust .. " <= Th : " .. TH_Gust )
print ( Res_Rain .. " - Rain : " .. Wu_Rain .. " <= Th : " .. TH_Rain )
print ( Res_UvCl .. " - UV Close : " .. Wu_Uv .. " >= Th : " .. TH_Uv_Close )
print ( Res_TpCl .. " - Temp Close : " .. Wu_Temp .. " >= Th : " .. TH_Tp_Close )
print ( "These conditions are valid when sunscreen is down: " )
print ( Res_UvOp .. " - UV Open : " .. Wu_Uv .. " <= Th : " .. TH_Uv_Open )
print ( Res_TpOp .. " - Temp Open : " .. Wu_Temp .. " <= Th : " .. TH_Tp_Open )
-- Manual override for x minutes
if (otherdevices[DeviceManual]) == 'On' then
now = os.date("*t")
T1 = os.time(now)
Man_to_Auto = Man_to_Auto * 60
s = otherdevices_lastupdate[DeviceManual]
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)
T2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
if os.difftime (T1,T2) > Man_to_Auto then
commandArray[DeviceManual] = 'Off'
end
if ManualTime~="D" and Res_Dayl==0 then
--
-- Outside daytime period, turn manual override off
--
commandArray[DeviceManual] = 'Off'
end
end
if (otherdevices[DeviceName]) == 'Open' then
print ("Current state: Up / Open")
if Res_Dayl + Res_Wind + Res_Gust + Res_Rain + Res_UvCl + Res_TpCl == 6 then
if (otherdevices[DeviceManual]) == 'On' then
print ("Sunscreen Manual Control")
else
t1 = os.time()
s = otherdevices_lastupdate[DeviceName]
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)
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
if difference <= SwitchTime then
print ("Recent open, wait for switchtime")
else
-- Close
print ("Action: Naar beneden")
commandArray[DeviceName] = ActionClose
if UseVoice=='Y' then
os.execute('su pi ./home/pi/domoticz/scripts/sh/dspeak.sh "SunDown" ')
end
if LogAction=='Y' or LogAction=='A' then
f:write(os.date("%Y-%m-%d;%H:%M:%S") .. ";DOWN;" .. Version .. ";" .. Res_Dayl .. ";" .. Res_Wind .. ";" .. Res_Gust .. ";" .. Res_Rain .. ";" .. Res_UvCl .. ";" .. Res_TpCl .. ";" .. Res_UvOp .. ";" .. Res_TpOp .. ";" .. Wu_Wind .. ";" .. Wu_Gust .. ";" .. Wu_Rain .. ";" .. Wu_Uv .. ";" .. Wu_Temp)
f:write("\n")
end
if Notifications == 'N' then else
commandArray['SendNotification']='Zonnescherm#Zonnescherm gaat uit (naar beneden)!#0#pushover'
end
end
end
else
print ("Action: None")
end
end
if (otherdevices[DeviceName]) == 'Closed' then
print ("Current state: naar beneden")
if Res_Dayl + Res_Wind + Res_Gust + Res_Rain < 4 or Res_UvOp==1 or Res_TpOp==1 then
if (otherdevices[DeviceManual]) == 'On' then
print ("Sunscreen Manual")
else
-- Open
print ("Action: Up / Open")
commandArray[DeviceName] = ActionOpen
if UseVoice=='Y' then
os.execute('su pi ./home/pi/domoticz/scripts/sh/dspeak.sh "SunUp" ')
end
if LogAction=='Y' or LogAction=='A' then
f:write(os.date("%Y-%m-%d;%H:%M:%S") .. ";OPEN;" .. Version .. ";" .. Res_Dayl .. ";" .. Res_Wind .. ";" .. Res_Gust .. ";" .. Res_Rain .. ";" .. Res_UvCl .. ";" .. Res_TpCl .. ";" .. Res_UvOp .. ";" .. Res_TpOp .. ";" .. Wu_Wind .. ";" .. Wu_Gust .. ";" .. Wu_Rain .. ";" .. Wu_Uv .. ";" .. Wu_Temp)
f:write("\n")
end
if Notifications == 'N' then else
commandArray['SendNotification']='Zonnescherm#Zonnescherm gaat in (omhoog)!#0#pushover'
end
end
else
print (" Action: None")
end
end
if LogAction=='A' then
-- DateTime ; State ; Version ; Res_Dayl ; Res_Wind ; Res_Gust ; Res_Rain ; Res_UvCl ; Res_TpCl ; Res_UvOp ; Res_TpOp ; Wu_Wind ; Wu_Gust ; Wu_Rain ; Wu_Uv ; Wu_Temp
f:write(os.date("%Y-%m-%d;%H:%M:%S") .. ";IDLE;" .. Version .. ";" .. Res_Dayl .. ";" .. Res_Wind .. ";" .. Res_Gust .. ";" .. Res_Rain .. ";" .. Res_UvCl .. ";" .. Res_TpCl .. ";" .. Res_UvOp .. ";" .. Res_TpOp .. ";" .. Wu_Wind .. ";" .. Wu_Gust .. ";" .. Wu_Rain .. ";" .. Wu_Uv .. ";" .. Wu_Temp)
f:write("\n")
f:close()
end
print ("______________________________________________________________________")
return commandArray