last notification in dzVents persistent data?

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

Moderator: leecollings

Post Reply
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

last notification in dzVents persistent data?

Post by papoo »

hi all
how is it possible to set a delay between two notifications from the same script?

not to receive a notification every minute as long as condition not changed

is it possible with the dzVents persistent data?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: last notification in dzVents persistent data?

Post by waaren »

papoo wrote: Saturday 09 June 2018 21:39 how is it possible to set a delay between two notifications from the same script?
not to receive a notification every minute as long as condition not changed
is it possible with the dzVents persistent data?
I am not sure I completely understand your question but you can use dzVent persistent data to have an extra time based condition for performing an action (e.g. send a notification)

Code: Select all

--[[ notificationDelay.lua 
]]--

return {
    on      =   {   timer           =   { "every minute" }},
                
    logging =   {   level     =   domoticz.LOG_DEBUG,
                    marker    =   "notificationDelay."    },

    data    =   {   notificationSent      = { initial = "1970-1-1 1:1:1.1"  },            -- Unix epoch
                    state                 = { initial = ""                  }},
                    
    execute = function(dz, _)
        local Time                              = require('Time')
        local currentTime                       = Time(dz.time.raw)
        local storedTime                        = Time(dz.data.notificationSent)
        local deltaHours                        = dz.utils.round(currentTime.compare(storedTime).secs / 3600,2)   -- secs to be able to use decimals in hours
        
        local conditionDeviceState              = dz.devices(nnn).state  -- Your condition for sending the notification
        local hoursBetweenNotifications         = 2.5				-- two and a half hour 
        
        dz.log("delta time in hours: "  .. deltaHours,dz.LOG_DEBUG)
        
        if dz.data.state ~= conditionDeviceState then   -- has state changed ?
            dz.data.initialize("notificationSent")      -- reset to initial date
            dz.data.state = conditionDeviceState
            dz.log("notificationSent reset to : "  .. dz.data.notificationSent,dz.LOG_DEBUG)            
        end        
                
        if conditionDeviceState == "On" then                        -- Your condition for sending the notification
            if deltaHours > hoursBetweenNotifications then
                dz.log("Sending notification ",dz.LOG_DEBUG)     
                -- Replace this line with your notification
                dz.data.notificationSent = dz.time.raw               -- Store time of notification  
            else
                dz.log("No notification to be send yet",dz.LOG_DEBUG)     
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
popoff1998
Posts: 5
Joined: Saturday 09 June 2018 10:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: last notification in dzVents persistent data?

Post by popoff1998 »

The time isn't the problem, I think that you must check if the condition changed or not, so you only need to store the state in persistent data, not the time.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: last notification in dzVents persistent data?

Post by waaren »

popoff1998 wrote: Sunday 10 June 2018 22:56 The time isn't the problem, I think that you must check if the condition changed or not, so you only need to store the state in persistent data, not the time.
Could be but I prefer to code it like this. If the OP decides that notification should never be given again if state does not change then the var hoursBetweenNotifications should be set to 40*365*24 (and accept the risk that an extra notification is send in 40 years :D )
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
popoff1998
Posts: 5
Joined: Saturday 09 June 2018 10:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: last notification in dzVents persistent data?

Post by popoff1998 »

Hmmm, papoo asked for:
papoo wrote:not to receive a notification every minute as long as condition not changed
So, in your code this test, " if dz.data.state ~= conditionDeviceState then" must send the notification, not only reset the state.

For this I said that the time isn't important if you want to send the notification *every time* that the state change.
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: last notification in dzVents persistent data?

Post by papoo »

thanks waaren.
I test timedelay, it works fine on my script
now i'm going to test conditionDeviceState
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest