So now that all is finally scripted (for me as a non-programmer quite a challenge, but doable) using dzvents, there is only one (for now ) question left.
I have the year totals of my energy consumption and solar panels, but I'd like to know the totals for the previous year. They are in the P1 meter as a json call spits them on screen. Google and the forum did not provide me with an answer, or the question was incorrect. Could be.
Below is what I use to get the data for this year. The previous year shouldn't be that hard to code, but how?? I hijacked waarens code a little bit
local function calculateYearTotal(rt)
local yearTotal = 0
local currentYear = dz.time.rawDate:sub(1,7)
for id, result in ipairs(rt) do
if rt[id].d:sub(1,7) == currentYear then
logWrite(rt[id].d .. " ==>> " .. rt[id].r1)
yearTotal = yearTotal + rt[id].r1
end
end
for id, result in ipairs(rt) do
if rt[id].d:sub(1,7) == currentYear then
logWrite(rt[id].d .. " ==>> " .. rt[id].r2)
yearTotal = yearTotal + rt[id].r2
end
end
return yearTotal * 1000
end
Kranendijk wrote: ↑Thursday 28 January 2021 21:00
Below is what I use to get the data for this year. The previous year shouldn't be that hard to code, but how?? I hijacked waarens code a little bit
It will make it much less error prone if you would share the complete script but in a nutshell is the previous year the current year - 1. Or in dzVents code:
-- Hier de managed counter vullen, die komt niet op het dashboard
-- Per week/maand/jaar
local httpResponses = "yearTotal"
return {
on = {
timer = { "every 8 hours" },
httpResponses = { httpResponses .. "*" }
},
logging = {
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = httpResponse
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(1) -- Replace xxxx with ID of energyDevice you want to track
yearTotal = dz.devices(536) -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local function logWrite(str,level)
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local function triggerJSON(id, period, delay)
local delay = delay or 0
local URLString = dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=counter&range=" ..
period .. "&idx=" .. id
dz.openURL({ url = URLString,
method = "GET",
callback = httpResponses .. "_" .. period}).afterSec(delay)
end
local function calculateYearTotal(rt)
local yearTotal = 0
local currentYear = dz.time.rawDate:sub(1,7)
for id, result in ipairs(rt) do
if rt[id].d:sub(1,7) == currentYear then
logWrite(rt[id].d .. " ==>> " .. rt[id].r1)
yearTotal = yearTotal + rt[id].r1
end
end
for id, result in ipairs(rt) do
if rt[id].d:sub(1,7) == currentYear then
logWrite(rt[id].d .. " ==>> " .. rt[id].r2)
yearTotal = yearTotal + rt[id].r2
end
end
return yearTotal * 1000
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, "year")
elseif item.ok then -- statusCode == 2xx
yearTotal.update(0,calculateYearTotal(item.json.result))
else
logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")" ,dz.LOG_ERROR)
logWrite(item.data)
end
end
}
Kranendijk wrote: ↑Thursday 28 January 2021 21:35
Ok, better to share the complete code then, that obviously makes more sense!
below script does collect the data of current- and last year for returned energy as collected from the history of a P1 meter and updates two managed counters with the year sums.
-- Hier de managed counter vullen, die komt niet op het dashboard
-- Per week/maand/jaar
local scriptVar = 'yearTotal'
return
{
on =
{
timer =
{
'every 8 hours',
},
httpResponses =
{
scriptVar .. '*',
}
},
logging =
{
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = scriptVar,
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(35) -- Replace with ID of P1 device you want to track
currentYear = dz.devices(3487) -- Create as virtual managed counter (energy returned) and change to the ID of the new device
lastYear = dz.devices(3488) -- Create as virtual managed (energy returned) and change to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local function triggerJSON(id, period, delay)
local URLString = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
dz.openURL(
{
url = URLString,
callback = scriptVar .. '_' .. period
}).afterSec(delay or 0)
end
local function calculateYearsTotal(rt)
local totals = {}
for id, result in ipairs(rt) do
dz.log(rt[id].d .. ' ==>> ' .. ( rt[id].r1 + rt[id].r2 ), dz.LOG_DEBUG)
totals[rt[id].d:sub(1,4)] = ( totals[rt[id].d:sub(1,4)] or 0 ) + rt[id].r1 + rt[id].r2
end
return totals
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, 'year')
elseif item.ok then -- statusCode == 2xx
local totals = calculateYearsTotal(item.json.result)
dz.utils.dumpTable(totals)
currentYear.updateCounter(totals[tostring(dz.time.year)] * 1000 )
lastYear.updateCounter(totals[tostring(dz.time.year - 1)] * 1000 )
else
dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')' ,dz.LOG_ERROR)
dz.log(item,dz.LOG_DEBUG)
end
end
}
Helps a lot, however I notice the counter for previousyear does not take into account all values for the previous year.
I created both managed counters (idx 562 and 563) to check usageDevice idx 1.
In the logfile I see the below lines when running the script, did I misconfigure? The script helps me for other scripts as well to make them more efficient, great!
-- Hier de managed counter vullen, die komt niet op het dashboard
-- Per week/maand/jaar
local scriptVar = 'yearTotal'
return
{
on =
{
timer =
{
'every 1 minutes',
},
httpResponses =
{
scriptVar .. '*',
}
},
logging =
{
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = scriptVar,
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(1) -- Replace with ID of P1 device you want to track
currentYear = dz.devices(562) -- Create as virtual managed counter (energy returned) and change to the ID of the new device
lastYear = dz.devices(563) -- Create as virtual managed (energy returned) and change to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local function triggerJSON(id, period, delay)
local URLString = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
dz.openURL(
{
url = URLString,
callback = scriptVar .. '_' .. period
}).afterSec(delay or 0)
end
local function calculateYearsTotal(rt)
local totals = {}
for id, result in ipairs(rt) do
dz.log(rt[id].d .. ' ==>> ' .. ( rt[id].r1 + rt[id].r2 ), dz.LOG_DEBUG)
totals[rt[id].d:sub(1,4)] = ( totals[rt[id].d:sub(1,4)] or 0 ) + rt[id].r1 + rt[id].r2
end
return totals
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, 'year')
elseif item.ok then -- statusCode == 2xx
local totals = calculateYearsTotal(item.json.result)
dz.utils.dumpTable(totals)
currentYear.updateCounter(totals[tostring(dz.time.year)] * 1000 )
lastYear.updateCounter(totals[tostring(dz.time.year - 1)] * 1000 )
else
dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')' ,dz.LOG_ERROR)
dz.log(item,dz.LOG_DEBUG)
end
end
}
json gives me output for the entire year, starting 2020-01-24 when the solar panels were placed.
Kranendijk wrote: ↑Friday 29 January 2021 13:09
Helps a lot, however I notice the counter for previousyear does not take into account all values for the previous year.
Looking at the log and the totals, I guess that the totals are about right but that the number of lines in a short time do flood the log function causing your display to skip most loglines.
I added some lines and a function to consolidate the loglines in months to limit the number of loglines (only when in debug mode)
-- Hier de managed counter vullen, die komt niet op het dashboard
-- Per week/maand/jaar
local scriptVar = 'yearTotal'
return
{
on =
{
timer =
{
'every minute',
},
httpResponses =
{
scriptVar .. '*',
}
},
logging =
{
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = scriptVar,
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(1) -- Replace with ID of P1 device you want to track
currentYear = dz.devices(562) -- Create as virtual managed counter (energy returned) and change to the ID of the new device
lastYear = dz.devices(563) -- Create as virtual managed (energy returned) and change to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local debug = _G.logLevel == dz.LOG_DEBUG
local function dumpSorted(t)
local st = {}
for key, _ in pairs(t) do
table.insert(st, key)
end
table.sort(st)
for _, key in ipairs(st) do
dz.log(key .. ': ' ..t[key],dz.LOG_DEBUG)
end
end
local function triggerJSON(id, period, delay)
local URLString = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
dz.openURL(
{
url = URLString,
callback = scriptVar .. '_' .. period
}).afterSec(delay or 0)
end
local function calculateYearsTotal(rt)
local totals = {}
for id, result in ipairs(rt) do
if debug then
totals[rt[id].d:sub(1,7)] = ( totals[rt[id].d:sub(1,7)] or 0 ) + rt[id].r1 + rt[id].r2
end
totals[rt[id].d:sub(1,4)] = ( totals[rt[id].d:sub(1,4)] or 0 ) + rt[id].r1 + rt[id].r2
end
return totals
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, 'year')
elseif item.ok then -- statusCode == 2xx
local totals = calculateYearsTotal(item.json.result)
if debug then dumpSorted(totals) end
currentYear.updateCounter(totals[tostring(dz.time.year)] * 1000 )
lastYear.updateCounter(totals[tostring(dz.time.year - 1)] * 1000 )
else
dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')' ,dz.LOG_ERROR)
dz.log(item,dz.LOG_DEBUG)
end
end
}