Page 1 of 1
Total count for past x days
Posted: Tuesday 13 July 2021 15:47
by ccaru
Hello everybody,
I am trying to build a DZVents script that will activate my watering system. I'd like to be able to calculate the total rain for the past x days and then determine the watering needs based on that.
Is it possible to return the total value of a counter (or rain meter) for the past x days ?
cheers,
Claude
Re: Total count for past x days
Posted: Tuesday 13 July 2021 16:14
by waltervl
In the following topic lots of examples how to extract data out of in this case energy counters (gas and electricity). Should be adaptable for rain counters.
https://domoticz.com/forum/viewtopic.ph ... 4&start=20
Re: Total count for past x days
Posted: Friday 13 August 2021 12:06
by ccaru
Hi, thank you very much for link. I have successfully modified the script to calculate the total rain for the past 7 days, which is then stored in a dz variable.
I use this value to control how much water I supply to my young trees. I am not sure how effective it is but the trees are looking healthy
Code: Select all
local scriptVar = 'Rain this week'
return
{
on =
{
timer =
{
'every 15 minutes',
},
httpResponses =
{
scriptVar .. '*',
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = scriptVar,
},
data =
{
Rain =
{
initial = {},
},
},
execute = function(dz, item)
-- ***** Your settings below this line
local Rain = dz.devices('Rain') -- change to name of your RainMeter between quotes or id without quotes
local AverageRain = dz.variables('AverageRain') -- change to name of Rain variable
-- ***** No changes required below this line
local function getGraphData(period, id)
if period == nil then period = 'year' end
if id == nil then id = Rain.id end
url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=rain&range=' .. period .. '&idx=' .. id
dz.openURL({ url = url, callback = scriptVar .. '_' .. period })
end
local function processJSON(t)
local weekVolume = 0, 0, 0
local day = 86400 -- (24 * 60 * 60)
local startDay = os.date("%Y-%m-%d",os.time()-(7*day)) --Past 7 days
for index, Rain in ipairs(t) do
if Rain.d:match(dz.time.year) then
if Rain.d >= startDay then -- will sum everything which is on or after startofWeek
weekVolume = weekVolume + Rain.mm
end
end
end
AverageRain.set(weekVolume)
end
-- main
if item.isHTTPResponse and item.isJSON then
processJSON(item.json.result)
elseif item.isTimer or item.isDevice then
getGraphData()
else
dz.log('Error while retrieving Rain data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
end
end
}