Sunscreen script v0.02

Moderator: leecollings

Post Reply
MrFrankie
Posts: 10
Joined: Tuesday 16 July 2013 8:45
Target OS: Raspberry Pi / ODroid
Domoticz version: V2.2585
Contact:

Sunscreen script v0.02

Post 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
Last edited by MrFrankie on Tuesday 08 July 2014 21:18, edited 1 time in total.
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Sunscreen script v0.02

Post by remb0 »

I use it already and it's really great! thanks mate. I look forward to comments and possible some feedback.
User avatar
Plaagje
Posts: 42
Joined: Thursday 30 April 2015 10:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Netherlands
Contact:

Re: Sunscreen script v0.02

Post 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...
User avatar
BartdaMan
Posts: 20
Joined: Sunday 22 May 2016 22:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Amersfoort
Contact:

Re: Sunscreen script v0.02

Post 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.
User avatar
Plaagje
Posts: 42
Joined: Thursday 30 April 2015 10:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Netherlands
Contact:

Re: Sunscreen script v0.02

Post 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
--
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Sunscreen script v0.02

Post by remb0 »

that's correct! but there is a newer version of this script that you can found in this forum.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest