I've searched the wiki, this forum, and the internet, but can't find a way to get a devices "last seen" timestamp in a LUA script.
Getting "lastupdate" is not a problem.
Any hints? I need it in LUA, to fit into an existing script.
To explain: this is to check if smoke detectors are still active. Every so often, the detectors send a message in via ZIgbee, which changes the "last seen" timestamp. It does not change the lastupdate, I assume this only happens when the detector changes state: alarm.
LastSeen in LUA
Moderator: leecollings
-
manjh
- Posts: 859
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
LastSeen in LUA
Hans
-
sammyke007
- Posts: 204
- Joined: Monday 08 May 2017 20:48
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2022.1
- Location: Belgium
- Contact:
Re: LastSeen in LUA
I use this script (dzvents), found on the forum (not written by me) to check my door/window sensors. They get updated (last seen) by zigbee2mqtt every 50 minutes, even without being opened or closed (state change). It's working fine over here.
EDIT: credits: viewtopic.php?t=18566&start=20
EDIT: credits: viewtopic.php?t=18566&start=20
Code: Select all
--[[
CheckDeadDevices script voor oa. Zigbee open/close contacts die wel een lastupdate sturen, maar niet van state veranderd zijn.
Verion: 19061603
WHAT DOES THE SCRIPTS?:
It looks to the LAST SEEN date/time of a device. When threshold runs out, you get a notification to check the device.
Be sure you are adding devices that are updated in a normal situation.
For example, devices connected to a Xiaomi gateway are not updatet, unless activated. When you add a smoke detector tru Xiaomi gateway, than the smoke detector has to give a smoke-alarm before the threshold runs out. Otherwise you get a notification.
Advice for Xiaomi smoke-detectors is to use Zigbee2MQTT, this gateway does update the LAST SEEN date/time of Xiaomi smoke-detectors.
WHAT TO SETUP TO MAKE THIS SCRIPT WORKING
Fill in the local network setting for Domoticz.
Go to SETTINGS --> SETTINGS --> SYSTEM --> LOCAL NETWORK.
Fill in your IP adres. When it is local you could fill in 127.0.0.*
Make a dummy device named: opVakantie
Dummy device opVakantie changes the threshold timer for the devices-check
The idea is: opVakantie Device ON = longer threshold numbers, so you don't get notification on your holiday
And opVakantie Device Off = to have shorter threshold numbers.
Set telegram true is you want telegram notification,
but therefor apiKEY and chatID has to be filled in SETTINGS --> SETTINGS --> NOTIFICATIONS --> TELEGAM
Set email true is you want to receive an e-mail, but therefor the e-mail has te be setup in SETTINGS --> SETTINGS --> E-MAIL
Fill in you e-mail address where to send to
Of course fill in the devices you would like to check.
You could choose to fill in with name or IDX number of the device.
There is a line which says: -- Don't change things undernead this line
If you have no idea how the scripts works, please lissen to this.
If you know how it works and have ideas, please post them on http://www.domoticz.com/forum/viewtopic.php?f=59&t=18566&start=20
]]--
local scriptVar = 'lastSeen'
return {
on = { timer = {'every 30 minutes'}, httpResponses = { scriptVar }},
--on = { timer = {'every minute'}, httpResponses = { scriptVar }}, -- Only use this one to test. When working disable it and activate "every 30 minutes"
--on = { timer = {'at 08:00', 'at 10:00'}, httpResponses = { scriptVar }},
logging = { level = domoticz.LOG_ERROR, marker = scriptVar },
data = { notified = { initial = {} } },
execute = function(dz, item)
--Telegram settings
local telegram = false -- Set true for sending telegram, set false for NOT sending telegram.
--Email settings (domoticz email settings have to be filled in)
local email = true -- Set true for sending email, set false for NOT sending email.
local mailaddress = '[email protected]' -- Fill in your e-mail address, which the notification needs to be send to.
-- Global settings
local notifyTime = 'at 10:00' -- You can add multiple notification times, i.e. 'at 12:00, at 20:00'
local notifyHead = 'dzCheckSensors' -- This is the start of everynotification, so you know it is this script.
-- Devices to check NO holiday.
-- Devices name or IDX number column = Time column in seconds
local devices_No = {
['Keukenraam'] = 86400, -- 1 Day
}
-- Devices to check when it is holiday.
-- Devices name or IDX number column = Time column in seconds
local devices_Yes = {
['Keukenraam'] = 86400, -- 1 Day
}
-- Don't change things undernead this line
local notified = dz.data.notified -- short reference
function Timers(sec)
local ti
sec=tonumber(sec)
if sec == nil then ti='NIL'
elseif sec >= 86400 then ti=string.sub(tostring(sec/24/3600),1,4)..' days'
elseif sec >= 3600 then ti=string.sub(tostring(sec/3600),1,4)..' hours'
elseif sec >= 60 then ti=string.sub(tostring(sec/60),1,4)..' minutes'
else ti=sec..' seconds'
end
return ti
end
if not (item.isHTTPResponse) then
dz.openURL({
url = dz.settings['Domoticz url'] .. '/json.htm?type=devices&used=true',
callback = scriptVar })
else
if dz.devices('opVakantie').state== 'On' then
dz.log(notifyHead .." ".. 'Vakantie-On',dz.LOG_FORCE)
DevicesToCheck = devices_Yes
else
dz.log(notifyHead .." ".. 'Vakantie-Off',dz.LOG_FORCE)
DevicesToCheck = devices_No
end
local Time = require('Time')
for _, node in pairs(item.json.result) do
local toCheck = ( DevicesToCheck[node.idx] or DevicesToCheck[node.Name])
if toCheck then
local lastUpdate = Time(node.LastUpdate).secondsAgo
local deviceName = (node.Name or node.idx)
local threshold = toCheck
if lastUpdate < threshold then
--device is alive
if notified[deviceName] == nil then
dz.log(notifyHead .." "..deviceName.." : "..Timers(lastUpdate).. ' was last response, threshold='..Timers(threshold),dz.LOG_FORCE)
else
dz.log(notifyHead .." ".. ' Signal back from sensor '..deviceName,dz.LOG_FORCE)
notified[deviceName] = nil --remove from notified list
if telegram then
dz.notify(notifyHead, notifyHead .." ".. 'Signal back from sensor '..deviceName..'.',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
end
if email then
dz.email('Domoticz device check', ''..notifyHead.. ' Signal back from sensor '..deviceName..'', ''..mailaddress..'')
end
end
else
--lost signal:
if notified[deviceName] == nil then
dz.log (notifyHead .." ".. 'Signal lost: '..deviceName .." sent "..Timers(lastUpdate).. ' the last signal, threshold='..Timers(threshold)..'. Battery empty?',dz.LOG_FORCE)
notified[deviceName] = 1
if telegram then
dz.notify(notifyHead, notifyHead ..' Signal lost: '..deviceName ..' sent '..Timers(lastUpdate).. ' the last signal, threshold='..Timers(threshold)..'. Battery empty?',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
end
if email then
dz.email('Domoticz device check', ''..notifyHead..' Signal lost: '..deviceName ..' sent '..Timers(lastUpdate).. ' the last signal, threshold='..Timers(threshold)..'. Battery empty?', ''..mailaddress..'')
end
elseif
dz.time.matchesRule(notifyTime) and
dz.devices('opVakantie').state == 'Off'
then
dz.log(notifyHead .." ".. 'Reminder, '..deviceName .." sent "..Timers(lastUpdate).. ' the last signal. Battery empty?',dz.LOG_FORCE)
if telegram then
dz.notify(notifyHead, notifyHead .." ".. 'Reminder, '..deviceName .." sent "..Timers(lastUpdate).. ' the last signal. Battery empty?',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
end
if email then
dz.email('Domoticz device check', ''..notifyHead..' Signal lost: '..deviceName ..' sent '..Timers(lastUpdate).. ' the last signal, threshold='..Timers(threshold)..'. Battery empty?', ''..mailaddress..'')
end
end
end
end
end
end
end
}
--[[
Changes version 19061603
-Possible to fill in IDX number or device name to be checked.
Changes version 19061602
-Using dzVents: natively send a notification to telegram
Changes version 19061601
-WriteTime removed, this only gives NIL and wasn't used
-Script now works with url = dz.settings['Domoticz url']
Changes version 19061501
-E-mail option added
-With false/true you could actived of de-actived e-mail or telegram
-All settings on top of the script
]]--
-
manjh
- Posts: 859
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: LastSeen in LUA
Never mind, I assume it cannot be done easely in LUA, so in stead of integrating into an existing LUA script, I created a separate dzVents script.
It now works perfectly!
Hans
-
sammyke007
- Posts: 204
- Joined: Monday 08 May 2017 20:48
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2022.1
- Location: Belgium
- Contact:
Re: LastSeen in LUA
Voilamanjh wrote:Never mind, I assume it cannot be done easely in LUA, so in stead of integrating into an existing LUA script, I created a separate dzVents script.
It now works perfectly!


Verstuurd vanaf mijn Oneplus 8 met Tapatalk
Who is online
Users browsing this forum: No registered users and 1 guest