last notification in dzVents persistent data?

Easy to use, 100% Lua-based event scripting framework.
Post Reply
User avatar
papoo
Posts: 86
Joined: Friday 22 January 2016 23:14
Target OS: Raspberry Pi
Domoticz version: beta
Location: France
Contact:

last notification in dzVents persistent data?

Post by papoo » Saturday 09 June 2018 21:39

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: 684
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: last notification in dzVents persistent data?

Post by waaren » Saturday 09 June 2018 23:13

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
}
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

popoff1998
Posts: 5
Joined: Saturday 09 June 2018 10:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: last notification in dzVents persistent data?

Post by popoff1998 » 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.

User avatar
waaren
Posts: 684
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: last notification in dzVents persistent data?

Post by waaren » Monday 11 June 2018 9:13

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 )
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

popoff1998
Posts: 5
Joined: Saturday 09 June 2018 10:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: last notification in dzVents persistent data?

Post by popoff1998 » Monday 11 June 2018 10:26

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: 86
Joined: Friday 22 January 2016 23:14
Target OS: Raspberry Pi
Domoticz version: beta
Location: France
Contact:

Re: last notification in dzVents persistent data?

Post by papoo » Monday 11 June 2018 21:24

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 2 guests