Page 1 of 1

error in combining timer and device action gives error

Posted: Friday 14 June 2019 21:43
by pvklink
Hi,

i try to combine a switch that executes
on script:///home/pi/domoticz/scripts/start_motion.sh
off script:///home/pi/domoticz/scripts/stop_motion.sh

with a script that executes the above motion switch on timed events ( groupOnTimer1 etc.)
so i can get rid off the two *.sh files and have the same logging (timed and manual)
So i first made a difference in the script between device triggering and timer triggering

Script is finished, but
1) I got an error "on info" that i don't understand and
2) when i turn the switch 'Bewegingsservice' On or off it always executes the ELSE part

I search for houws but i dont see it....

2019-06-14 22:00:31.531 Status: User: Admin initiated a switch command (153/Bewegingsservice/Off)
2019-06-14 22:00:31.770 Status: Executing script: /home/pi/domoticz/scripts/stop_motion.sh
2019-06-14 22:00:31.784 Status: dzVents: Error (2.4.23): MotionTimers: An error occurred when calling event handler DZ_Bewegingsservice
2019-06-14 22:00:31.784 Status: dzVents: Error (2.4.23): MotionTimers: ...cripts/dzVents/generated_scripts/DZ_Bewegingsservice.lua:42: attempt to index local 'info' (a nil value)

Code: Select all

local groupOnTimer1    = 'at 00:05 on mon,tue,wed,thu,fri,sat,sun'
local groupOffTimer1    = 'at 06:00 on mon,tue,wed,thu,fri,sat,sun'

return {
	on =    {
                timer = {groupOnTimer1,groupOffTimer1},
                devices = {'Bewegingsservice'},
            },
        
    logging =   { level   = domoticz.LOG_ERROR ,                                                
                  marker  = "MotionTimers"},
              
    execute = function(dz,device,item,info)
    local switch0 = dz.devices('timerlog')
    local switch2 = dz.devices('Jablotronenabled')
    local switch3 = dz.devices('Bewegingsservice')

    if ((item.isTimer and item.trigger == groupOnTimer1 and switch2.state == 'Unlocked') or (item.isDevice and device.state == 'On')) then                                                      
    
        if item.isTimer then 
            switch3.switchOn().checkFirst()  
        end
        osExecute('sudo mount -a')
	osExecute('sudo /etc/init.d/motion start')

        dz.log("Script: " .. info.scriptName .. " Bewegingsservice is geactiveerd....", dz.LOG_INFO)
        timertekst = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : Bewegingsservice is geactiveerd....' .. '<br>' ..  dz.devices('timerlog').text 
        switch0.updateText(timertekst)

    elseif ((item.isTimer and item.trigger == groupOffTimer1 and switch2.state == 'Unlocked') or (item.isDevice and device.state == 'Off')) then                                                      

        if item.isTimer then 
            switch3.switchOff().checkFirst()  
        end
        osExecute('sudo /etc/init.d/motion stop')

        dz.log("Script: " .. info.scriptName .. " Bewegingsservice is gedeactiveerd....", dz.LOG_INFO)
        timertekst = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : Bewegingsservice is gedeactiveerd....' .. '<br>' ..  dz.devices('timerlog').text 
        switch0.updateText(timertekst)
    else
    
        dz.log("Script: " .. info.scriptName .. " Bewegingsservice blijft aan, waarschijnlijk met vakantie....", dz.LOG_INFO)
        timertekst = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : Bewegingsservice blijft aan, waarschijnlijk vakantie....' .. '<br>' ..  dz.devices('timerlog').text 
        switch0.updateText(timertekst)
    end

end
}

Re: error in combining timer and device action gives error

Posted: Friday 14 June 2019 21:59
by waaren
pvklink wrote: Friday 14 June 2019 21:43 2019-06-14 21:34:05.925 Status: dzVents: Error (2.4.23): MotionTimers: An error occurred when calling event handler DZ_Bewegingsservice
2019-06-14 21:34:05.925 Status: dzVents: Error (2.4.23): MotionTimers: ...cripts/dzVents/generated_scripts/DZ_Bewegingsservice.lua:39: attempt to index local 'info' (a nil value)

Code: Select all

    execute = function(dz,device,item,info)
}
You have 4 parameters in the function header but dzVents is only sending 3. So dz is correct, device = the item that triggered the script, and item = triggerinfo but info = nil

1st parm: The domoticz object.
This object gives you access to almost everything in your Domoticz system, including all methods to manipulate them—like modifying switches or sending notifications and some stuff added by dzVents C++ code.

2nd. parm: item
Depending on what actually triggered the script, item is either a:
  • device;
  • variable;
  • scene;
  • group;
  • timer;
  • security;
  • httpResponse.
3rd parm: triggerInfo
holds information about what triggered the script. The object has 3 attributes:
  1. type: like Timer, Device, etc
  2. trigger: the timer rule that triggered the script if the script was called due to a timer event, or the security state that triggered the security trigger rule.
  3. scriptName: the name of the current script.

Re: error in combining timer and device action gives error

Posted: Friday 14 June 2019 22:04
by pvklink
so i delete device and use item?

Re: error in combining timer and device action gives error

Posted: Friday 14 June 2019 22:06
by waaren
pvklink wrote: Friday 14 June 2019 22:04 so i delete device and use item?
Yes and change device.state to item.state on 2 places in your script.

Re: error in combining timer and device action gives error

Posted: Friday 14 June 2019 22:12
by pvklink
1) OK, info error is gone !
2) the right If's are executed
3) Only errors on the OS commands....

2019-06-14 22:09:55.199 Status: dzVents: Error (2.4.23): MotionTimers: An error occurred when calling event handler DZ_Bewegingsservice
2019-06-14 22:09:55.199 Status: dzVents: Error (2.4.23): MotionTimers: ...cripts/dzVents/generated_scripts/DZ_Bewegingsservice.lua:23: attempt to call global 'osExecute' (a nil value)

Code: Select all

local groupOnTimer1    = 'at 00:05 on mon,tue,wed,thu,fri,sat,sun'
local groupOffTimer1    = 'at 06:00 on mon,tue,wed,thu,fri,sat,sun'

return {
	on =    {
                timer = {groupOnTimer1,groupOffTimer1},
                devices = {'Bewegingsservice'},
            },
        
    logging =   { level   = domoticz.LOG_ERROR ,                                                
                  marker  = "MotionTimers"},
              
    execute = function(dz,item,info)
    local switch0 = dz.devices('timerlog')
    local switch2 = dz.devices('Jablotronenabled')
    local switch3 = dz.devices('Bewegingsservice')

    if ((item.isTimer and item.trigger == groupOnTimer1 and switch2.state == 'Unlocked') or (item.isDevice and item.state == 'On')) then                                                      
    
        if item.isTimer then 
            switch3.switchOn().checkFirst()  
        end
        osExecute('sudo mount -a')
	    osExecute('sudo /etc/init.d/motion start')

        dz.log("Script: " .. info.scriptName .. " Bewegingsservice is geactiveerd....", dz.LOG_INFO)
        timertekst = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : Bewegingsservice is geactiveerd....' .. '<br>' ..  dz.devices('timerlog').text 
        switch0.updateText(timertekst)

    elseif ((item.isTimer and item.trigger == groupOffTimer1 and switch2.state == 'Unlocked') or (item.isDevice and item.state == 'Off')) then                                                      

        if item.isTimer then 
            switch3.switchOff().checkFirst()  
        end
        osExecute('sudo /etc/init.d/motion stop')

        dz.log("Script: " .. info.scriptName .. " Bewegingsservice is gedeactiveerd....", dz.LOG_INFO)
        timertekst = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : Bewegingsservice is gedeactiveerd....' .. '<br>' ..  dz.devices('timerlog').text 
        switch0.updateText(timertekst)
    else
    
        dz.log("Script: " .. info.scriptName .. " Bewegingsservice blijft aan, waarschijnlijk met vakantie....", dz.LOG_INFO)
        timertekst = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : Bewegingsservice blijft aan, waarschijnlijk vakantie....' .. '<br>' ..  dz.devices('timerlog').text 
        switch0.updateText(timertekst)
    end

end
}

Re: error in combining timer and device action gives error

Posted: Friday 14 June 2019 22:32
by waaren
pvklink wrote: Friday 14 June 2019 22:12 Only errors on the OS commands....

2019-06-14 22:09:55.199 Status: dzVents: Error (2.4.23): MotionTimers: An error occurred when calling event handler DZ_Bewegingsservice
2019-06-14 22:09:55.199 Status: dzVents: Error (2.4.23): MotionTimers: ...cripts/dzVents/generated_scripts/DZ_Bewegingsservice.lua:23: attempt to call global 'osExecute' (a nil value)
I don't see an osExecute function in your script. Could it be that you forgot to copy this function from one of the examples on this forum because you confused the calls to this user defined function with calls to the built in os.execute ?

example of user defined osExecute function. (Works in std Lua and in dzVents without modification)

Code: Select all

local function osExecute(cmd)
    local fileHandle     = assert(io.popen(cmd, 'r'))
    local commandOutput  = assert(fileHandle:read('*a'))
    local returnTable    = {fileHandle:close()}

    return commandOutput,returnTable[3]            -- rc[3] contains returnCode
end