switch off and repeat  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

switch off and repeat

Post by pvklink »

Hi,

During my holidat my irrigiation switches (some zones) (sometimes) did not went OFF because of a signal that did not reach the switch.
So i want to add some security that the zones (4 pieces) go off. The scripts does work ok.

explenation, I have to scripts:
1. script M that manages a zone manually.
2. timer script T that manages all zones and calls the other script M to put it on and off.

a. When a zone reaches its event via script T it calls script M and script M puts the zone on and off after the zone timer...
b when manually activate the zone to ON it does the same
c. when putting the zone manually to OFF (abort) it kills the schedule and stops the zone

Question:
So i think i have to add in script M (when putting a zone to ON)
item.switchOff().afterMin(devtimer).silent().repeatAfterSec(5, 2) instead off item.switchOff().afterMin(devtimer).silent()

Possible problem(did not test this) is that when the zone goes to off after x-time it kills the repeat schedule (it executes item.cancelQueuedCommands) (dont know this for sure)
I cant remove the item.cancelQueuedCommands(), i need this when putting the zones manually to on/off, in this case the schedule must be killed when pressing to OFF...

script Manually manage a device

Code: Select all

return {
    on = {devices = {'Tuinirrigatie_gazon','Tuinirrigatie_border_links','Tuinirrigatie_planten','Tuinirrigatie_border_rechts'}},

    logging = { 
                    level   = domoticz.LOG_ERROR,
                    marker  = "Tuinirrigatie",
              },

    execute = function(dz, item, info)
    local devtimer  = tonumber(dz.devices(item.name .. '_waarde').levelName)  -- timer ophalen. timer heeft altijd de naam van het device met _waarde erachter

    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
   
    if item.state == 'Off' then
        item.cancelQueuedCommands()
        dz.devices('usb lamp').switchSelector('Off') -- er op 29-6-19 bijgeplaatst
        globalMessage(add, ' IRRIGATIE UIT: ZONE ' .. item.name .. ' uitgezet...',logcode) -- 'om: ' .. dz.time.rawTime

    else 
        item.switchOff().afterMin(devtimer).silent()

        dz.helpers.alertLidl(dz, "blauw", devtimer*60)   
        globalMessage(add, ' IRRIGATIE AAN: ZONE ' .. item.name .. ' aangezet voor: ' .. devtimer .. ' minuten...',logcode) 

    end
    globalMessage(chg) -- dump

end
}
script T (timers)

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 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 devpauze      = 1
    local aftertimer1   = 0     -- toegevoegd als t niet werkt verwijderen
    local aftertimer2   = 0
    local aftertimer3   = 0
    local aftertimer4   = 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    -- ALLES nogmaals uit en schakelaar op UIT
    

    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
            
        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.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(aftertimer4)     --    dz.devices('Tuinirrigatie').switchSelector(dz.devices('Tuinirrigatie').lastLevel).afterMin(aftertimer4) GEEFT problemen soms autom weer op AAN
            globalMessage(add, ' IRRIGATIE UIT: Tuinirrigatie wordt uitgezet om: ' .. os.date('%Y-%m-%d %H:%M:%S', now + aftertimer4 * 60) .. ' ...',logcode)

        end    

    elseif (item.isTimer and iswitch1.level == 0 and iswitch2.level == 0 and iswitch3.level == 0 and iswitch4.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() 

        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
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: switch off and repeat  [Solved]

Post by waaren »

pvklink wrote: Friday 16 August 2019 10:19 During my holiday my irrigiation switches (some zones) (sometimes) did not went OFF because of a signal that did not reach the switch.
So i want to add some security that the zones (4 pieces) go off. The scripts does work ok.

Possible problem(did not test this) is that when the zone goes to off after x-time it kills the repeat schedule (it executes item.cancelQueuedCommands) (dont know this for sure)
the script
Manually manage a device
will not be triggered by the

Code: Select all

item.switchOff().afterMin(devtimer).silent()[b].repeatAfterSec(5, 2) 
command because of the silent()
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: switch off and repeat

Post by pvklink »

okay,
1) so when using ".silent" in an action, the action itself is executed but not the corresponding script(s) of the corresponding device ?

2) adding ".repeatAfterSec(5, 2)" in: item.switchOff().afterMin(devtimer).silent().repeatAfterSec(5, 2) will execute 2 extra OFF commands ?

3) so this is the correct way to execute the OFF command three times?
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: switch off and repeat

Post by waaren »


pvklink wrote:okay,
1)2)3)
Yes



Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest