I have a script pulling the monthly readings from my P1 meter and updating a managed counter to reflect the kWh delivery of my solar panels. I thought it worked, but it seems to fetch only 0 amounts while there are tonnes (well few kWh's) generated. Where am I thinking wrong? r1 and r2 should be the values to fetch?
Script
- Spoiler: show
Code: Select all
-- Hier de managed counter vullen, die komt niet op het dashboard -- Per week/maand/jaar local httpResponses = "monthTotal" return { on = { timer = { "every 1 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 monthTotal = dz.devices(534) -- 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].r1) monthTotal = monthTotal + rt[id].r1 end end 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 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 }
- Spoiler: show
Code: Select all
2021-02-02 22:32:00.558 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua 2021-02-02 22:32:00.670 Status: dzVents: Info: Handling httpResponse-events for: "monthTotal_month" 2021-02-02 22:32:00.703 Status: dzVents: Info: ------ Start internal script: 034StroomPanelenMaand: HTTPResponse: "monthTotal_month" 2021-02-02 22:32:00.719 Status: dzVents: Debug: Processing device-adapter for StroomPanelenMaand: Counter device adapter 2021-02-02 22:32:00.720 Status: dzVents: Debug: 2021-02-01 ==>> 0.000 2021-02-02 22:32:00.720 Status: dzVents: Debug: 2021-02-02 ==>> 0.000 2021-02-02 22:32:00.720 Status: dzVents: Debug: 2021-02-01 ==>> 0.000 2021-02-02 22:32:00.720 Status: dzVents: Debug: 2021-02-02 ==>> 0.000 2021-02-02 22:32:00.720 Status: dzVents: Info: ------ Finished 034StroomPanelenMaand