Page 1 of 1
Extract value from report
Posted: Sunday 10 February 2019 10:03
by pimseb
Hello,
Is there a simple way to extract a value from the report ? :
I'd like to take the value in red (total daily cost) and receive it everyday as a notification.
I'm not sure that it's possible
Thank you
Re: Extract value from report
Posted: Sunday 10 February 2019 15:10
by waaren
pimseb wrote: ↑Sunday 10 February 2019 10:03
Hello,
Is there a simple way to extract a value from the report ? :
This works for my P1 meter.
Code: Select all
-- getDailyUsageReport
local httpResponses = "getDailyUsageReport"
return {
on = {
timer = { "at 06:07" },
httpResponses = { httpResponses }
},
logging = {
level = domoticz.LOG_DEBUG,
marker = httpResponse
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
energyDevice = dz.devices(35) -- Replace with ID of energyDevice you want to track
textDevice = dz.devices(1492) -- Create as virtual text device and change 1492 to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local Time = require('Time')
local function logWrite(str,level)
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local function triggerJSON(energyDevice)
local URLString = dz.settings['Domoticz url'] .. "/json.htm?range=year&sensor=counter&type=graph&idx=" .. energyDevice.id
dz.openURL({ url = URLString,
method = "GET",
callback = httpResponses })
end
local function showResult(str)
textDevice.updateText(str)
logWrite(str,dz.LOG_FORCE)
dz.notify("Yesterday's usage: ", str)
end
local function getResult(rt)
local yesterday = os.date("%Y-%m-%d",os.time()-1*24*60*60)
local dayBeforeYesterday = os.date("%Y-%m-%d",os.time()-2*24*60*60)
local yesterdayTotal, dayBeforeYesterdayTotal
for key in ipairs(rt) do
if rt[key].d == yesterday then
yesterdayTotal = rt[key].c1 + rt[key].c3 - rt[key].c2 - rt[key].c4
elseif rt[key].d == dayBeforeYesterday then
dayBeforeYesterdayTotal = rt[key].c1 + rt[key].c3 - rt[key].c2 - rt[key].c4
end
end
return yesterdayTotal - dayBeforeYesterdayTotal
end
if not item.isHTTPResponse then
triggerJSON(energyDevice)
elseif item.ok then -- statusCode == 2xx
showResult("Usage : ".. dz.utils.round(getResult(item.json.result),4) .. " kwH")
else
logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")" ,dz.LOG_ERROR)
logWrite(item.data)
end
end
}
Re: Extract value from report
Posted: Sunday 10 February 2019 19:48
by pimseb
Thank you waaren but it's not exactly what I'm trying to do
Your script returns an usage in Kwh and in fact I don't know what value it returns because it doesn't match any of the value of my smart P1 meter report

I'd like that the script returns me the last column of my screenshot which is the total daily cost.
We have 2 different pricing periods in France : one for night use (T1) and one for day time use (T2)
Actually my P1 smart meter gives me the T1 counter * T1 price + T2 counter * T2 price. It returns me the total price (13.38 € for example the 1st of february on my screenshot, that's a lot

). I'm trying to have this value being daily notified, like "yesterday you spent xx euros for electricity"
Re: Extract value from report
Posted: Monday 11 February 2019 0:07
by waaren
pimseb wrote: ↑Sunday 10 February 2019 19:48
Thank you waaren but it's not exactly what I'm trying to do
Your script returns an usage in Kwh and in fact I don't know what value it returns because it doesn't match any of the value of my smart P1 meter report
I amended the script to take the costs into account.
Code: Select all
-- getDailyUsageReport
local httpResponses = "getDailyUsageReport"
return {
on = {
timer = { "at 06:07" },
httpResponses = { httpResponses .. "*" }
},
logging = {
level = domoticz.LOG_DEBUG,
marker = httpResponse
},
data = { energyCosts = { initial = {} }},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
energyDevice = dz.devices(35) -- Replace with ID of energyDevice you want to track
textDevice = dz.devices(1492) -- Create as virtual text device and change 1492 to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local Time = require('Time')
local function logWrite(str,level)
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local function triggerJSON(url,delay, response)
dz.openURL({ url = url,
method = "GET",
callback = response }).afterSec(delay)
end
local function showResult(str)
textDevice.updateText(str)
logWrite(str,dz.LOG_FORCE)
dz.notify("Yesterday's ", str)
end
local function setCosts(t)
dz.data.energyCosts.t1 = ( t.CostEnergy or 0 ) / 10000
dz.data.energyCosts.t2 = ( t.CostEnergyT2 or 0 ) / 10000
dz.data.energyCosts.r1 = ( t.CostEnergyR1 or 0 ) / 10000
dz.data.energyCosts.r2 = ( t.CostEnergyR2 or 0 ) / 10000
end
local function getResult(rt)
local yesterday = os.date("%Y-%m-%d",os.time()-1*24*60*60)
local dayBeforeYesterday = os.date("%Y-%m-%d",os.time()-2*24*60*60)
local yesterdayTotal, dayBeforeYesterdayTotal
for key in ipairs(rt) do
if rt[key].d == yesterday then
yesterdayTotal = rt[key].c1 * dz.data.energyCosts.t1 + rt[key].c3 * dz.data.energyCosts.t2 - rt[key].c2 * dz.data.energyCosts.r1 - rt[key].c4 * dz.data.energyCosts.r2
elseif rt[key].d == dayBeforeYesterday then
dayBeforeYesterdayTotal = rt[key].c1 * dz.data.energyCosts.t1 + rt[key].c3 * dz.data.energyCosts.t2 - rt[key].c2 * dz.data.energyCosts.r1 - rt[key].c4 * dz.data.energyCosts.r2
end
end
return yesterdayTotal - dayBeforeYesterdayTotal
end
if not item.isHTTPResponse then
if dz.data.energyCosts.T1 == nil or day == 1 then
triggerJSON(dz.settings['Domoticz url'] .. "/json.htm?param=getcosts&type=command&idx=" .. energyDevice.id,0 , httpResponses .. "_cost")
end
triggerJSON(dz.settings['Domoticz url'] .. "/json.htm?range=year&sensor=counter&type=graph&idx=" .. energyDevice.id,10, httpResponses .. "_graph")
elseif item.ok then -- statusCode == 2xx
if item.trigger == httpResponses .. "_cost" then
setCosts(item.json)
else
showResult("elektricity Costs: ".. dz.utils.round(getResult(item.json.result),2) .. " Euro" )
end
else
logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")" ,dz.LOG_ERROR)
logWrite(item.data)
end
end
}
Re: Extract value from report
Posted: Monday 11 February 2019 9:13
by pimseb
Hi,
The script works (no error in log file) and notification aswell but I receive this notification :
Status: dzVents: !Info: elektricity Costs: 11.25 Euro
Unfortunately it's not the yesterday cost shown in the report (9.67 € - 10 february)

Re: Extract value from report
Posted: Monday 11 February 2019 10:07
by waaren
What the script does is calculate the total cost of the total usage until yesterday and -the total cost of the total usage of the usage until today and subtract these two values.
The total usages / returns are extracted from the P1 counters and the costs do come from the domoticz settings.
If you can post the outcome of these two JSON/ API calls (only the relevant days will do) I can maybe see where the difference between the GUI and the script come from.
Code: Select all
http://domoticz_IP:domotic_port/json.htm?range=year&sensor=counter&type=graph&idx=<your energy device>
http://domoticz_IP:domotic_port/json.htm?param=getcosts&type=command&idx=<your energy device>
Re: Extract value from report
Posted: Monday 11 February 2019 10:17
by pimseb
json.htm?range=year&sensor=counter&type=graph&idx=7
Just parsing the last lines :
Code: Select all
{
"c1" : "153.969",
"c2" : "0",
"c3" : "5569.362",
"c4" : "0",
"d" : "2019-02-09",
"r1" : "0.000",
"r2" : "0.000",
"v" : "26.169",
"v2" : "58.232"
},
{
"c1" : "180.503",
"c2" : "0",
"c3" : "5627.594",
"c4" : "0",
"d" : "2019-02-10",
"r1" : "0.000",
"r2" : "0.000",
"v" : "25.071",
"v2" : "48.292"
},
{
"d" : "2019-02-11",
"r1" : "0.000",
"r2" : "0.000",
"v" : "16.165",
"v2" : "12.377"
}
],
"status" : "OK",
"title" : "Graph counter year"
}
json.htm?param=getcosts&type=command&idx=7 :
Code: Select all
{
"CostEnergy" : 1102,
"CostEnergyR1" : 800,
"CostEnergyR2" : 800,
"CostEnergyT2" : 1430,
"CostGas" : 6218,
"CostWater" : 16473,
"CounterR1" : "0.000",
"CounterR2" : "0.000",
"CounterT1" : "222.104",
"CounterT2" : "5688.327",
"DividerWater" : 100.0,
"status" : "OK",
"title" : "GetElectraCosts"
}
Re: Extract value from report
Posted: Monday 11 February 2019 12:05
by waaren
pimseb wrote: ↑Monday 11 February 2019 10:17
json.htm?range=year&sensor=counter&type=graph&idx=7
Just parsing the last lines :
If you do the math you will see that the script reports the correct cost. The GUI shows the costs for a day earlier. If you want the script to report that (in my opinion wrong) cost you can change lines 49 and 50 of attached script
from
Code: Select all
local yesterday = os.date("%Y-%m-%d",os.time()-1*24*60*60)
local dayBeforeYesterday = os.date("%Y-%m-%d",os.time()-2*24*60*60)
to
Code: Select all
local yesterday = os.date("%Y-%m-%d",os.time()-2*24*60*60)
local dayBeforeYesterday = os.date("%Y-%m-%d",os.time()-3*24*60*60)
Below script has some minor changes and will produce some extra loglines
Code: Select all
-- getDailyUsageReport
local httpResponses = "getDailyUsageReport"
return {
on = {
devices = { "XButton-2" },
timer = { "at 06:07" },
httpResponses = { httpResponses .. "*" }
},
logging = {
level = domoticz.LOG_DEBUG,
marker = httpResponse
},
data = { energyCosts = { initial = {} }},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
energyDevice = dz.devices(35) -- Replace with ID of energyDevice you want to track
textDevice = dz.devices(1492) -- Create as virtual text device and change 1492 to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local Time = require('Time')
local function logWrite(str,level)
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local function triggerJSON(url,delay, response)
dz.openURL({ url = url,
method = "GET",
callback = response }).afterSec(delay)
end
local function showResult(str)
textDevice.updateText(str)
logWrite(str,dz.LOG_FORCE)
dz.notify("Yesterday's ", str)
end
local function setCosts(t)
dz.data.energyCosts.c1 = ( t.CostEnergy or 0 ) / 10000
dz.data.energyCosts.c3 = ( t.CostEnergyT2 or 0 ) / 10000
dz.data.energyCosts.c2 = ( t.CostEnergyR1 or 0 ) / 10000
dz.data.energyCosts.c4 = ( t.CostEnergyR2 or 0 ) / 10000
end
local function getResult(rt)
local yesterday = os.date("%Y-%m-%d",os.time()-1*24*60*60)
local dayBeforeYesterday = os.date("%Y-%m-%d",os.time()-2*24*60*60)
local longNames = {
c1 = "Low tariff consumed",
c2 = "High tariff delivered",
c3 = "High tariff consumed",
c4 = "Low tariff delivered"
}
local yesterdayTotal, dayBeforeYesterdayTotal, yesterdayKey, dayBeforeYesterdayKey
for key in ipairs(rt) do
if rt[key].d == yesterday then
yesterdayKey = key
for meter, total in pairs(rt[key]) do
if meter:sub(1,1) == "c" then
logWrite(rt[key].d .. ": " .. longNames[meter] ..": Value ==>> " .. total .. " cost = " .. dz.utils.round((tonumber(total) or 0) * (dz.data.energyCosts[meter] or 1),2))
end
end
yesterdayTotal = rt[key].c1 * dz.data.energyCosts.c1 + rt[key].c3 * dz.data.energyCosts.c3 - rt[key].c2 * dz.data.energyCosts.c2 - rt[key].c4 * dz.data.energyCosts.c4
elseif rt[key].d == dayBeforeYesterday then
dayBeforeYesterdayKey = key
for meter, total in pairs(rt[key]) do
if meter:sub(1,1) == "c" then
logWrite(rt[key].d .. ": " .. longNames[meter] ..": Value ==>> " .. total .. " cost = " .. dz.utils.round((tonumber(total) or 0) * (dz.data.energyCosts[meter] or 1),2))
end
end
dayBeforeYesterdayTotal = rt[key].c1 * dz.data.energyCosts.c1 + rt[key].c3 * dz.data.energyCosts.c3 - rt[key].c2 * dz.data.energyCosts.c2 - rt[key].c4 * dz.data.energyCosts.c4
end
end
logWrite( "\n\n\t\t\t subtotals Costs: " .. longNames.c1 .. ": " .. dz.utils.round(rt[yesterdayKey].c1 * dz.data.energyCosts.c1 - rt[dayBeforeYesterdayKey].c1 * dz.data.energyCosts.c1,2) .. " Euro" ..
"\n\t\t\t subtotals Costs: " .. longNames.c2 .. ": " .. dz.utils.round(rt[yesterdayKey].c2 * dz.data.energyCosts.c2 - rt[dayBeforeYesterdayKey].c2 * dz.data.energyCosts.c2,2).. " Euro" ..
"\n\t\t\t subtotals Costs: " .. longNames.c3 .. ": " .. dz.utils.round(rt[yesterdayKey].c3 * dz.data.energyCosts.c3 - rt[dayBeforeYesterdayKey].c3 * dz.data.energyCosts.c3,2) .. " Euro" ..
"\n\t\t\t subtotals Costs: " .. longNames.c4 .. ": " .. dz.utils.round(rt[yesterdayKey].c4 * dz.data.energyCosts.c4 - rt[dayBeforeYesterdayKey].c4 * dz.data.energyCosts.c4,2) .. " Euro\n" )
return yesterdayTotal - dayBeforeYesterdayTotal
end
if not item.isHTTPResponse then
local delay = 0
if dz.data.energyCosts.c1 == nil or day == 1 then
triggerJSON(dz.settings['Domoticz url'] .. "/json.htm?param=getcosts&type=command&idx=" .. energyDevice.id,0 , httpResponses .. "_cost")
delay = 10
end
triggerJSON(dz.settings['Domoticz url'] .. "/json.htm?range=year&sensor=counter&type=graph&idx=" .. energyDevice.id,delay, httpResponses .. "_graph")
elseif item.ok then -- statusCode == 2xx
if item.trigger == httpResponses .. "_cost" then
setCosts(item.json)
else
showResult("elektricity Costs: ".. dz.utils.round(getResult(item.json.result),2) .. " Euro" )
end
else
logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")" ,dz.LOG_ERROR)
logWrite(item.data)
end
end
}
Re: Extract value from report
Posted: Monday 11 February 2019 14:18
by pimseb
If I put for line 49 and 50 :
Code: Select all
local yesterday = os.date("%Y-%m-%d",os.time()-2*24*60*60)
local dayBeforeYesterday = os.date("%Y-%m-%d",os.time()-3*24*60*60)
your script returns me 9.68 € which is almost the same than what I see in the gui screenshot (Dimanche = Sunday = Yesterday = 9.67 €)
So why do you say the report is wrong in your opinion ?
Re: Extract value from report
Posted: Monday 11 February 2019 14:35
by waaren
Because it's not yesterdays cost but cost of the day before yesterday.