Page 1 of 1

another event script for irrigation

Posted: Wednesday 20 January 2021 10:40
by pvklink
Hi,

Last week a created (with a bit of help) a master event/timer script for alle my light switches. It works GREAT!
I like to use the same setup for my irrigation events.
I have 5 irrigation zones with different settings.

For example:
Tuinirrigatie_border_links_timer is a selector switch for zone 1 irrigation with a timer/event as content
There a 4 more zones with 4 selector switches with timer/events

Script
1. I want to read the content of the 5 timerevent devices (see array dashboardTimersdevices) and filling an array (dashboardTimers) with the timers
2. Add manual off events to this array(dashboardTimers) with the content of array (dashboardTimersadd)
3. Let the on statement use the filled array(dashboardTimers) to execute the events
4. print the content of the dashboardTimers so i can check if it works

I think i am almost there but still an error

2021-01-20 10:38:02.066 Error: dzVents: Error: (3.1.1) ...scripts/dzVents/generated_scripts/test_irrigatieklok.lua:24: attempt to call a nil value (field 'devices')
2

Code: Select all

--------------------------------------------------------------------------------------------------------------------------------------------------

local dashboardTimersdevices =
{
    { 'timer_on1','Tuinirrigatie_border_links_timer',true},
    { 'timer_on2','Tuinirrigatie_border_rechts_timer',true},
    { 'timer_on3','Tuinirrigatie_gazon_timer',true},
    { 'timer_on4','Tuinirrigatie_planten_timer',true},
    { 'timer_on5','Tuinirrigatie_voortuin_rechts_timer',true},
}

local dashboardTimersadd =
{
    { 'Allways','every minute',true},                             -- nodig om te testen, kan bij werking uit
    { 'timer_off','every day 90 minutes after sunset',true},      -- deze moet alle timers bij elkaar geteld + pauzes later zijn dan de start van de timer
}

--------------------------------------------------------------------------------------------------------------------------------------------------
local function get_dashboard_timers(dz,item,info,dashboardTimersdevices,dashboardTimersadd)    
    local dashboardTimers = {}
    for _, record in ipairs(dashboardTimersdevices) do
            table.insert(dashboardTimers, dz.devices(record[2]).state)            -- timerwaarden ophalen van de 5 timersdevices 
    end
    for _, record in ipairs(dashboardTimersadd) do
            table.insert(dashboardTimers, record[2])                              -- timers toevoegen om alles voor de zekerheid te stoppen
    end
    return dashboardTimers
end

return 
{
    on ={
        timer = get_dashboard_timers(domoticz,item,info,dashboardTimersdevices,dashboardTimersadd),
        },

    execute = function(dz,item,info)
    _G.logLevel = dz.helpers.get_logtype(dz)    -- dz.log('debug test',dz.LOG_DEBUG) this will only make it to the log when LOG_DEBUG is set  AND dz.log('blabla',dz.LOG_FORCE) -- this will allways make it tp the log
    _G.logMarker = _G.moduleLabel -- marker wordt scriptnaam, _G.logMarker = info.scriptName is idem
    local messageTable = {}       -- log alles, 0 = niets, 1 dom, 2 global, 3 alles... evt per regel ook in te regelen
    -- HIER START DE EXECUTIE
  
    -- hier komt een reeds werkend irrigatiescript
  
    dz.helpers.globalMessage2(dz,item,info,messageTable,'add', ' Timers: ' .. dashboardTimers,10)   -- inhoud van de timers laten zien

    dz.helpers.globalMessage2(dz,item,info,messageTable,'chg') -- dump
end
}

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 10:45
by waaren
pvklink wrote: Wednesday 20 January 2021 10:40 2021-01-20 10:38:02.066 Error: dzVents: Error: (3.1.1) ...scripts/dzVents/generated_scripts/test_irrigatieklok.lua:24: attempt to call a nil value (field 'devices')
It does not help to show an errormessage with a linenumber if you post something else then the script generating the error. I cannot see which line was failing based on the snippet you posted.

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 10:48
by pvklink
mm, strange, i thought i cut and paste it right..
Try again..

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 10:49
by pvklink
error

2021-01-20 10:48:45.471 Error: dzVents: Error: (3.1.1) ...scripts/dzVents/generated_scripts/test_irrigatieklok.lua:24: attempt to call a nil value (field 'devices')
2

Code: Select all

-- timers mogen niet op t exacte moment draaien, dus 5mn verschil
--
--------------------------------------------------------------------------------------------------------------------------------------------------

local dashboardTimersdevices =
{
    { 'timer_on1','Tuinirrigatie_border_links_timer',true},
    { 'timer_on2','Tuinirrigatie_border_rechts_timer',true},
    { 'timer_on3','Tuinirrigatie_gazon_timer',true},
    { 'timer_on4','Tuinirrigatie_planten_timer',true},
    { 'timer_on5','Tuinirrigatie_voortuin_rechts_timer',true},
}

local dashboardTimersadd =
{
    { 'Allways','every minute',true},                             -- nodig om te testen, kan bij werking uit
    { 'timer_off','every day 90 minutes after sunset',true},      -- deze moet alle timers bij elkaar geteld + pauzes later zijn dan de start van de timer
}

--------------------------------------------------------------------------------------------------------------------------------------------------
local function get_dashboard_timers(dz,item,info,dashboardTimersdevices,dashboardTimersadd)    
    local dashboardTimers = {}
    for _, record in ipairs(dashboardTimersdevices) do
            table.insert(dashboardTimers, dz.devices(record[2]).state)            -- timerwaarden ophalen van de 5 timersdevices 
    end
    for _, record in ipairs(dashboardTimersadd) do
            table.insert(dashboardTimers, record[2])                              -- timers toevoegen om alles voor de zekerheid te stoppen
    end
    return dashboardTimers
end

return 
{
    on ={
        timer = get_dashboard_timers(domoticz,item,info,dashboardTimersdevices,dashboardTimersadd),
        },

    execute = function(dz,item,info)
    _G.logLevel = dz.helpers.get_logtype(dz)    -- dz.log('debug test',dz.LOG_DEBUG) this will only make it to the log when LOG_DEBUG is set  AND dz.log('blabla',dz.LOG_FORCE) -- this will allways make it tp the log
    _G.logMarker = _G.moduleLabel -- marker wordt scriptnaam, _G.logMarker = info.scriptName is idem
    local messageTable = {}       -- log alles, 0 = niets, 1 dom, 2 global, 3 alles... evt per regel ook in te regelen
    -- HIER START DE EXECUTIE
  
    -- hier komt een reeds werkend irrigatiescript
  
    dz.helpers.globalMessage2(dz,item,info,messageTable,'add', ' Timers: ' .. dashboardTimers,10)   -- inhoud van de timers laten zien

    dz.helpers.globalMessage2(dz,item,info,messageTable,'chg') -- dump
end
}

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 11:22
by waaren
pvklink wrote: Wednesday 20 January 2021 10:49 error
If you check with below modified script, you will discover that the domoticz object is not populated yet when the script initiating phase is at the on = stage.
There are always possibilities to work around that but it will make the code hard to understand and it will hamper response / performance.
These type of conditions are better manageable and performance wise better coding practice when you move it to the execute = section.

Code: Select all

-- timers mogen niet op t exacte moment draaien, dus 5mn verschil
--
--------------------------------------------------------------------------------------------------------------------------------------------------

local dashboardTimersdevices =
{
    { 'timer_on1','Tuinirrigatie_border_links_timer',true},
    { 'timer_on2','Tuinirrigatie_border_rechts_timer',true},
    { 'timer_on3','Tuinirrigatie_gazon_timer',true},
    { 'timer_on4','Tuinirrigatie_planten_timer',true},
    { 'timer_on5','Tuinirrigatie_voortuin_rechts_timer',true},
}

local dashboardTimersadd =
{
    { 'Allways','every minute',true},                             -- nodig om te testen, kan bij werking uit
    { 'timer_off','every day 90 minutes after sunset',true},      -- deze moet alle timers bij elkaar geteld + pauzes later zijn dan de start van de timer
}

--------------------------------------------------------------------------------------------------------------------------------------------------
local function get_dashboard_timers(dz, item,info,dashboardTimersdevices,dashboardTimersadd)    
    utils = require("Utils")
    utils.log(dz, utils.LOG_FORCE)
    local dashboardTimers = {}
    for _, record in ipairs(dashboardTimersdevices) do
			utils.log(record, utils.LOG_FORCE)
            table.insert(dashboardTimers, dz.devices(record[2]).state)            -- timerwaarden ophalen van de 5 timersdevices 
    end
    for _, record in ipairs(dashboardTimersadd) do
            table.insert(dashboardTimers, record[2])                              -- timers toevoegen om alles voor de zekerheid te stoppen
    end
    return dashboardTimers
end

return 
{
    on ={
        timer = get_dashboard_timers(domoticz,item,info,dashboardTimersdevices,dashboardTimersadd),
        },

    execute = function(dz,item,info)
    _G.logLevel = dz.helpers.get_logtype(dz)    -- dz.log('debug test',dz.LOG_DEBUG) this will only make it to the log when LOG_DEBUG is set  AND dz.log('blabla',dz.LOG_FORCE) -- this will allways make it tp the log
    _G.logMarker = _G.moduleLabel -- marker wordt scriptnaam, _G.logMarker = info.scriptName is idem
    local messageTable = {}       -- log alles, 0 = niets, 1 dom, 2 global, 3 alles... evt per regel ook in te regelen
    -- HIER START DE EXECUTIE
  
    -- hier komt een reeds werkend irrigatiescript
  
    dz.helpers.globalMessage2(dz,item,info,messageTable,'add', ' Timers: ' .. dashboardTimers,10)   -- inhoud van de timers laten zien

    dz.helpers.globalMessage2(dz,item,info,messageTable,'chg') -- dump
end
}

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 11:39
by pvklink
ok, thanks. i found the problem but no idea how to solve it :-)

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 11:45
by pvklink
Is the solution to
1. remove the on statement
2. use some _G.trigger statement

then the domoticz object is there ...

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 12:12
by waaren
pvklink wrote: Wednesday 20 January 2021 11:45 Is the solution to
1. remove the on statement
2. use some _G.trigger statement

then the domoticz object is there ...
My solution would be to move the logic to the execute section. What you try to do is outside the scope of dzVents and more or less puts you back to a classic Lua approach. Probably a great learning experience :D

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 12:29
by pvklink
ok, but the events/timer section is before the execute.
And when solving it in the execute section i have to create the same timers in dzvents and in the 5 selector switches and match them with all the (same) timers in the on statement.
That is how it works now.

I have to manage double timers, and when i select for a moment the same timer for the 5 selector switches (every day at 20.00 for example), the script does a poll also on alle the other events. So my thougt was to check which timers where selected in the 5 devices and use only the selected timers for the on statement. This looks much better for the performance so that my old rpi does not have to poll at times that are not selected

I thought there is simple solution by using something like _G_timers = function or something like that, just like the logging.
But if this means a lot of LUA code, then ok, i stick to the old solution...
Would be ideal if everything before the execution section also could be adressed after the execution section
Perhaps i can solve this easy with node-red

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 13:55
by waaren
pvklink wrote: Wednesday 20 January 2021 12:29 ok, but the events/timer section is before the execute.
And when solving it in the execute section i have to create the same timers in dzvents and in the 5 selector switches and match them with all the (same) timers in the on statement.
When trying to solve something before the execute section that code is executed every minute and on every device change, -every security change -every variable change, -every httpResponse, -every Group change, -every Scene change, -every customEvent and every shellCommandResponse.

Re: another event script for irrigation

Posted: Wednesday 20 January 2021 16:41
by pvklink
ok, clear!
Poor performance solution...