Page 1 of 1

Energie-Usage last year

Posted: Wednesday 02 November 2022 15:25
by SlaXeon
Dear all,

I have a problem and maybe someone can help me.
Spoiler: show
local httpResponses = "energyYearTotal"

return {
on = {
timer = { "at 15:13" },
httpResponses = { "setSolarInVar1y" }
},

logging = {
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = "setSolarInVar1y" },

execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(255) -- Replace xxxx with ID of energyDevice you want to track
yearTotal = dz.devices(319) -- 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(idx, period, delay)
local URLString = "http://192.168.10.47:8080" .. "/json.htm?type=graph&sensor=counter&range=" .. period .. "&idx=" .. usageDevice.idx
dz.openURL({ url = URLString,
method = "GET",
callback = "setSolarInVar1y" })
end

local function calculateYearTotal(rt)
local yearTotal = 0
local currentYear = dz.time.rawDate:sub(1,4)
for i=1,#rt do
--if rt[id].d:sub(1,4) == currentYear then
logWrite(rt.d .. " ==>> " .. rt.u_max)
yearTotal = yearTotal + rt.u_max
--end
end
return yearTotal / 1000
end

if not item.isHTTPResponse then
triggerJSON(usageDevice.idx, "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
}


I am using the code above to calculate the total power consumption for the last year. When I am calling the URL manually in the browser, I get 365 items as expected from the URL, but unfortunately when Domoticz itself is running the URL I get only 97,98 or 99 item (this is random changing).

Has someone an Idea how to enlarge the amount of items I will get back from Domotizc. Maybe I can add something to the URL to give the feedback some more time for running.

Enjoy your Day
Best regards
Daniel

Re: Energie-Usage last year

Posted: Wednesday 02 November 2022 17:28
by waltervl
to debug you can log the URLString variable to see if it differs from the manual entered URL.

Edit: could also be that the 365 items list has a incorrect character that cause to shorten the list in DzVents translation from json to table.

Re: Energie-Usage last year

Posted: Thursday 03 November 2022 11:47
by SlaXeon
Hi,

thank you for the reply.

I have debug the URL and everything seems good. The problem is, that the Amout of Items I get back is randomly changing, sometime 97 items, sometime 98 or 99 items. I have checked the list and this is correct. I can only believe, that the timing wrong.

Best regards
Daniel

Re: Energie-Usage last year

Posted: Thursday 03 November 2022 13:02
by jvdz
I am not a expert in DzVents, just use basic LUA myself, but shouldn't this it bit :

Code: Select all

		if not item.isHTTPResponse then
			triggerJSON(usageDevice.idx, "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
look like this:?

Code: Select all

		if item.isTimer then
			triggerJSON(usageDevice.idx, "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
To avoid multiple triggers and log errors when they occur?

EDIT:Is the u_max field always there in each record? I don't think i have any Energy device that contains that field so can't check.
When it isn't always present you could try changing these lines to avoid any hard LUA NIL errors to:

Code: Select all

				logWrite(rt.d .. " ==>> " .. (rt.u_max or 0))
				yearTotal = yearTotal + (rt.u_max or 0)

Re: Energie-Usage last year

Posted: Thursday 03 November 2022 14:15
by SlaXeon
Hi,

unfortunately each value has a u_max position and with isTimer the script is not working any more :(

Thank you for trying to help me, but the tips do not work :(

Re: Energie-Usage last year

Posted: Thursday 03 November 2022 19:27
by jvdz
Think that it should fire at the time defined in: timer = { "at 15:13" },
That is how the example looks in the Wiki.