Page 1 of 1

last notification in dzVents persistent data?

Posted: Saturday 09 June 2018 21:39
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?

Re: last notification in dzVents persistent data?

Posted: Saturday 09 June 2018 23:13
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
}

Re: last notification in dzVents persistent data?

Posted: Sunday 10 June 2018 22:56
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.

Re: last notification in dzVents persistent data?

Posted: Monday 11 June 2018 9:13
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 )

Re: last notification in dzVents persistent data?

Posted: Monday 11 June 2018 10:26
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.

Re: last notification in dzVents persistent data?

Posted: Monday 11 June 2018 21:24
by papoo
thanks waaren.
I test timedelay, it works fine on my script
now i'm going to test conditionDeviceState