Page 1 of 1

Sunscreen script v0.02

Posted: Friday 27 June 2014 11:23
by MrFrankie
Together with a friend I developed a Sunscreen script that checks the weather conditions and automate the screen.
Of course the script must be configured for your settings and device names (begin of script). But maybe you can use some parts.

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

This is a first release. If interesting I will post new versions.
Suggestions are Welcome.

Code: Select all

-- 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   = 'On'
ActionOpen    = 'Off'
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

Re: Sunscreen script v0.02

Posted: Friday 27 June 2014 15:49
by remb0
I use it already and it's really great! thanks mate. I look forward to comments and possible some feedback.

Re: Sunscreen script v0.02

Posted: Thursday 04 June 2015 18:12
by Plaagje
Got the following error:

Error: /home/pi/domoticz/scripts/lua/script_time_sunscreen.lua:192: attempt to index global 'f' (a nil value)
when opening

and
Error: /home/pi/domoticz/scripts/lua/script_time_sunscreen.lua:165: attempt to index global 'f' (a nil value)
when closing

the lines refer to:

Code: Select all

                              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)
and

Code: Select all

                              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)
this error shows only when the sunscreen is opening or closing, after it opened or closed, there is no error message.

the rest of the script seems fine


EDIT: When i change f:write to io.write, then i'm getting no errors...

Re: Sunscreen script v0.02

Posted: Saturday 25 June 2016 12:55
by BartdaMan
Installed the script, but Domoticz log shows:
2016-06-25 12:50:00.018 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_zonwering.lua: /home/pi/domoticz/scripts/lua/script_time_zonwering.lua:54: attempt to concatenate global 'Wu_Dev_Temp' (a nil value)

How do I change the WU devices so that Wu_Dev_Temp is no longer a nil value?

Thanks in advance for any tips.

Re: Sunscreen script v0.02

Posted: Monday 27 June 2016 22:10
by Plaagje
BartdaMan wrote:Installed the script, but Domoticz log shows:
2016-06-25 12:50:00.018 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_zonwering.lua: /home/pi/domoticz/scripts/lua/script_time_zonwering.lua:54: attempt to concatenate global 'Wu_Dev_Temp' (a nil value)

How do I change the WU devices so that Wu_Dev_Temp is no longer a nil value?

Thanks in advance for any tips.

The error tell you that it cant retrieve any values from the sensors listed in the script

You have to rename all the devices in the script according to your names

Code: Select all

Wu_Dev_Temp = otherdevices_svalues['WU Temperature']
WU Temperature should be the name for your temperature sensor

the script listed in this post has 3 devices listed under:

Code: Select all

--
-- Retrieve values from devices
--

Re: Sunscreen script v0.02

Posted: Monday 27 June 2016 22:28
by remb0
that's correct! but there is a newer version of this script that you can found in this forum.