Compute minutes virtual switch
Posted: Saturday 26 December 2020 20:12
Hello, I would like to compute the total number of minutes that a virtual switch has been 'on' during a day. I have made a script that manages to download the json data from the database, I suppose that now it would be necessary to create another function that compares the 'on' and 'off' states to calculate the range time that it has been working and accumulate it in a counter. The result can be given in a text variable, for example. Here is the code I have developed, some help is welcome.
Code: Select all
local activeDevice = 592
local scriptVar = 'Compute total minutes'
return
{
on =
{
timer =
{
'every 1 minutes', -- for DEBUG
},
httpResponses =
{
scriptVar .. '*',
},
},
logging =
{
level = domoticz.LOG_DEBUG ,
marker = scriptVar,
},
execute = function(dz, item)
if not(dz.devices(activeDevice).active) then return end
local AC_room = dz.devices(590)
local function triggerJSON(id, delay)
url = dz.settings['Domoticz url'] .. '/json.htm?type=lightlog&idx=' .. id
dz.openURL({ url = url, callback = scriptVar .. '_' .. id}).afterSec(delay or 0)
end
-- main
if item.isTimer then
triggerJSON(AC_room.id)
end
if item.isHTTPResponse then
if item.isJSON then
rt = item.json.result
--dz.utils.dumpTable(item.json) -- dump the string represenation of the complete table to log
local time = rt[1]["Date"]
dz.log(time,dz.LOG_DEBUG)
end
end
end
}