Page 1 of 1
lastUpdate time versus "Last Seen" in web interface [Solved]
Posted: Saturday 22 June 2019 0:50
by Shred
I've been going around and around in circles searching the forum, trying to find a way to use a script to access the "Last Seen" time that I see in the Domoticz Devices page.
I'm using the current ZigBee2MQTT plugin and a bunch of Xiaomi Aqara sensors. The sensors send a heartbeat approximately every 50 minutes. This heartbeat updates the Last Seen time, so I want to write a script similar to the "Dead Devices" script I found in this forum to send a notification if a sensor hasn't been heard for (say) 3 x 50 minute heartbeat.
The problem is that all the scripts seem to use lastUpdate e.g.:
Code: Select all
local minutes = domoticz.devices(name).lastUpdate.minutesAgo
lastUpdate is just what it says it is: it's the last time that the sensor told Domoticz that there was a change of state, it is not the last time a heartbeat was received and it's not the time that I'm seeing in the "Last Seen" field. I thought I was making progress when I found that I could see every property for a device with: "domoticz.devices(name).dump()"
I get a huge list of properties... but the only timestamp in there is the useless (to me anyway) "lastUpdate" time. How do I use dzVents to access "Last seen"?
Re: lastUpdate time versus "Last Seen" in web interface
Posted: Saturday 22 June 2019 2:52
by waaren
Shred wrote: ↑Saturday 22 June 2019 0:50
I've been going around and around in circles searching the forum, trying to find a way to use a script to access the "Last Seen" time that I see in the Domoticz Devices page.
How do I use dzVents to access "Last seen"?
The challenge here is that the Last seen property is not something that domoticz store in the database, nor does it export it to the event system. In fact it is only in memory and browser cache. So the only way to get it is to ask for it with the JSON / API call. If we would do that in dzVents for all devices for every device change it would take too many resources while hardly being used. That's why it 's not - and will not be implemented in native dzVents until the main domoticz event-system will find a way to export this attribute.
If you need the lastSeen property of devices you will have to query for it in your own script(s).
An example on how to get it is shown below. The script compares the lastUpdate timestamp with the lastSeen timeStamp and will report those with a delta > 10 seconds. But you can change it to satisfy your requirements. It is just an example
And please note that the state of the lastSeen property is not preserved over domoticz restarts.
Code: Select all
local scriptVar = 'lastSeen'
return {
on = { timer = {'every minute'}, httpResponses = { scriptVar }},
logging = { level = domoticz.LOG_ERROR, marker = scriptVar },
execute = function(dz, item)
if not (item.isHTTPResponse) then
dz.openURL({
url = dz.settings['Domoticz url'] .. '/json.htm?type=devices&used=true',
callback = scriptVar })
else
local Time = require('Time')
for _, node in pairs(item.json.result) do
-- if node.Used == "1" and dz.devices(tonumber(node.idx)) then
if node.Used ~= "1" and dz.devices(tonumber(node.idx)) then
local lastSeen = Time(node.LastUpdate).secondsAgo
local lastUpdated = dz.devices(tonumber(node.idx)).lastUpdate.secondsAgo
local delta = lastSeen - lastUpdated
if delta > 10 then -- lastSeen longer ago
dz.log('id '.. node.idx .. '(' ..node.Name .. ') Delta ' .. delta ,dz.LOG_FORCE)
end
end
end
end
end
}
Re: lastUpdate time versus "Last Seen" in web interface
Posted: Saturday 22 June 2019 7:57
by hoeby
Check
this thread
scroll to the last post of this thread, not the first.
The first works on lastupdate
the last works on lastseen
I also wanted to check the last seen from the device, not the last update
I am using it on Xiaomi smokedetectors, which only send a last update when they are in alarm.
Therefor i wanted to use the last seen. The script i changed, works for me on the last seen.
Please be free to look at it. And when the script would be wrong, please give some information what is going wrong, than we can look at it.
Re: lastUpdate time versus "Last Seen" in web interface
Posted: Saturday 22 June 2019 12:33
by Shred
Thankyou both waaren and hoeby for taking the time to give such detailed answers. I think that your responses have put me on the right track.