Notification after switch is on for x minutes

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Notification after switch is on for x minutes

Post 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
genepi
Posts: 1
Joined: Saturday 05 November 2016 16:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Notification after switch is on for x minutes

Post by genepi »

User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification after switch is on for x minutes

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Notification after switch is on for x minutes

Post 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)
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Notification after switch is on for x minutes

Post by gschmidt »

I already got it....it is a dzVents Script and not a LUA
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest