Page 1 of 1
get a counter value of a specific date
Posted: Monday 02 December 2019 15:51
by steef27
Hi guys,
I need your help.
I would like to retrieve the gas usage over the past 365 days or the position of the gas meter on a specific date.
With the url below I get the counter values of each (!) day.
/json.htm?type=graph&sensor=counter&idx=IDX&range=year
Is it possible to insert a date parameter?
gtz
Re: get a counter value of a specific date
Posted: Monday 02 December 2019 16:00
by waaren
steef27 wrote: ↑Monday 02 December 2019 15:51
I would like to retrieve the gas usage over the past 365 days or the position of the gas meter on a specific date.
Is it possible to insert a date parameter?
This is not possible in the API call itself but quite easy to get it with dzVents. How do you want to use this ?
Re: get a counter value of a specific date
Posted: Monday 09 December 2019 9:26
by steef27
I want to calculate the annual usage by comparing the current position with the position 365 days ago. Every day.
Re: get a counter value of a specific date
Posted: Monday 09 December 2019 14:14
by waaren
steef27 wrote: ↑Monday 09 December 2019 9:26
I want to calculate the annual usage by comparing the current position with the position 365 days ago. Every day.
Can you try this one ?
Code: Select all
--[[
this dzVents script collects the current energy return / usage and gas usage to enable comparing with Same date last year figure
]]--
local scriptVar = 'gasHistory'
return
{
on =
{
timer = { 'every 5 minutes' },
httpResponses = { scriptVar },
},
logging = { level = domoticz.LOG_DEBUG },
execute = function(dz, item)
-- ***** Your settings below this line
local gas = dz.devices('Gas') -- change to name of your gasMeter between quotes or id without quotes
local gasText = dz.devices('gasText') -- text device showing gas Consumption
-- ***** No changes required below this line
_G.logMarker = _G.moduleLabel
local day = 86400 -- (24 * 60 * 60)
local function callYearGraph(id)
url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. id
dz.openURL({ url = url, callback = scriptVar })
end
local function daysInLastYear()
local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
return math.floor(os.difftime(os.time(), lastYear) / day )
end
local function processResult(t)
local gastToday,gasLastYear, gasNow, gasYear = 0, 0, 0 ,0
local dateFmt = "%Y-%m-%d"
local today = os.date(dateFmt,os.time())
local yesterday = os.date(dateFmt,os.time() - day)
local sameDayLastYear = os.date(dateFmt,os.time() - daysInLastYear() * day)
for index, gas in ipairs(t) do
if gas.d == today then gasToday = gas.v gasNow = gas.c
elseif gas.d == yesterday then gasYesterDay = gas.v
elseif gas.d == sameDayLastYear then gasLastYear = gas.v gasYear = gas.c
end
end
dz.log('gasToday: ' .. gasToday .. ' M³',LOG_DEBUG)
dz.log('gasYesterDay: ' .. gasYesterDay .. ' M³',LOG_DEBUG)
dz.log('gasLastYear: ' .. gasLastYear .. ' M³',LOG_DEBUG)
dz.log('gasYearConsumption: ' .. (gasNow - gasYear) .. ' M³',LOG_DEBUG)
return gasToday, gasYesterDay, gasLastYear, (gasNow - gasYear)
end
local function updateTextSensor(gasToday, gasYesterDay, gasLastYear, gasTotal)
gasText.updateText
( 'Gas used today: ' .. gasToday .. ' M³ \n' ..
'Gas used yesterday: ' .. gasYesterDay .. ' M³ \n' ..
'Gas used same day last Year: ' .. gasLastYear .. ' M³ \n' ..
'Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³'
)
end
-- main
if item.isHTTPResponse and item.isJSON then
updateTextSensor(processResult(item.json.result))
elseif item.isTimer then
callYearGraph(gas.id)
else
dz.log('Error while retrieving gas data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
end
end
}
Re: get a counter value of a specific date
Posted: Thursday 19 December 2019 16:36
by MadMedic
waaren wrote: ↑Monday 09 December 2019 14:14
Can you try this one ?
Hey Waaren, perfect dz vents script...
can I alter this for Energy (Power) consumption too ? Since power has 4 or 6 different values,
instead of gas which has only one, as far as I know
please let me know
Re: get a counter value of a specific date
Posted: Thursday 19 December 2019 18:21
by waaren
MadMedic wrote: ↑Thursday 19 December 2019 16:36
can I alter this for Energy (Power) consumption too ? Since power has 4 or 6 different values,
Should be possible to do that but you will have to decide what you want to compare. Below you see the result for one day.
v + v2 is usage , r1 + r2 is return
Code: Select all
{
"c1" : "6560.563",
"c2" : "2941.709",
"c3" : "4764.800",
"c4" : "6741.620",
"d" : "2018-12-19",
"r1" : "0.000",
"r2" : "0.531",
"v" : "3.964",
"v2" : "12.504"
},
Re: get a counter value of a specific date
Posted: Thursday 23 January 2020 16:10
by steef27
thanks, this is perfect!
Re: get a counter value of a specific date
Posted: Monday 27 January 2020 11:11
by steef27
Can I also use multiple custom sensors instead of 1 text sensor with all values?
Re: get a counter value of a specific date
Posted: Monday 27 January 2020 23:22
by waaren
steef27 wrote: ↑Monday 27 January 2020 11:11
Can I also use multiple custom sensors instead of 1 text sensor with all values?
Please provide some more detail on what you mean / want.
Re: get a counter value of a specific date
Posted: Monday 03 February 2020 11:28
by steef27
Instead of using one single textsensor, i would like to store each value in a separate custom sensor.
I would like to split the text values.
Re: get a counter value of a specific date
Posted: Monday 03 February 2020 11:42
by waaren
steef27 wrote: ↑Monday 03 February 2020 11:28
Instead of using one single textsensor, i would like to store each value in a separate custom sensor.
I would like to split the text values.
That should probably be not too hard. If you share what you have now I can help.
Re: get a counter value of a specific date
Posted: Friday 07 February 2020 20:15
by steef27
Code: Select all
--[[
this dzVents script collects the current energy return / usage and gas usage to enable comparing with Same date last year figure
]]--
local scriptVar = 'gasHistory'
return
{
on =
{
--devices = { 4484, },
timer = { 'every 5 minutes' },
httpResponses = { scriptVar },
},
--logging = { level = domoticz.LOG_DEBUG },
execute = function(dz, item)
-- ***** Your settings below this line
local gas = dz.devices(4484) -- change to name of your gasMeter between quotes or id without quotes
local gasText2 = dz.devices(4746)
local gasText3 = dz.devices(4747)
local gasText4 = dz.devices(4748)
local gasText5 = dz.devices(4749)
-- ***** No changes required below this line
_G.logMarker = _G.moduleLabel
local day = 86400 -- (24 * 60 * 60)
local function callYearGraph(id)
url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. id
dz.openURL({ url = url, callback = scriptVar })
end
local function daysInLastYear()
local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
return math.floor(os.difftime(os.time(), lastYear) / day )
end
local function processResult(t)
local gasToday,gasLastYear, gasNow, gasYear = 0, 0, 0 ,0
local dateFmt = "%Y-%m-%d"
local today = os.date(dateFmt,os.time())
local yesterday = os.date(dateFmt,os.time() - day)
local sameDayLastYear = os.date(dateFmt,os.time() - daysInLastYear() * day)
for index, gas in ipairs(t) do
if gas.d == today then gasToday = gas.v gasNow = gas.c
elseif gas.d == yesterday then gasYesterDay = gas.v
elseif gas.d == sameDayLastYear then gasLastYear = gas.v gasYear = gas.c
end
end
--dz.log('gasToday: ' .. gasToday .. ' M³',LOG_DEBUG)
--dz.log('gasYesterDay: ' .. gasYesterDay .. ' M³',LOG_DEBUG)
--dz.log('gasLastYear: ' .. gasLastYear .. ' M³',LOG_DEBUG)
--dz.log('gasYearConsumption: ' .. (gasNow - gasYear) .. ' M³',LOG_DEBUG)
-- [[Aanpassing zodat verbuik vandaag ook wordt meegerekend ]]
--return gasToday, gasYesterDay, gasLastYear, (gasNow - gasYear)
return gasToday, gasYesterDay, gasLastYear, (gasNow - gasYear + gasToday)
end
-- local function updateTextSensor(gasToday, gasYesterDay, gasLastYear, gasTotal)
-- gasText.updateText
-- ( 'Gas used today: ' .. gasToday .. ' M³ \n' ..
-- 'Gas used yesterday: ' .. gasYesterDay .. ' M³ \n' ..
-- 'Gas used same day last Year: ' .. gasLastYear .. ' M³ \n' ..
-- 'Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³'
-- )
-- end
local function updateTextSensor(gasToday, gasYesterDay, gasLastYear, gasTotal)
--gasText2.updateText('Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³')
gasText2.updateCustomSensor(gasToday)
gasText3.updateCustomSensor(gasYesterDay)
gasText4.updateCustomSensor(gasLastYear)
gasText5.updateCustomSensor(gasTotal)
end
-- main
if item.isHTTPResponse and item.isJSON then
updateTextSensor(processResult(item.json.result))
elseif item.isTimer then
callYearGraph(gas.id)
else
dz.log('Error while retrieving gas data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
end
end
}
Re: get a counter value of a specific date
Posted: Saturday 08 February 2020 20:41
by waaren
steef27 wrote: ↑Friday 07 February 2020 20:15
can you try this ?
Code: Select all
--[[
this dzVents script collects the current energy return / usage and gas usage to enable comparing with Same date last year figure
]]--
local scriptVar = 'gasHistory'
return
{
on =
{
--devices = { 4484, },
timer = { 'every 5 minutes' },
httpResponses = { scriptVar },
},
--logging = { level = domoticz.LOG_DEBUG },
execute = function(dz, item)
-- ***** Your settings below this line
local gas = dz.devices(4484) -- change to name of your gasMeter between quotes or id without quotes
local gasText2 = dz.devices(4746)
local gasText3 = dz.devices(4747)
local gasText4 = dz.devices(4748)
local gasText5 = dz.devices(4749)
-- ***** No changes required below this line
_G.logMarker = _G.moduleLabel
local day = 86400 -- (24 * 60 * 60)
local function callYearGraph(id)
url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. id
dz.openURL({ url = url, callback = scriptVar })
end
local function daysInLastYear()
local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
return math.floor(os.difftime(os.time(), lastYear) / day )
end
local function processResult(t)
local gasTable = {}
local dateFmt = "%Y-%m-%d"
local today = os.date(dateFmt,os.time())
local yesterday = os.date(dateFmt,os.time() - day)
local sameDayLastYear = os.date(dateFmt,os.time() - daysInLastYear() * day)
for index, gas in ipairs(t) do
if gas.d == today then
gasTable.Today = gas.v
gasTable.gasNow = gas.c
elseif gas.d == yesterday then
gasTable.YesterDay = gas.v
elseif gas.d == sameDayLastYear then
gasTable.LastYear = gas.v
gasTable.gasYear = gas.c
end
end
gasTable.gasYearConsumption = gasTable.gasNow - gasTable.gasYear + gasTable.gasToday
--dz.log('gasToday: ' .. gasToday .. ' M³',LOG_DEBUG)
--dz.log('gasYesterDay: ' .. gasYesterDay .. ' M³',LOG_DEBUG)
--dz.log('gasLastYear: ' .. gasLastYear .. ' M³',LOG_DEBUG)
--dz.log('gasYearConsumption: ' .. (gasNow - gasYear) .. ' M³',LOG_DEBUG)
-- [[Aanpassing zodat verbuik vandaag ook wordt meegerekend ]]
--return gasToday, gasYesterDay, gasLastYear, (gasNow - gasYear)
return gasTable
end
-- local function updateTextSensor(gasToday, gasYesterDay, gasLastYear, gasTotal)
-- gasText.updateText
-- ( 'Gas used today: ' .. gasToday .. ' M³ \n' ..
-- 'Gas used yesterday: ' .. gasYesterDay .. ' M³ \n' ..
-- 'Gas used same day last Year: ' .. gasLastYear .. ' M³ \n' ..
-- 'Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³'
-- )
-- end
local function updateTextSensor(t)
--gasText2.updateText('Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³')
gasText2.updateCustomSensor(t.gasToday or 0)
gasText3.updateCustomSensor(t.gasYesterDay or 0)
gasText4.updateCustomSensor(t.gasLastYear) or 0
gasText5.updateCustomSensor(t.gasTotal or 0)
end
-- main
if item.isHTTPResponse and item.isJSON then
updateTextSensor(processResult(item.json.result))
elseif item.isTimer then
callYearGraph(gas.id)
else
dz.log('Error while retrieving gas data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
end
end
}
Re: get a counter value of a specific date
Posted: Monday 10 February 2020 15:54
by steef27
thanks, great!
Re: get a counter value of a specific date
Posted: Monday 04 May 2020 17:01
by Piacco
I want to calaculate the energy cost since I got my new energy contract until now.
An example I get a new energy contract on the 26th of june 2019, and i want to calculate how many energy I have used en produced since now.
I tought something like this:
26-06-2019 Get countervalue of the P1-meter C1 and C2 and subtract it with the countervalue of my P1-meter C1 and C2 from today and do exact the same for the delivered energy with counter C3 and C4.
But how do i get the countervalue on a specific day?
Greetz,
Piacco
Re: get a counter value of a specific date
Posted: Monday 04 May 2020 17:12
by waaren
Piacco wrote: ↑Monday 04 May 2020 17:01
But how do i get the countervalue on a specific day?
With http://<domoticz_ip:domoticz_port>/json.htm?type=graph&sensor=counter&idx=<idx>&range=year
you get all days
So walk the result table and select the specific date.
Or if it is a once of date then do this in your browser, write down the counter values and make these constants in your script.
Re: get a counter value of a specific date
Posted: Thursday 29 April 2021 11:28
by steef27
I've been running the script for some time now and I noticed that "sameDayLastYear" is not a year ago today, but a year ago tomorrow.
Re: get a counter value of a specific date
Posted: Thursday 29 April 2021 11:51
by waaren
steef27 wrote: ↑Thursday 29 April 2021 11:28
I've been running the script for some time now and I noticed that "sameDayLastYear" is not a year ago today, but a year ago tomorrow.
can you try again after changing
Code: Select all
local function daysInLastYear()
local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
return math.floor(os.difftime(os.time(), lastYear) / day )
end
to
Code: Select all
local function daysInLastYear()
local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
return math.ceil(os.difftime(os.time(), lastYear) / day )
end
?