global function

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:

global function

Post by pvklink »

The snapshot function (made by @waaren) works great!
I like to use this function in more scripts.
Is there a way to make this function 'global' so that i can use this function more times without cut and paste the function in every script?
Like putting this function in a special named dzvents script that functions as a library?

Code: Select all

return {
	on = {devices = {'Neemfoto'}},    

    logging =   { level   = domoticz.LOG_DEBUG ,                  -- Uncomment to override the dzVents global logging setting
                  marker  = "Security"},
	
    execute = function(dz, device, info)

local function snapshot(idx,subject,delay)
    if not dz then dz = domoticz end
    if not idx then idx = 1 end
    if not subject then 
        subject = "snapshot_" .. dz.time.rawDate .. "_" .. dz.time.rawTime  
    else
        subject = subject .. "_" .. dz.time.rawDate .. "_" .. dz.time.rawTime  
    end

    if delay and delay > 0 then
        local url = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=emailcamerasnapshot&camidx=".. idx .. "&subject=" .. dz.utils.urlEncode(subject)
        dz.openURL(url).afterSec(delay)
    else
        dz.sendCommand("SendCamera:" .. idx, subject)
    end
end

local cameraID  = 1
local cameraNAME  = 'pvkcam'
local repeats   = 1  --- number of repeats 
local interval  = 1  -- seconds between repeats  

for i = 0,(repeats - 1) do 
    snapshot(cameraID,"Foto (" .. i+1 .."/" ..  repeats .. ") van camera (" .. cameraNAME .. 
                        "). Foto is gemaakt ".. (i * interval) ..  " seconden na eerste foto ", ( i * interval ))
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: global function

Post by waaren »

pvklink wrote: Wednesday 23 January 2019 10:08 I like to use this snapshot dzVents function in more scripts.
Is there a way to make this function 'global' so that i can use this function more times without cut and paste the function in every script?
Like putting this function in a special named dzvents script that functions as a library?
Yes, that is possible (see the shared helper section of dzVents wiki) .
Happy to make an example but today a native dzVents snapshot command was merged into development. So starting Domoticz V4.10363 you can use domoticz.snapshot with the option .afterHour(hours), .afterMin(minutes), .afterSec(seconds), withinHour(hours), withinMin(minutes) or withinSec(seconds)
With this I think it is no longer needed to have this as a global helper function.
Please comment if you think otherwise.
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: global function

Post by pvklink »

Great!
I wait for that! (and just installed the latest domoticz)
now examine how to call this new function and what to change in my script.
part 1 is to remove the old function of my script...
part 2 is to add new parameters

Code: Select all

return {
	on = {devices = {'Neemfoto'}},    

    logging =   { level   = domoticz.LOG_DEBUG ,                  -- Uncomment to override the dzVents global logging setting
                  marker  = "Security"},
	
    execute = function(dz, device, info)

local cameraID  = 1
local cameraNAME  = 'pvkcam'
local repeats   = 1  --- number of repeats 
local interval  = 1  -- seconds between repeats  

for i = 0,(repeats - 1) do 
    snapshot(cameraID,"Foto (" .. i+1 .."/" ..  repeats .. ") van camera (" .. cameraNAME .. 
                        "). Foto is gemaakt ".. (i * interval) ..  " seconden na eerste foto ", ( i * interval ))
end

end
}
Just installed my spirit zwave radiator valve, worked within 5 minutes. But have some questions. Can i open a topic in dzvents or is it better to use another part of the board?
I saw that you offered an other user to help with this valve and and dzvents, he didn't responped.
I would have some help
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: global function

Post by waaren »

pvklink wrote: Wednesday 23 January 2019 11:32 Now examine how to call this new function and what to change in my script.
Change

Code: Select all

for i = 0,(repeats - 1) do 
    snapshot(cameraID,"Foto (" .. i+1 .."/" ..  repeats .. ") van camera (" .. cameraNAME .. 
                        "). Foto is gemaakt ".. (i * interval) ..  " seconden na eerste foto ", ( i * interval ))
end
to

Code: Select all

for i = 0,(repeats - 1) do 
    dz.snapshot(cameraID,"Foto (" .. i+1 .."/" ..  repeats .. ") van camera (" .. cameraNAME .. 
                        "). Foto is gemaakt ".. (i * interval) ..  " seconden na eerste foto ").afterSec( i * interval )
end
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: global function

Post by pvklink »

Works!
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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest