My Notification Delay Solution(s)

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

Moderator: leecollings

Post Reply
Wob76
Posts: 110
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

My Notification Delay Solution(s)

Post by Wob76 »

Since someone showed some interest in my solution to delaying notifications I thought I would post my scripts, or at least relevant snippets. I have only just implemented these, so I may yet find some bugs, but preliminary testing is positive.

I wanted to delay messages, and have them not send if a device returns to the desired state within a set time.

I use dummy switches to trigger my script again after a delay, I do this as the switch can be configured to be momentary with a variable delay, this saves using and delays inside the script. I am also able to read the values of switches in Dashitcz, which I can't do with variables.

This is my switch (1 min Delay)
switch.JPG
switch.JPG (20.18 KiB) Viewed 1722 times

My first example is a script I use to monitor my Doors\Windows while the AC is on, I have had this in place for sometime, but the messages were instant, if you are just opening a door\window for a moment it is a little annoying. I was considering using the .minsAgo() function, its not yet supported by .notify, and issuing a .cancelQueuedCommands() would cancel any message, not just the ones from this script.

I have the delay switch being triggered from inside a foreach loop that inspects all my "Openings" since it loops through this on each pass the switch will be turned back on as long as any of the openings is open, the end result is the script will run every 1 mins as long as something is open, then return to only device triggers when they are all closed (or the AC is powered off)

I had an issue with "item.lastUpdate.minutesAgo >= 5" inside the loop the open device would report the lastupdate of the time before the opening that triggered the script, so even though the door\window had just opened, triggering the script, it would send a message using just this condition. To resolve that I also check that the script was triggered by my delay switch device, so message will only send if triggered by that, and not by an opening. The openings only activate the delay switch (hope that all makes sense)

I have also added a function to turn the AC off if the device is left open for longer than 60 mins, but that is untested.

I have cut snippets from my larger AC Check script to make it a little easier to digest, If I have messed up something or if something doesn't make sense, just ask.

I also have a RGB Notification Light, near the AC controls, that turns Yellow if something is open, this is not delayed. It turns other colours when zones are too hot\cold, but that's a mix between other scripts.

Code: Select all

-- Device Action Switches
local POWER_SWITCH = "AC Power"
local MODE_SWITCH = "AC Mode"

-- External Windows and Doors
local FRONTDOOR_DEVICE = "Front Door"
local BACKDOOR_DEVICE = "Back Door"
local LAUNDRYWINDOW_DEVICE = "Laundry Window"
local PLAYROOM_DEVICE = "Playroom"

-- Notification Light
local GATEWAY_LIGHT = "Xiaomi Light"

-- Variable Switches
local AC_DOORLIGHT = "AC_DoorLight"
local AC_NOTIFY_DELAY = "AC_Notification_Delay"

local LOGGING = true

return {
    active = true,
    on = {
        devices = {
            POWER_SWITCH,
            FRONTDOOR_DEVICE,
            BACKDOOR_DEVICE,
            LAUNDRYWINDOW_DEVICE,
            PLAYROOM_DEVICE,
            AC_NOTIFY_DELAY,
            }
    },
    logging = {
    level = domoticz.LOG_INFO,
    marker = "AC Checkz"
    },
    execute = function(domoticz, device, triggerInfo)
        -- define script action switches
        local power = domoticz.devices(POWER_SWITCH)
        local mode = domoticz.devices(MODE_SWITCH)
        
        -- Notification Light
        local gatewaylight = domoticz.devices(GATEWAY_LIGHT)
        
        -- Variable Switches
        local doorlight = domoticz.devices(AC_DOORLIGHT)
        local notifydelay = domoticz.devices(AC_NOTIFY_DELAY)
        
        -- Door monitoring Devices
        local openings = {
            [FRONTDOOR_DEVICE]=true,
            [BACKDOOR_DEVICE]=true,
            [LAUNDRYWINDOW_DEVICE]=true,
            [PLAYROOM_DEVICE]=true,
        }
        
        -- Check the Doors and Windows (Warning Light)
        local open_count = 0
        if (power.state == "On" and mode.state ~= "Fan") then
            domoticz.devices().filter( function(item)
                return (openings[item.name] == true)
            end).forEach( function(item)
                if LOGGING then domoticz.log(item.name.." is "..item.state, domoticz.LOG_INFO) end
                if (item.state == "Open") then
                    if (doorlight.state == "Off") then -- Check doorlight
                        if LOGGING then domoticz.log("Turning ON the Openings Light as "..item.name.." is "..item.state..", since "..item.lastUpdate.minutesAgo.." Mins Ago", domoticz.LOG_FORCE) end
                        domoticz.scenes('Yellow Light').switchOn()
                        doorlight.switchOn()
                    end
                    notifydelay.switchOn().checkFirst().silent()
                    open_count = open_count + 1
                    -- Triggered by Delay Swtich and Item Open for longer than 5 Mins, OR Trigged by AC Power turning ON
                    if ((device.name == notifydelay.name and item.lastUpdate.minutesAgo >= 5) or (device.name == power.name and device.state == "On")) then 
                        if (domoticz.variables('AC_Message1').lastUpdate.minutesAgo > 30) then -- Notification Rate limit (1 per 30 Mins)
                            if LOGGING then domoticz.log("Sending a notification", domoticz.LOG_INFO) end
                            count = domoticz.variables('AC_Message1').value + 1
                            domoticz.variables('AC_Message1').set(count)
                            domoticz.notify("The " .. item.name .. " is Open",
                                "The AC is on and " .. item.name .. " is open. Close " .. item.name .. " for better efficency.",
                                domoticz.PRIORITY_NORMAL,
                                domoticz.SOUND_COSMIC)
                        end
                        -- Triggered by Delay Swtich and Item Open for longer than 60 Mins
                        if (device.name == notifydelay.name and item.lastUpdate.minutesAgo >= 60) then
                            power.switchOff() -- Turn the AC OFF
                        end
                    end
                end
            end)
            if (open_count == 0 and doorlight.state ~= "Off") then
                if LOGGING then domoticz.log("Turning OFF the Openings Light, Opening Closed", domoticz.LOG_FORCE) end
                gatewaylight.switchOff().checkFirst()
                doorlight.switchOff().checkFirst()
                notifydelay.switchOff().checkFirst().silent()
            end
        end  
    end
}
Wob76
Posts: 110
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: My Notification Delay Solution(s)

Post by Wob76 »

My second script was a Door\Window monitor for when the house is empty, if something opens\closes when nobody is home it sends a notification. Again I have had this script running for some time, but quite often when I get home I have opened the back door before my phone has connected to the wifi (my presence detection method of choice). So I wanted to delay and check again if someone is home after a delay.

The issue I had here is I wanted to carry forward the device and its state in that delay, so I write that out to a variable that is read back if nobody is still home. My first check is for presence, so if the script is triggered by the delay device and someone is home, nothing will happen.oh...

I should have also noted I record the number of messages sent into a variable, if you are wondering what the count code is doing in both scripts.

No edits to this as this is all this script does, so shouldn't be any editing errors, feel free to ask questions.

Code: Select all

-- Occupied Switches
local HOUSE_SWITCH = "House Occupied"
local GUESTS_SWITCH = "Guests Visiting" -- Known Guests

-- External Windows and Doors
local FRONTDOOR_DEVICE = "Front Door"
local BACKDOOR_DEVICE = "Back Door"
local PLAYROOMDOOR_DEVICE = "Playroom Door"
local LAUNDRYWINDOW_DEVICE = "Laundry Window"
local PLAYROOMWINDOW_DEVICE = "Playroom Window"

-- Message Delay Switch
local NOTIFY_DELAY = "Alert_Notification_Delay"

local LOGGING = true

return {
    active = true,
    on = {
        devices = { 
            FRONTDOOR_DEVICE,
            BACKDOOR_DEVICE,
            PLAYROOMDOOR_DEVICE,
            LAUNDRYWINDOW_DEVICE,
            PLAYROOMWINDOW_DEVICE,
            NOTIFY_DELAY,
            -- "Motion Sensor",
            -- "Test Switch",
        },
    },
    logging = {
    level = domoticz.LOG_ERROR,
    marker = "Motion Alertz"
    },

    execute = function(domoticz, device, triggerInfo)

        local house = domoticz.devices(HOUSE_SWITCH)
        local guests = domoticz.devices(GUESTS_SWITCH)
        
        local notifydelay = domoticz.devices(NOTIFY_DELAY)
        
        if LOGGING then domoticz.log("Trigger Device State:"..device.state..", All Devices states "..domoticz.devices("Front Door").state.." "..domoticz.devices("Back Door").state.." "..domoticz.devices("Playroom Door").state.." "..domoticz.devices("Laundry Window").state.." "..domoticz.devices("Playroom Window").state, domoticz.LOG_INFO) end
        -- Check if anybody is home
        if (house.state == "Off" and guests.state == "Off") then
            -- If not delay switch
            if (device.name ~= notifydelay.name) then 
                if LOGGING then domoticz.log("Alert on "..device.name..", the house should be empty.", domoticz.LOG_FORCE) end
                -- If device is a door or window
                if (string.match(device.name, "Door")~=nil or string.match(device.name, "Window")~=nil) then
                    notifydelay.switchOn().checkFirst()
                    domoticz.variables('Alert_Opening').set(device.name.." changed to "..device.state) -- Store device.name in variable
                -- If device is a montion sensor (not currently active)
                elseif (string.match(device.name, "Motion Sensor")~=nil) then
                    if (domoticz.variables('Alert_Message').lastUpdate.minutesAgo > 5) then -- Rate limit (5 Mins)
                        count = domoticz.variables('Alert_Message').value + 1
                        domoticz.variables('Alert_Message').set(count)
                        domoticz.notify("Motion Alert!",
                            "Detected movement on "..device.name..".",
                            domoticz.PRIORITY_NORMAL,
                            domoticz.SOUND_GAMELAN)
                    end
                end
            -- If delay switch turning off and still nobody home then notify
            elseif (device.state == "Off" and device.name == notifydelay.name) then
            if LOGGING then domoticz.log("Still nobody home, sending alert for "..domoticz.variables('Alert_Opening').value, domoticz.LOG_FORCE) end
                    count = domoticz.variables('Alert_Message').value + 1
                    domoticz.variables('Alert_Message').set(count)
                    domoticz.notify("House Alert!",
                                        "Detected that "..domoticz.variables('Alert_Opening').value..", but nobody seems to be home.",
                                        domoticz.PRIORITY_NORMAL,
                                        domoticz.SOUND_GAMELAN)
            end
        end
    end
}
Wob76
Posts: 110
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: My Notification Delay Solution(s)

Post by Wob76 »

Should have noted.
Domoticz Version: 3.8932
dzVents Version: 2.4.1
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: My Notification Delay Solution(s)

Post by waaren »

@Wob76,

thx for sharing!
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Wob76
Posts: 110
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: My Notification Delay Solution(s)

Post by Wob76 »

waaren wrote: Friday 18 May 2018 12:46 @Wob76,

thx for sharing!
No problem, happy to hear any suggestions on improvements etc
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest