monthly energy usage and yearly energy usage
Both give me the same outcomes
Device IDX's are correct
Who has a minute or two to look these over with me ?
the monthly script:
Code: Select all
local httpResponses = "energyMonthTotal"
return {
on = {
timer = { "every 15 minutes" },
httpResponses = { httpResponses .. "*" }
},
logging = {
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = httpResponses
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(69) -- Replace xxxx with ID of energyDevice you want to track
monthTotal = dz.devices(133) -- 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 calculateMonthTotal(rt)
local monthTotal = 0
local currentMonth = dz.time.rawDate:sub(1,7)
for id, result in ipairs(rt) do
if rt[id].d:sub(1,7) == currentMonth then
logWrite(rt[id].d .. " ==>> " .. rt[id].v)
monthTotal = monthTotal + rt[id].v
end
end
return monthTotal * 1000
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, "month")
elseif item.ok then -- statusCode == 2xx
monthTotal.update(0,calculateMonthTotal(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
}
Code: Select all
local httpResponses = "energyYearTotal"
return {
on = {
timer = { "every 15 minutes" },
httpResponses = { httpResponses .. "*" }
},
logging = {
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = httpResponses
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(69) -- Replace xxxx with ID of energyDevice you want to track
yearTotal = dz.devices(134) -- 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].v)
yearTotal = yearTotal + rt[id].v
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
}
- Spoiler: show
And they don't give me the correct monthly values too
I'm lost here.. who has the definitive answer ... Waaren ?
