Sunscreen

Moderator: leecollings

Post Reply
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Sunscreen

Post by remko2000 »

The summer is near :-) so I want a nice script to automate my sunscreen.
I see on this forum a couple of nice scripts but most of them looks outdated because the use Weather Underground (WU) sensors whose API has stopped.
I have add some nice weathersensors/switches form buienradar which is now officially supported in version 2020.1 (very happy with it).

Now I'm looking for a (LUA?) script which I can edit to my situation. Does anyone have a lead to a script wich use buienradarsensor and take for exmaple UV, wind en temp. in account?

Unfortunately I have too little knowledge of lua to adapt these beautiful WU scripts myself to a script that uses ' buienradar' as a basis for threshold values. For example, the wind sensor has different values (wind force, gust, etc.) that must be divided in order to control with the correct value
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: Sunscreen

Post by remko2000 »

I'm started to change the script in the wiki (https://www.domoticz.com/wiki/Automate_ ... nds_shades).
Startingpoint are my weathersensor from buienradar. I use the sensors wind, uv and regen.I see a few different from the sensor of weather underground. Buienradar uses for UV not the ' UV-index' but ' watt/m2' . So I converted the 2,5 and 1,2 UV to 763 and 366 watt/m2 (thressholdvalues) in the script. What I can't do is the wind/gust value. My log says:

Code: Select all

2020-03-29 14:47:11.209 Status: LUA: These conditions are valid when sunscreen is up:
2020-03-29 14:47:11.209 Status: LUA: 1 - Daytime : True
2020-03-29 14:47:11.209 Status: LUA: 0 - Wind : 147 <= Th : 30
2020-03-29 14:47:11.209 Status: LUA: 0 - Gust : 198 <= Th : 50
2020-03-29 14:47:11.209 Status: LUA: 0 - Rain : 0.2 <= Th : 0
2020-03-29 14:47:11.209 Status: LUA: 0 - UV Close : 704.0 >= Th : 763
2020-03-29 14:47:11.209 Status: LUA: 0 - Temp Close : 5.6 >= Th : 20
2020-03-29 14:47:11.209 Status: LUA: These conditions are valid when sunscreen is down:
2020-03-29 14:47:11.209 Status: LUA: 0 - UV Open : 704.0 <= Th : 366
2020-03-29 14:47:11.209 Status: LUA: 1 - Temp Open : 5.6 <= Th : 15
So wind and gust have to be divided by 10 in my script. I just don't have enough knowledge of lua so I don't know where to do it best.

This is my script until now:

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   = 763          -- treshold voor Uv close
TH_Uv_Open    = 366          -- 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['Wind']
Wu_Dev_Rain = otherdevices_svalues['Regen']
Wu_Dev_UvMe = otherdevices_svalues['Zonkracht']
--
--------------------------------------------------------------------------------
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
when I look at my log it should be nice for the rest. Any suggestions where I can add the dividing by 10 for the wind/gustvalues?
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Sunscreen

Post by EdwinK »

I'm using this dzVents script for sunscreen for quite some time. (Just need time to check all the devices again)

Code: Select all

-- Define all the sensors which needs to be considered for the sunscreen to close
local sensors = {
temperature = {
active = true,
device = 'Buiten Temp',
closeRule = function(device)
return device.temperature <= 19
end
},
wind = {
active = true,
device = 'Wind',
closeRule = function(device)
return device.speed >= 50 or device.gust >= 150
end
},
rain = {
active = true,
device = 'Rain',
closeRule = function(device)
return device.rainRate > 0
end
},
rainExpected = {
active = false,
device = 'IsItGonnaRain', -- This needs to be a virtual sensor of type 'percentage'
closeRule = function(device)
return device.percentage > 15
end
},
uv = {
active = true,
device = 'UV',
closeRule = function(device)
return device.uv <= 2
end
},
lux = {
active = true,
device = 'LUX',
closeRule = function(device)
return device.lux <= 500
end
}
}

local sunscreenDevice = 'Sunscreen' -- Define the name of your sunscreen device
local dryRun = 'N' -- Enable dry run mode to test the sunscreen script without actually activating the sunscreen
-- Define the name of a virtual switch which you can use to disable the sunscreen automation script, Set to false to disable this feature
local manualOverrideSwitch = 'Sunscreen - Manual'
local timeBetweenOpens = 10 -- Minutes to wait after a sunscreen close before opening it again.
local LogFile= '/home/pi/domoticz/Logs/Sunscreen.csv ~m.csv'
local LogAction = 'N'
local message = '.'

return { 
    active = true,
        
    on      =   {   timer   =   {'every minute'} },

    logging =   {   level   =   domoticz.LOG_DEBUG,
                    marker  =   'Sunscreen' },

    data    =   {   safedMessage = { initial = "-" } },
            
execute = function(domoticz)

    -- FUNCTIONS
    
    function LogActions()
        -- LogFile=string.gsub(LogFile,"~d",os.date("%Y-%m-%d"))
        LogFile=string.gsub(LogFile,"~m",os.date("%Y-%m"))
    
        if not My.File_exists(LogFile) then
            f=io.open(LogFile,"a")
            f:write("Datum ; Tijd ; dryRun ; Manual_or_Auto ; Message")
            f:write("\r\n")
        else
            f=io.open(LogFile,"r")
            c=f:read("*line")
            f:close()
            f=io.open(LogFile,"a")
        end
        if domoticz.data.safedMessage == message then
            domoticz.log("Same message skip write action",domoticz.LOG_DEBUG)
        else    
            domoticz.data.safedMessage = message
            f:write(os.date("%Y-%m-%d;%H:%M:%S") .. ";" .. dryRun .. ";" .. domoticz.devices(manualOverrideSwitch).state .. ";" .. message )
            f:write("\r\n")
        end
        f:close()
    end
    
    local function switchOn(sunscreen, message)
        if LogAction == 'Y' or LogAction == 'A' then LogActions() end
        if (sunscreen.state == 'Closed') then
            if dryRun == 'N' then
                sunscreen.switchOn()
                --domoticz.notify('Sunscreen', message)
            end
            domoticz.log(message, domoticz.LOG_INFO)
            --domoticz.notify('Sunscreen', message)
        else
            domoticz.log('Sunscreen is already down' , domoticz.LOG_INFO)
        end
        if LogAction == 'Y' or LogAction == 'A' then LogActions() end
    end
    
    local function switchOff(sunscreen, message)
        if (sunscreen.state == 'Open') then
            if dryRun == 'N' then
                sunscreen.switchOff()
            end
            domoticz.log(message, domoticz.LOG_INFO)
            --domoticz.notify('Sunscreen', message)
        end
        if LogAction == 'Y' or LogAction == 'A' then LogActions() end
    end
    
    -- PROGRAM STARTS
    
    
    if (manualOverrideSwitch and domoticz.devices(manualOverrideSwitch).state == 'On') then
        domoticz.log('Automatic sunscreen script is manually disabled', domoticz.LOG_DEBUG)
        return
    end
    
    local sunscreen = domoticz.devices(sunscreenDevice)
    
    -- Sunscreen must always be up during nighttime
    if (domoticz.time.isNightTime) then
        message = 'Raising sunscreen, It is night'
        switchOff(sunscreen, message)
        if LogAction == 'Y' or LogAction == 'A' then LogActions() end   
        return
    end
    
    -- Check all sensor tresholds and if any exeeded close sunscreen
    for sensorType, sensor in pairs(sensors) do
        if (sensor['active'] == true) then
            local device = domoticz.devices(sensor['device'])
            local closeRule = sensor['closeRule']
            domoticz.log('Checking sensor: ' .. sensorType, domoticz.LOG_DEBUG)
            if (closeRule(device)) then
                message = (sensorType .. ' treshold exceeded , Sunscreen up')
                switchOff(sunscreen, message )
                domoticz.log(message, domoticz.LOG_DEBUG)
                if LogAction == 'Y' or LogAction == 'A' then LogActions() end
                -- Return early when we exeed any tresholds
                return
            end
        else
            domoticz.log('Sensor not active skipping: ' .. sensorType, domoticz.LOG_DEBUG)
        end
    end
    
    -- All tresholds OK, sunscreen may be lowered
    if (sunscreen.lastUpdate.minutesAgo > timeBetweenOpens) then
        message = 'Sun is shining, all thresholds OK, lowering sunscreen'
        switchOn(sunscreen, message)
    end
end
}
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: Sunscreen

Post by remko2000 »

Maybe I'm crazy but my log says:

Code: Select all

20-03-30 16:39:08.094 Status: LUA: >> Sunscreen LUA Contitions.....v0.021.....Beta
2020-03-30 16:39:08.094 Status: LUA: These conditions are valid when sunscreen is up:
2020-03-30 16:39:08.094 Status: LUA: 1 - Daytime : True
2020-03-30 16:39:08.094 Status: LUA: 0 - Wind : 73 <= Th : 10
2020-03-30 16:39:08.094 Status: LUA: 0 - Gust : 101 <= Th : 20
2020-03-30 16:39:08.094 Status: LUA: 0 - Rain : 0.1 <= Th : 0
2020-03-30 16:39:08.094 Status: LUA: 0 - UV Close : 228.0 >= Th : 763
2020-03-30 16:39:08.094 Status: LUA: 0 - Temp Close : 8.4 >= Th : 10
2020-03-30 16:39:08.094 Status: LUA: These conditions are valid when sunscreen is down:
2020-03-30 16:39:08.094 Status: LUA: 0 - UV Open : 228.0 <= Th : 100
2020-03-30 16:39:08.094 Status: LUA: 0 - Temp Open : 8.4 <= Th : 1
2020-03-30 16:39:08.094 Status: LUA: ______________________________
If I'm looking to the conditions for 'sunscreen down' the current values (UV and Temp) are higher than the (test)thresholdvalues. Why doens't switch my virtual sunscreenswitch on?

This is my luascript:

Code: Select all

-- Time_Sunscreen
--
-- Check the weather conditions with buienradar 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
-- 30-03-2020  Convert weatherunderground to buienradar
--------------------------------------------------------------------------------
--
--      Variable
--
Version       = 0.021
Version_Type  = 'Beta'
TH_Wind       = 10      -- Treshold wind
TH_Gust       = 20      -- Treshold windstoten
TH_Rain       = 0           -- Treshold regen/neerslag
TH_Uv_Close   = 763          -- treshold voor Uv close
TH_Uv_Open    = 100          -- treshold voor Uv open   
TH_Tp_Close   = 10          -- treshold voor Temperature close
TH_Tp_Open    = 1          -- 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    = 'Zonnescherm'
DeviceManual  = 'Zonnescherm - handmatig'
TelegramName  = 'xxx'
--
-- Retrieve values from devices
--
Wu_Dev_Temp = otherdevices_svalues['Wind']
Wu_Dev_Rain = otherdevices_svalues['Regen']
Wu_Dev_UvMe = otherdevices_svalues['Zonkracht']
--
--------------------------------------------------------------------------------
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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest