optimize script
Posted: Thursday 29 August 2019 22:09
Hi,
i have i script that manages 5 waterareas.
1. First It calculates when the 5 zone has to start and then
2. activates these areas one by one so the waterpressure is enough.
3. After that it does some reset stuff
4. the script works with timers and also with on and off (on activate alle zones one time) off stops everything, auto uses timed events)
another scripts puts the switches off after x time, this script is very small and already optimized because it is triggered for each individual switch.
The script works great, no problems at all!
I added zone 5 recently and saw that the 5 area's all have for 95% the same code (see section between ---xxx and ---xxx)
i find it scary to change this working script but i know that half of the code is unnecessary...
It is possible to change this with something like:
local function irrigation_zones(actions)
for action in pairs(actions) do
--- the adjusted code
end
end
actions = {Tuinirrigatie_border_links,Tuinirrigatie_border_rechts,Tuinirrigatie_gazon,Tuinirrigatie_planten,Tuinirrigatie_voortuin}
irrigation_zones(actions)
original code
i also made some pseudo code for the 5 zones
attention the first switch has no .aftermin and and another message...
no idea how i pass code 1-5 for nr
i have i script that manages 5 waterareas.
1. First It calculates when the 5 zone has to start and then
2. activates these areas one by one so the waterpressure is enough.
3. After that it does some reset stuff
4. the script works with timers and also with on and off (on activate alle zones one time) off stops everything, auto uses timed events)
another scripts puts the switches off after x time, this script is very small and already optimized because it is triggered for each individual switch.
The script works great, no problems at all!
I added zone 5 recently and saw that the 5 area's all have for 95% the same code (see section between ---xxx and ---xxx)
i find it scary to change this working script but i know that half of the code is unnecessary...
It is possible to change this with something like:
local function irrigation_zones(actions)
for action in pairs(actions) do
--- the adjusted code
end
end
actions = {Tuinirrigatie_border_links,Tuinirrigatie_border_rechts,Tuinirrigatie_gazon,Tuinirrigatie_planten,Tuinirrigatie_voortuin}
irrigation_zones(actions)
original code
Code: Select all
-- timers mogen niet op t exacte moment draaien, dus 5mn verschil
-- local Tuinirrigatietimer10 = 'at 18:10 on mon,tue,wed,thu,fri,sat,sun'
--
local Tuinirrigatietimer10 = 'every day at sunset'
local Tuinirrigatietimer20 = 'every other day 5 minutes after sunset'
local Tuinirrigatietimer30 = '10 minutes after sunset on mon,thu,sun'
local Tuinirrigatietimer40 = '10 minutes before sunset on sat'
local Tuinirrigatietimer50 = '5 minutes before sunset on 1/*'
return {
on = { timer = {Tuinirrigatietimer10,Tuinirrigatietimer20,Tuinirrigatietimer30,Tuinirrigatietimer40,Tuinirrigatietimer50},
devices = {'Tuinirrigatie'}},
logging = { level = domoticz.LOG_ERROR ,
marker = "Timers irrigatie"},
execute = function(dz,item,info)
local logcode = 3
local messageTable = {}
local add = 'add'
local del = 'del'
local chg = 'chg'
local function globalMessage(action, message, logcode)
if logcode == nil then logcode = 3 end
if action == add and logcode > 0 then
messageTable = dz.helpers.addMessage(dz, item, info, message, messageTable, logcode)
elseif action == del then
dz.globalData.mylogging = message
dz.devices('timerlog').updateText(message)
elseif action == chg then
dz.helpers.dumpMessages(dz, messageTable)
end
end
local iswitch1 = dz.devices('Tuinirrigatie_border_links_timer')
local iswitch2 = dz.devices('Tuinirrigatie_border_rechts_timer')
local iswitch3 = dz.devices('Tuinirrigatie_gazon_timer')
local iswitch4 = dz.devices('Tuinirrigatie_planten_timer')
local iswitch5 = dz.devices('Tuinirrigatie_voortuin_timer')
local now = os.time(os.date('*t'))
local devduur1 = tonumber(dz.devices('Tuinirrigatie_border_links_waarde').levelName)
local devduur2 = tonumber(dz.devices('Tuinirrigatie_border_rechts_waarde').levelName)
local devduur3 = tonumber(dz.devices('Tuinirrigatie_gazon_waarde').levelName)
local devduur4 = tonumber(dz.devices('Tuinirrigatie_planten_waarde').levelName)
local devduur5 = tonumber(dz.devices('Tuinirrigatie_voortuin_waarde').levelName)
local devpauze = 1
local aftertimer1 = 0 -- toegevoegd als t niet werkt verwijderen
local aftertimer2 = 0
local aftertimer3 = 0
local aftertimer4 = 0
local aftertimer5 = 0
-- hier de tijden bepalen (wanneer de 4 groepen mogen worden gestart...
-- kijkt wel of de duur is gevuld en neemt de tijd anders niet mee
-- kijkt bij timers of de timer aan staat
-- bij manueel of het AAN betreft bij AUTO en OFF spelen deze geen rol, wordt berekend bij timed event of bij aan
aftertimer1 = 0
if (devduur1 >0 and ((item.isTimer and iswitch1.level > 0) or (item.isDevice and dz.devices('Tuinirrigatie').state == 'aan'))) then aftertimer1 = devduur1 + devpauze end
aftertimer2 = aftertimer1
if (devduur2 >0 and ((item.isTimer and iswitch2.level > 0) or (item.isDevice and dz.devices('Tuinirrigatie').state == 'aan'))) then aftertimer2 = aftertimer1 + devduur2 + devpauze end
aftertimer3 = aftertimer2
if (devduur3 >0 and ((item.isTimer and iswitch3.level > 0) or (item.isDevice and dz.devices('Tuinirrigatie').state == 'aan'))) then aftertimer3 = aftertimer2 + devduur3 + devpauze end
aftertimer4 = aftertimer3
if (devduur4 >0 and ((item.isTimer and iswitch4.level > 0) or (item.isDevice and dz.devices('Tuinirrigatie').state == 'aan'))) then aftertimer4 = aftertimer3 + devduur4 + devpauze end
aftertimer5 = aftertimer4
if (devduur5 >0 and ((item.isTimer and iswitch5.level > 0) or (item.isDevice and dz.devices('Tuinirrigatie').state == 'aan'))) then aftertimer5 = aftertimer4 + devduur5 + devpauze end
if (
(item.isTimer and dz.devices('hetregent').state == 'Off' and dz.devices('Tuinirrigatie').state == 'auto')
or (item.isDevice and dz.devices('Tuinirrigatie').state == 'aan')
) then
-- timed event, het regent niet en irrigatie handeling is auto or
-- device handeling en irrigatie handeling is AAN
--- xxx
if (item.isTimer and iswitch1.level == 10 and item.trigger == Tuinirrigatietimer10) or -- controleer of de irrigatiezone of er aan een van de 4 programma's is voldaan of AAN
(item.isTimer and iswitch1.level == 20 and item.trigger == Tuinirrigatietimer20) or
(item.isTimer and iswitch1.level == 30 and item.trigger == Tuinirrigatietimer30) or
(item.isTimer and iswitch1.level == 40 and item.trigger == Tuinirrigatietimer40) or
(item.isTimer and iswitch1.level == 50 and item.trigger == Tuinirrigatietimer50) or
(item.isDevice and dz.devices('Tuinirrigatie').state == 'aan') then
if devduur1 >0 then
dz.devices('Tuinirrigatie_border_links').switchOn().checkFirst() --.forMin(devtimer1) wordt in DZ_irrigatie_source bepaalt
globalMessage(add, ' IRRIGATIE AAN: ZONE: Tuinirrigatie_border_links wordt aangezet om: ' .. os.date('%Y-%m-%d %H:%M:%S', now) .. ' voor ' .. devduur1 .. ' minuten...',logcode)
else
globalMessage(add, ' IRRIGATIE UIT: ZONE: Tuinirrigatie_border_links is niet aangezet, de duur stond uit',logcode)
end
end
if (item.isTimer and iswitch2.level == 10 and item.trigger == Tuinirrigatietimer10) or
(item.isTimer and iswitch2.level == 20 and item.trigger == Tuinirrigatietimer20) or
(item.isTimer and iswitch2.level == 30 and item.trigger == Tuinirrigatietimer30) or
(item.isTimer and iswitch2.level == 40 and item.trigger == Tuinirrigatietimer40) or
(item.isTimer and iswitch2.level == 50 and item.trigger == Tuinirrigatietimer50) or
(item.isDevice and dz.devices('Tuinirrigatie').state == 'aan') then
if devduur2 > 0 then
dz.devices('Tuinirrigatie_border_rechts').switchOn().checkFirst().afterMin(aftertimer1)
globalMessage(add, ' IRRIGATIE AAN: ZONE: Tuinirrigatie_border_rechts wordt aangezet om: ' .. os.date('%Y-%m-%d %H:%M:%S', now + aftertimer1 * 60) .. ' voor ' .. devduur2 .. ' minuten...',logcode)
else
globalMessage(add, ' IRRIGATIE UIT: ZONE: Tuinirrigatie_border_rechts is niet aangezet, de duur stond uit',logcode)
end
end
if (item.isTimer and iswitch3.level == 10 and item.trigger == Tuinirrigatietimer10) or
(item.isTimer and iswitch3.level == 20 and item.trigger == Tuinirrigatietimer20) or
(item.isTimer and iswitch3.level == 30 and item.trigger == Tuinirrigatietimer30) or
(item.isTimer and iswitch3.level == 40 and item.trigger == Tuinirrigatietimer40) or
(item.isTimer and iswitch3.level == 50 and item.trigger == Tuinirrigatietimer50) or
(item.isDevice and dz.devices('Tuinirrigatie').state == 'aan') then
if devduur3 > 0 then
dz.devices('Tuinirrigatie_gazon').switchOn().checkFirst().afterMin(aftertimer2)
globalMessage(add, ' IRRIGATIE AAN: ZONE: Tuinirrigatie_gazon wordt aangezet om: ' .. os.date('%Y-%m-%d %H:%M:%S', now + aftertimer2 * 60) .. ' voor ' .. devduur3 .. ' minuten...',logcode)
else
globalMessage(add, ' IRRIGATIE UIT: ZONE: Tuinirrigatie_gazon is niet aangezet, de duur stond uit',logcode)
end
end
if (item.isTimer and iswitch4.level == 10 and item.trigger == Tuinirrigatietimer10) or
(item.isTimer and iswitch4.level == 20 and item.trigger == Tuinirrigatietimer20) or
(item.isTimer and iswitch4.level == 30 and item.trigger == Tuinirrigatietimer30) or
(item.isTimer and iswitch4.level == 40 and item.trigger == Tuinirrigatietimer40) or
(item.isTimer and iswitch4.level == 50 and item.trigger == Tuinirrigatietimer50) or
(item.isDevice and dz.devices('Tuinirrigatie').state == 'aan') then
if devduur4 > 0 then
dz.devices('Tuinirrigatie_planten').switchOn().checkFirst().afterMin(aftertimer3)
globalMessage(add, ' IRRIGATIE AAN: ZONE: Tuinirrigatie_planten wordt aangezet om: ' .. os.date('%Y-%m-%d %H:%M:%S', now + aftertimer3 * 60) .. ' voor ' .. devduur4 .. ' minuten...',logcode)
else
globalMessage(add, ' IRRIGATIE UIT: ZONE: Tuinirrigatie_planten is niet aangezet, de duur stond uit',logcode)
end
end
if (item.isTimer and iswitch5.level == 10 and item.trigger == Tuinirrigatietimer10) or
(item.isTimer and iswitch5.level == 20 and item.trigger == Tuinirrigatietimer20) or
(item.isTimer and iswitch5.level == 30 and item.trigger == Tuinirrigatietimer30) or
(item.isTimer and iswitch5.level == 40 and item.trigger == Tuinirrigatietimer40) or
(item.isTimer and iswitch5.level == 50 and item.trigger == Tuinirrigatietimer50) or
(item.isDevice and dz.devices('Tuinirrigatie').state == 'aan') then
if devduur5 > 0 then
dz.devices('Tuinirrigatie_voortuin').switchOn().checkFirst().afterMin(aftertimer3)
globalMessage(add, ' IRRIGATIE AAN: ZONE: Tuinirrigatie_voortuin wordt aangezet om: ' .. os.date('%Y-%m-%d %H:%M:%S', now + aftertimer4 * 60) .. ' voor ' .. devduur5 .. ' minuten...',logcode)
else
globalMessage(add, ' IRRIGATIE UIT: ZONE: Tuinirrigatie_voortuin is niet aangezet, de duur stond uit',logcode)
end
end
--- xxx
if (item.isDevice) then -- als alle zones zijn gesproeid en dit is als het manueel is altijd AAN geweest, dan deze UIT zetten
dz.devices('Tuinirrigatie').switchSelector(0).afterMin(aftertimer5) -- dz.devices('Tuinirrigatie').switchSelector(dz.devices('Tuinirrigatie').lastLevel).afterMin(aftertimer5) GEEFT problemen soms autom weer op AAN
globalMessage(add, ' IRRIGATIE UIT: Tuinirrigatie wordt uitgezet om: ' .. os.date('%Y-%m-%d %H:%M:%S', now + aftertimer5 * 60) .. ' ...',logcode)
end
elseif (item.isTimer and iswitch1.level == 0 and iswitch2.level == 0 and iswitch3.level == 0 and iswitch4.level == 0 and iswitch5.level == 0) then -- TIMER
globalMessage(add, ' IRRIGATIE UIT: Timerevent gestart maar alle timerzones staat uit',logcode)
elseif (item.isTimer and dz.devices('Tuinirrigatie').state == 'uit') then
globalMessage(add, ' IRRIGATIE UIT: Timerevent gestart maar het timerssysteem staat uit',logcode)
elseif (item.isTimer and dz.devices('Tuinirrigatie').state == 'auto' and dz.devices('hetregent').state == 'On') then
globalMessage(add, ' IRRIGATIE UIT: Timerevent gestart en is niet aangezet, het is aan t regenen... ',logcode)
elseif (item.isDevice and dz.devices('Tuinirrigatie').state == 'uit') then -- er is gekozen voor OFF op de switch
-- device handeling en irrigatie handeling is uit
dz.devices('Tuinirrigatie_border_links').switchOff() -- gebruik geen checkfirst want bij voortijdig annuleren kan het zijn dat het device nog niet aanstaat ivm after xxx... dan gebeurt er dus niets ook geen cancelqueue
dz.devices('Tuinirrigatie_border_rechts').switchOff()
dz.devices('Tuinirrigatie_gazon').switchOff()
dz.devices('Tuinirrigatie_planten').switchOff()
dz.devices('Tuinirrigatie_voortuin').switchOff()
globalMessage(add, ' IRRIGATIE UIT: irrigatie timers/queue verwijderd... ',logcode)
elseif (item.isDevice and dz.devices('Tuinirrigatie').state == 'auto') then -- er is gekozen voor AUTO op de switch
-- device handeling is AUTO
globalMessage(add, ' IRRIGATIE AUTO: irrigatie is op automatisch gezet, timers worden actief... ',logcode)
else
globalMessage(add, ' IRRIGATIE UIT: is niet aangezet, onbekende reden... ' .. item.name .. '/' .. item.state,logcode)
end
globalMessage(chg) -- dump
end
}
attention the first switch has no .aftermin and and another message...
no idea how i pass code 1-5 for nr
Code: Select all
function(name)
if (item.isTimer and iswitch<nr>.level == 10 and item.trigger == Tuinirrigatietimer10) or -- controleer of de irrigatiezone of er aan een van de 4 programma's is voldaan of AAN
(item.isTimer and iswitch<nr>.level == 20 and item.trigger == Tuinirrigatietimer20) or
(item.isTimer and iswitch<nr>.level == 30 and item.trigger == Tuinirrigatietimer30) or
(item.isTimer and iswitch<nr>.level == 40 and item.trigger == Tuinirrigatietimer40) or
(item.isTimer and iswitch<nr>.level == 50 and item.trigger == Tuinirrigatietimer50) or
(item.isDevice and dz.devices('Tuinirrigatie').state == 'aan') then
if devduur<nr> >0 then
bij <nr> =1
dz.devices('Tuinirrigatie_border_links').switchOn().checkFirst() --.forMin(devtimer1) wordt in DZ_irrigatie_source bepaalt
globalMessage(add, ' IRRIGATIE AAN: ZONE: Tuinirrigatie_<name> wordt aangezet om: ' .. os.date('%Y-%m-%d %H:%M:%S', now) .. ' voor ' .. devduur<nr> .. ' minuten...',logcode)
bij nr <>1
dz.devices('Tuinirrigatie_<name>').switchOn().checkFirst().afterMin(aftertimer<nr-1>)
globalMessage(add, ' IRRIGATIE AAN: ZONE: Tuinirrigatie_<name> wordt aangezet om: ' .. os.date('%Y-%m-%d %H:%M:%S', now + aftertimer<nr-1> * 60) .. ' voor ' .. devduur<nr> .. ' minuten...',logcode)
else
globalMessage(add, ' IRRIGATIE UIT: ZONE: Tuinirrigatie_<name> is niet aangezet, de duur stond uit',logcode)
end
end
end