Page 1 of 1
Is it possible to read data from log into a script
Posted: Wednesday 28 October 2020 13:30
by abdolhamednik
Hello everyone
Imagine when a sensor is out of order and you want to use its data from past days right at this time...
Is it possible to read data from log into a lua script?

i.e. log of 'sensors/switches'
Re: Is it possible to read data from log into a script
Posted: Wednesday 28 October 2020 13:52
by waaren
abdolhamednik wrote: ↑Wednesday 28 October 2020 13:30
Is it possible to read data from log into a lua script?

i.e. log of 'sensors/switches'
Yes, that is possible with both classic Lua (via an OS curl) command and with dzVents openURL().
In both methods you call one of
these API entries
Re: Is it possible to read data from log into a script
Posted: Thursday 29 October 2020 7:52
by abdolhamednik
very nice
thank u waaren
but now 2 questions:
1.how to fetch data for an exact time? it is not mentioned here the exact time:
Code: Select all
/json.htm?type=graph&sensor=temp&idx=IDX&range=day
it only gave me last 6 5-min data, like this:
- Spoiler: show
Code: Select all
{
"result" : [
{
"d" : "2020-10-29 09:50",
"te" : 23.0
},
{
"d" : "2020-10-29 09:55",
"te" : 23.0
},
{
"d" : "2020-10-29 10:00",
"te" : 23.0
},
{
"d" : "2020-10-29 10:05",
"te" : 23.0
},
{
"d" : "2020-10-29 10:10",
"te" : 23.0
},
{
"d" : "2020-10-29 10:15",
"te" : 23.0
}
],
"status" : "OK",
"title" : "Graph temp day"
}
and
2.how to use it in lua? for example if i want temp of 2020-10-27 10:00
Re: Is it possible to read data from log into a script
Posted: Thursday 29 October 2020 8:14
by waaren
abdolhamednik wrote: ↑Thursday 29 October 2020 7:52
1.how to fetch data for an exact time?
You cannot get the individual datapoints from the API. You collect all and select what's needed in your script .
2.how to use it in Lua? for example if i want temp of 2020-10-27 10:00
Fot the 5 minute granularity you can only go back for a limited number of days (as set in [Log History] Short Log Sensors number of days:)
For day values you can go back to when you started to collect the data.
Most code needed in Lua to do this, is already part of dzVents (100 % Lua) and I don't think it makes sense to redo this work. So If dzVents is ok for you I can get you an example.
Re: Is it possible to read data from log into a script
Posted: Thursday 29 October 2020 9:10
by abdolhamednik
Most code needed in Lua to do this, is already part of dzVents (100 % Lua) and I don't think it makes sense to redo this work. So If dzVents is ok for you I can get you an example.
that's very good. i would be thankful
Re: Is it possible to read data from log into a script
Posted: Thursday 29 October 2020 12:38
by waaren
abdolhamednik wrote: ↑Thursday 29 October 2020 9:10
Most code needed in Lua to do this, is already part of dzVents (100 % Lua) and I don't think it makes sense to redo this work. So If dzVents is ok for you I can get you an example.
that's very good. i would be thankful
Example data retrieval (temperature sensor) from short- and long log
Code: Select all
-- getDataPoints
local scriptVar = 'getDataPoints'
return
{
on =
{
timer =
{
'at 7:30' -- Your preferred time
},
devices =
{
scriptVar, -- just for test can be ignored
},
httpResponses =
{
scriptVar .. '*',
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = scriptVar,
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
local sensorIDX = 3 -- device id of a temperature sensor
-- ****************************** No changes required below this line *********************************************
local function getData(id, range, delay)
local range = range or 'year'
local actYear = ''
if range == 'year' then
actYear = dz.time.year
end
local URLString = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=temp&range=' .. range .. '&idx=' .. id .. '&actyear=' .. actYear
dz.openURL(
{
url = URLString,
callback = scriptVar .. '_' .. range
}).afterSec(delay or 0)
end
-- Main
if item.isTimer or item.isDevice then
getData(sensorIDX)
getData(sensorIDX, 'day')
elseif item.isJSON and #item.json.result > 0 then -- statusCode = OK and JSON contains enough records
history = item.json.result
if item.trigger:find('day') then -- short log history
dz.log('First record in short log: ' .. history[1].d .. '; temperature: ' .. history[1].te, dz.LOG_FORCE )
dz.log('Last record in short log: ' .. history[#history].d .. '; temperature: ' .. history[#history].te, dz.LOG_FORCE )
dz.log('Number of records in short log: ' .. #history, dz.LOG_FORCE )
else
lastYearHistory = item.json.resultprev
dz.log('first record in return: ' .. lastYearHistory[1].d .. '; temperatures: ' .. lastYearHistory[1].te .. ' ((high), ' .. lastYearHistory[1].tm .. ' (low), ' .. lastYearHistory[1].ta .. ' (average)', dz.LOG_FORCE )
dz.log('last record in return: ' .. history[#history].d .. '; temperatures: ' .. history[#history].te .. ' ((high), ' .. history[#history].tm .. ' ((low), ' .. history[#history].ta .. ' (average)', dz.LOG_FORCE )
dz.log('number of records in return: ' .. (#history + #lastYearHistory), dz.LOG_FORCE )
-- dz.utils.dumpTable(history)
end
else
dz.log('Could not get (good) data from domoticz!' ,dz.LOG_ERROR)
dz.log(item,dz.LOG_DEBUG)
end
end
}