Automate sunscreen blinds shades

From Domoticz
Jump to navigation Jump to search

Purpose

This script is used to Automate sunscreen, blinds, shades.

This script has been taken from here

Dependencies - hardware / software / operating system

This script will work on all type of hardware / software / operating system.

Domoticz Setup - switches, variables, version

Variable / Thresholds declaration.

Reading weather underground devices.

Split values from these devices.

Calculate results with thresholds

Log results to Domoticz log

Check if manual Override is active: - Less than x minutes - Only during Daytime or Always

When screen is Up/Open: - Don't close it within x minutes after open - Close screen - Optional Log to log file (conditions and results) - Optional Send Telegram notification

When screen is Down/Closed: - Open screen - Optional Log to log file (conditions and results) - Optional Send Telegram notification

Installation instructions

Copy this script in your domoticz\scripts\lua\ folder.

Script with comments

-- Time_Sunscreen
--
-- Check the weather conditions with Weather Underground for controlling the suncreen.
--
-- Release note
--
-- 14-06-2014  Redesigned for generic use
-- 17-06-2014  Include a Temperature condition
--             Manual override setting for daytime only
-- 26-06-2014  Optional waiting time after open
--             Option for logging to log file
--
--------------------------------------------------------------------------------
--
--      Variable
--
Version       = 0.021
Version_Type  = 'Beta'
TH_Wind       = 30      -- Treshold wind
TH_Gust       = 50      -- Treshold windstoten
TH_Rain       = 0           -- Treshold regen/neerslag
TH_Uv_Close   = 2.5          -- treshold voor Uv close
TH_Uv_Open    = 1.2          -- treshold voor Uv open   
TH_Tp_Close   = 20          -- treshold voor Temperature close
TH_Tp_Open    = 15          -- treshold voor Temperature close

ActionClose   = 'Close'
ActionOpen    = 'Open'
ManualTime    = 'D'             -- D=Daytime / A=All
Debugging     = 'Y'
Man_to_Auto   = 240             -- Minutes manual override
SwitchTime    = 10
LogAction     = 'Y'             -- A=All, Y=Only on change

LogFile       = '/home/pi/domoticz/Log/Sunscreen.log'
DeviceName    = 'Sunscreen'
DeviceManual  = 'Sunscreen - Manual'
TelegramName  = 'Leon_Mol'
--
-- 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']
--
--------------------------------------------------------------------------------
commandArray = {}
print ("______________________________________________________________________")
print (">> Sunscreen LUA Contitions.....v" .. Version .. "....." .. Version_Type)
--
-- 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")
      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: Down / Close")
                           commandArray[DeviceName] = ActionClose
                           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 TelegramName == nil then else
                              --
                              -- Telegram notification for close
                              --
                              os.execute('su pi ./home/pi/domoticz/scripts/lua/Telegram.sh ' .. TelegramName .. ' "Sunscreen closed."')
            end
                        end
                end
   else
      print ("Action: None")
   end
end
if (otherdevices[DeviceName]) == 'Closed' then
   print ("Current state: Down / Closed")
   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 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 TelegramName == nil then else
                           --
                           -- Telegram notification for open
                           --
                           os.execute('su pi ./home/pi/domoticz/scripts/lua/Telegram.sh ' .. TelegramName .. ' "Sunscreen open."')
                        end
                end
   else
      print ("  Action: None")
   end
end
if LogAction=='A' then
                              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

Example of use (if relevant) i.e. output files / screen displays

N/A