Energie-Usage last year

Moderator: leecollings

Post Reply
SlaXeon
Posts: 3
Joined: Wednesday 13 December 2017 15:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Energie-Usage last year

Post 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
User avatar
waltervl
Posts: 5906
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Energie-Usage last year

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
SlaXeon
Posts: 3
Joined: Wednesday 13 December 2017 15:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Energie-Usage last year

Post 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
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Energie-Usage last year

Post 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)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
SlaXeon
Posts: 3
Joined: Wednesday 13 December 2017 15:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Energie-Usage last year

Post 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 :(
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Energie-Usage last year

Post 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.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest