error in combining timer and device action gives error

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:

error in combining timer and device action gives error

Post 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
}
Last edited by pvklink on Friday 14 June 2019 22:01, edited 1 time in total.
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: error in combining timer and device action gives error

Post 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.
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: error in combining timer and device action gives error

Post by pvklink »

so i delete device and use item?
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: error in combining timer and device action gives error

Post 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.
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: error in combining timer and device action gives error

Post 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
}
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: error in combining timer and device action gives error

Post 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
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