Hi everyone, I need to create a virtual kWh counter used monthly and read the data from my energy meter. place the photos to better understand, I tried to use this script but it shows me the daily Khw and not the total monthly ones
local httpResponses = "monthTotal"
return {
on = {
timer = { "every 15 minutes" },
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(xxxx) -- Replace xxxx with ID of energyDevice you want to track
monthTotal = dz.devices(yyyy) -- 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].v)
monthTotal = monthTotal + rt[id].v
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
}
djdevil wrote: ↑Monday 09 December 2019 20:08
Hi everyone, I need to create a virtual kWh counter used monthly and read the data from my energy meter. place the photos to better understand, I tried to use this script but it shows me the daily Khw and not the total monthly ones
rl=https://it.imgbb.com/]hosting immagini gratis[/url]
You created a virtual device counter. You should create a virtual device managed counter
djdevil wrote: ↑Monday 09 December 2019 22:12
if you want instead I want to extrapolate the result of the last two months by adding them together?
Please help me understand what you want to see in the target device.
If today is december 9th, do you then want the whole of november plus 1-9 december or do you want october 9th - december 9th. ?
local httpResponses = 'twoMonthTotal'
return {
on = {
timer = { 'on 1/* at 04:17' }, -- if you get data from previous months you only have to do this once a month at a quiet time
httpResponses = { httpResponses .. '*' }
},
logging = {
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = httpResponses
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(xxxx) -- Replace xxxx with ID of energyDevice you want to track
twoMonthTotals = dz.devices(yyyy) -- 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 calculateTwoMonthTotal(rt)
local twoMonthTotal = 0
local dateFmt = '%Y-%m'
monthMinus1 = os.date(dateFmt,os.time{day=1, year=dz.time.year, month=dz.time.month - 1 })
monthMinus2 = os.date(dateFmt,os.time{day=1, year=dz.time.year, month=dz.time.month - 2 })
for id, result in ipairs(rt) do
if result.d:sub(1,7) == monthMinus1 or result.d:sub(1,7) == monthMinus2 then
logWrite(result.d .. ' ==>> ' .. result.v)
twoMonthTotal = twoMonthTotal + result.v
end
end
return twoMonthTotal * 1000
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, 'year')
elseif item.ok then -- statusCode == 2xx
twoMonthTotals.update(0,calculateTwoMonthTotal(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
}
local httpResponses = 'oneMonthTotal'
return {
on = {
{ timer = { "every 5 minutes" }}, -- script draait iedere 5 minuten
-- timer = { 'on 1/* at 04:17' }, -- if you get data from previous months you only have to do this once a month at a quiet time
httpResponses = { httpResponses .. '*' }
},
logging = {
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = httpResponses
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(513) -- Replace xxxx with ID of energyDevice you want to track
oneMonthTotal = dz.devices(1862) -- 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['192.168.0.186:8080'] .. '/json.htm?type=graph&sensor=counter&range=' ..
period .. '&idx=' .. id
dz.openURL({ url = URLString,
method = 'GET',
callback = httpResponses .. '_' .. period}).afterSec(delay)
end
local function calculateoneMonthTotal(rt)
local oneMonthTotal = 0
local dateFmt = '%Y-%m'
monthMinus1 = os.date(dateFmt,os.time{day=1, year=dz.time.year, month=dz.time.month - 1 })
for id, result in ipairs(rt) do
if result.d:sub(1,7) == monthMinus1 then
logWrite(result.d .. ' ==>> ' .. result.v)
oneMonthTotal = oneMonthTotal + result.v
end
end
return oneMonthTotal * 1000
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, 'year')
elseif item.ok then -- statusCode == 2xx
oneMonthTotals.update(0,calculateoneMonthTotal(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
}
RduPre wrote: ↑Sunday 12 January 2020 13:32
I changed the script to 1 month total, see below.
You should not change dz.settings['Domoticz url'] to something like dz.settings['192.168.0.186:8080']
dz.settings is a table and 'Domoticz url' is a key in that table. The value of that element is already your domoticz url.
So what you did here is asking dzVents to get the value of the element with key '192.168.0.186:8080'
local httpResponses = 'oneMonthTotal'
return {
on = {
timer = { 'every 5 minutes' }, -- if you get data from previous months you only have to do this once a month at a quiet time
-- timer = { 'on 1/* at 04:17' }, -- if you get data from previous months you only have to do this once a month at a quiet time
httpResponses = { httpResponses .. '*' }
},
logging = {
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = httpResponses
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(513) -- Replace xxxx with ID of energyDevice you want to track
oneMonthTotal = dz.devices(1862) -- 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 calculateoneMonthTotal(rt)
local oneMonthTotal = 0
local dateFmt = '%Y-%m'
monthMinus1 = os.date(dateFmt,os.time{day=1, year=dz.time.year, month=dz.time.month - 1 })
for id, result in ipairs(rt) do
if result.d:sub(1,7) == monthMinus1 then
logWrite(result.d .. ' ==>> ' .. result.v)
oneMonthTotal = oneMonthTotal + result.v
end
end
return oneMonthTotal * 1000
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, 'year')
elseif item.ok then -- statusCode == 2xx
oneMonthTotal.update(0,calculateoneMonthTotal(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
}
kniazio wrote: ↑Sunday 23 May 2021 0:37
I have such an error
Error: dzVents: local netWork not open for dzVents openURL call !
__________________________________________________________________________________________________________________________
When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents enabled' is checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 (and / or ::1 when using IPv6 ) to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."
___________________________________________________________________________________________________________________________
I chose another meter for counting monthly energy which shows positive values and the script worked immediately. Probably the problem lies in the negative results of the measurement