Device.lastUpdate question

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

Moderator: leecollings

Post Reply
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Device.lastUpdate question

Post by ronaldbro »

Hi,

Can anyone (probably waaren ;)) explain what the value of lastUpdate means?
I can't understand the value of lastUpdate of my window sensors (Haven't checked other devices).

I have some window sensors which haven't had a changed setpoint in days, I can see that in the log. The last seen timestamp in the hardware page shows it was seen about 15 minutes ago and the lastUpdate of the device says 5 hours ago... So what happened 5 hours a ago? (It could be a restart of domoticz...)

My actual problem is, I want the last seen timestamp as displayed on the hardware page in dzVents. How can I do that?

Some background. I'm working on my roller shutter script and of course I want to check if the window is open but also if the window sensor is still alive. My thoughts was to use device.lastUpdate.minutesAgo > 60 to determine an unreliable window state.

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

Re: Device.lastUpdate question

Post by waaren »

ronaldbro wrote: Wednesday 20 November 2019 1:23 I can't understand the value of lastUpdate of my window sensors (Haven't checked other devices).
I tried to explain it in this post
My actual problem is, I want the last seen timestamp as displayed on the hardware page in dzVents. How can I do that?
in dzVents you will need an API call to domoticz for that.

Below script does check the lastSeen field for a defined set of devices.

Code: Select all

local scriptVar = 'lastSeen'

return {
    on = { timer =  {'every minute'}, httpResponses = { scriptVar }},
    logging = { level   = domoticz.LOG_DEBUG, marker  = scriptVar },
    
    execute = function(dz, item) 
        local aMinute = 60                   -- Old habits ... ;-)
        local anHour = 60 * aMinute
        local aDay = 24 * anHour
        local twoDays = 2 * aDay
        local aWeek = 7 * aDay
        local twoWeeks = 14 * aDay

      
        local function getTimeString(seconds)
            if not seconds then return 'NIL' end

            local width = 4
            if seconds > aWeek then return string.sub(tostring(seconds/aWeek),1,width)..' weeks' end
            if seconds > aDay  then return string.sub(tostring(seconds/aDay),1,width)..' days' end
            if seconds > anHour then return string.sub(tostring(seconds/anHour),1,width)..' hours' end
            if seconds > aMinute then return string.sub(tostring(seconds/aMinute),1,width)..' minutes' end
            
            return seconds .. ' seconds'
        end
        
        if not (item.isHTTPResponse) then
            dz.openURL({ 
                url = dz.settings['Domoticz url'] .. '/json.htm?type=devices&used=true',
                callback = scriptVar })
        else
            local devicesToBeChecked =  { ["982"] = twoDays, ["35"] = anHour, ["1003"]= twoDays, ["1011"]= twoDays, ["Watermeter"]= aDay } -- change to idx / name of the devices you want to check
            
            local Time = require('Time')
            for _, node in pairs(item.json.result) do
                -- dz.log(node.idx,dz.LOG_DEBUG)
                local toCheck = ( devicesToBeChecked[node.idx] or devicesToBeChecked[node.Name]) 
                if toCheck then -- note that any other value than false or nil is true in Lua
                    dz.log('id '..  node.idx .. '(' ..node.Name ..
                                    ') lastUpdate was ' .. 
                                    getTimeString(Time(node.LastUpdate).secondsAgo) ..
                                    ' ago. Treshold is ' .. getTimeString(toCheck)
                                    ,dz.LOG_FORCE)
                end
            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
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Device.lastUpdate question

Post by ronaldbro »

Thanks waaren, stil don't understand the functional meaning of the lastUpdate. But I can work with the example in your script.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest