And now again it doesn't work well.
The script is executing, and tells met that the screen is down, but in fact it isn't. This time I checked if the dryrun is turned off,
local dryRun = 'N' -- Enable dry run mode to test the sunscreen script without actually activating the sunscreen
and also the switch in local manualOverrideSwitch = 'Sunscreen - Manual' is set to automatic.
the screen itself will be lowered when I click the button on the tablet. So,, also the KaKu device is working.
The script:
Code: Select all
-- Define all the sensors which needs to be considered for the sunscreen to raise
local sensors = {
temperature = {
active = true,
device = 'TempHumBaro',
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 = 'BR Regen Percentage', -- This needs to be a virtual sensor of type 'percentage'
closeRule = function(device)
return device.percentage > 15
end
},
uv = {
active = true,
device = 'UV Index',
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('Zonnescherm is al uit' , 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('Script is handmatig uitgezet', domoticz.LOG_DEBUG)
return
end
local sunscreen = domoticz.devices(sunscreenDevice)
-- Sunscreen must always be up during nighttime
if (domoticz.time.isNightTime) then
message = 'Zonneschermm omhoog, het is nacht'
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 .. ' drempel overschreden , scherm omhoog')
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 niet actief: ' .. sensorType, domoticz.LOG_DEBUG)
end
end
-- All tresholds OK, sunscreen may be lowered
if (sunscreen.lastUpdate.minutesAgo > timeBetweenOpens) then
message = 'Zon schijnt, drempels ok, scherm omlaag'
switchOn(sunscreen, message)
end
end
}