Monthly gas total script virtual device not working.
Posted: Saturday 31 December 2022 14:14
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
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