Page 1 of 1

Notification after switch is on for x minutes

Posted: Saturday 02 March 2019 17:34
by gschmidt
Hi,

Is there an example of a script which sends a notification when a switch is on and when it is on for X minutes?
can't figure it out with blockly

Greetzzz,

Gerben

Re: Notification after switch is on for x minutes

Posted: Saturday 02 March 2019 18:33
by genepi

Re: Notification after switch is on for x minutes

Posted: Saturday 02 March 2019 18:46
by waaren
gschmidt wrote: Saturday 02 March 2019 17:34 Hi,

Is there an example of a script which sends a notification when a switch is on and when it is on for X minutes?
can't figure it out with blockly

Greetzzz,

Gerben
Could be something like

Code: Select all

-- Check Switches.lua
local deviceNamesArray = {"Test_1", "Test_2",}
return {
            on =    
                    {
                        devices = deviceNamesArray,
                        timer = {'every 5 minutes'},   
                    },
                    
        logging    =   
                        {   
                            level       =  domoticz.LOG_DEBUG,   
                            marker      =  "Check switches" 
                        },    

    execute = function(dz,item)
        local function logWrite(str,level)             -- Support function for shorthand debug log statements
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local maxOnTime = 15  -- maxOnTime in minutes 

        local myNotificationTable   = {   
                                                        -- table with one or more notification system. 
                                                        -- Can be one or more of
                                                        -- dz.NSS_GOOGLE_CLOUD_MESSAGING, 
                                                        dz.NSS_PUSHOVER,               
                                                        -- dz.NSS_HTTP, 
                                                        -- dz.NSS_KODI, 
                                                        -- dz.NSS_LOGITECH_MEDIASERVER, 
                                                        -- dz.NSS_NMA,
                                                        -- dz.NSS_PROWL, 
                                                        -- dz.NSS_PUSHALOT, 
                                                        -- dz.NSS_PUSHBULLET, 
                                                        -- dz.NSS_PUSHOVER, 
                                                        -- dz.NSS_PUSHSAFER,
                                } -- uncomment the ones you need

        if item.isTimer  then 
            for index, deviceName in ipairs(deviceNamesArray) do
                local device = dz.devices(deviceNamesArray[index])
                if device.active and device.lastUpdate.minutesAgo > maxOnTime then
                    local message = "device " .. device.name .. " already on for " .. tostring(device.lastUpdate.minutesAgo) .. " minutes." 
                    logWrite ("Check switch: " .. message)
                    dz.notify("Check switch",message,nil,nil,"",  myNotificationTable ) 
                end
            end
        
        elseif item.active then 
            logWrite(item.name .. " is switched on.")
            dz.notify("Check switch","device " .. item.name .. " switched on" ,nil,nil,"",  myNotificationTable ) 
        end
    end
}

Re: Notification after switch is on for x minutes

Posted: Sunday 03 March 2019 12:16
by gschmidt
waaren wrote: Saturday 02 March 2019 18:46
gschmidt wrote: Saturday 02 March 2019 17:34 Hi,

Is there an example of a script which sends a notification when a switch is on and when it is on for X minutes?
can't figure it out with blockly

Greetzzz,

Gerben
Could be something like

Code: Select all

-- Check Switches.lua
local deviceNamesArray = {"Test_1", "Test_2",}
return {
            on =    
                    {
                        devices = deviceNamesArray,
                        timer = {'every 5 minutes'},   
                    },
                    
        logging    =   
                        {   
                            level       =  domoticz.LOG_DEBUG,   
                            marker      =  "Check switches" 
                        },    

    execute = function(dz,item)
        local function logWrite(str,level)             -- Support function for shorthand debug log statements
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local maxOnTime = 15  -- maxOnTime in minutes 

        local myNotificationTable   = {   
                                                        -- table with one or more notification system. 
                                                        -- Can be one or more of
                                                        -- dz.NSS_GOOGLE_CLOUD_MESSAGING, 
                                                        dz.NSS_PUSHOVER,               
                                                        -- dz.NSS_HTTP, 
                                                        -- dz.NSS_KODI, 
                                                        -- dz.NSS_LOGITECH_MEDIASERVER, 
                                                        -- dz.NSS_NMA,
                                                        -- dz.NSS_PROWL, 
                                                        -- dz.NSS_PUSHALOT, 
                                                        -- dz.NSS_PUSHBULLET, 
                                                        -- dz.NSS_PUSHOVER, 
                                                        -- dz.NSS_PUSHSAFER,
                                } -- uncomment the ones you need

        if item.isTimer  then 
            for index, deviceName in ipairs(deviceNamesArray) do
                local device = dz.devices(deviceNamesArray[index])
                if device.active and device.lastUpdate.minutesAgo > maxOnTime then
                    local message = "device " .. device.name .. " already on for " .. tostring(device.lastUpdate.minutesAgo) .. " minutes." 
                    logWrite ("Check switch: " .. message)
                    dz.notify("Check switch",message,nil,nil,"",  myNotificationTable ) 
                end
            end
        
        elseif item.active then 
            logWrite(item.name .. " is switched on.")
            dz.notify("Check switch","device " .. item.name .. " switched on" ,nil,nil,"",  myNotificationTable ) 
        end
    end
}
I get an error in the log when using this script:

Code: Select all

Error: EventSystem: in Frietpan_timer: [string "-- Check Switches.lua..."]:12: attempt to index global 'domoticz' (a nil value)

Re: Notification after switch is on for x minutes

Posted: Sunday 03 March 2019 12:29
by gschmidt
I already got it....it is a dzVents Script and not a LUA