
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'
Moderator: leecollings
Yes, that is possible with both classic Lua (via an OS curl) command and with dzVents openURL().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'
Code: Select all
/json.htm?type=graph&sensor=temp&idx=IDX&range=day
You cannot get the individual datapoints from the API. You collect all and select what's needed in your script .
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:)2.how to use it in Lua? for example if i want temp of 2020-10-27 10:00
that's very good. i would be thankfulMost 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.
Example data retrieval (temperature sensor) from short- and long logabdolhamednik wrote: ↑Thursday 29 October 2020 9:10that's very good. i would be thankfulMost 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.
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
}
Users browsing this forum: Bing [Bot] and 1 guest