Monthly gas total script virtual device not working.

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
hemant5400z
Posts: 114
Joined: Monday 05 November 2018 17:41
Target OS: Linux
Domoticz version:
Contact:

Monthly gas total script virtual device not working.

Post by hemant5400z »

Hi All,

I get my gas from my P1 to make it easy I created avirtual deivice for a month counter to fill the month totals.
For some reason the P1 smart meter Gas and the virtual counter are not in sync.

local httpResponses = "monthTotalGas"

return {
on = {
timer = { "every hour" },
--devices = {74},
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(74) -- Replace xxxx with ID of energyDevice you want to track
monthTotal = dz.devices(202) -- 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 = "http://192.168.1.7:8080/json.htm?type=g ... ter&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 * 100
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
}



When I look at P1 Gas IDX 74 for December it shows 95.95 m3 however my virtual device idx 202 managed counter shows 92.690 M3, as you can see above script runs every hour. But not sure why I have different values.

Anyone knows where i'm going wrong?

Thanx,
Hemant
hemant5400z
Posts: 114
Joined: Monday 05 November 2018 17:41
Target OS: Linux
Domoticz version:
Contact:

Re: Monthly gas total script virtual device not working.

Post by hemant5400z »

I just noticed when I do a query to the DB:

SELECT round(sum(value),5)/1000 as total, strftime('%Y-%m', date) year_month
from Meter_calendar where devicerowid=74 and strftime('%Y',date) = strftime('%Y',date('now'))
GROUP BY year_month
ORDER BY year_month ASC


I can see the 92.6 value, but looks like de P1 graph for is is doing a different calculation?

Hemant
willemd
Posts: 741
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Monthly gas total script virtual device not working.

Post by willemd »

This is probably caused by the bug in the daily closing that I reported some time ago and affects all devices in the meter tables and the multimeter tables. Have a look here https://github.com/domoticz/domoticz/issues/5437

The daily closing takes the values from the (multi)meter table and stores the daily cutover values in the (multi)meter_calendar table.
The calendar tables contains fields for the daily change and the end-of-day counter and you would expect, if you take the last counter minus the previous counter, that should be equal to the change of the day value...... but this is not the case.

Very often a small value around midnight is missing from the value field. I run a daily python script to correct the values fields as long as the bug is not solved.

So the sum of the values fields is different from the value you get by subtracting counters.
hemant5400z
Posts: 114
Joined: Monday 05 November 2018 17:41
Target OS: Linux
Domoticz version:
Contact:

Re: Monthly gas total script virtual device not working.

Post by hemant5400z »

Thanx that explains because i noticed i missed 13th of december with screws the virtual devices.

What i actually want is simple i want to show the monthly total which shows correct value in a virtual switch. So just take the total value from the anual totals frim current month. Not sure how to achieve.

Hemant
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest