Just a tip for future users, It costed me 4 nights to find out... the r1 and r2 in the code are return1 and return2... In the original code it said v and v2 which are the numbers for the power consumption high and low...
NOTE! This is the ACTUAL returned value, not what you used right away but what went through the smart meter and is deducted from your total monthly consumption (at least in the Netherlands it works like that for how long it will last)
The return value will look like this:
Code: Select all
local httpResponses = "monthTotal"
return {
on = {
timer = { "every 1 minutes" },
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 ***************************************************
usageDelivered = dz.devices(1) -- Replace 1 with ID of energyDevice you want to track
monthTotal = dz.devices(65) -- Create as virtual managed counter (energy) and change 65 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].r2)
monthTotal = monthTotal + rt[id].r2 + rt[id].r1
end
end
return ( ( monthTotal ) * 1000 )
end
if not item.isHTTPResponse then
triggerJSON(usageDelivered.id, "month")
elseif item.ok then -- statusCode == 2xx
monthTotal.updateCounter(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
}