Hi I'm using this script to extrapolate the Khw of the past two months.
But my invoice always starts from the 3rd of every month, so it would be necessary for the script to calculate like this from 03.05.2020 to 03.07.2020. How can I do thanks
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(580) -- Replace xxxx with ID of energyDevice you want to track
twoMonthTotals = dz.devices(725) -- 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=3, year=dz.time.year, month=dz.time.month - 1 })
monthMinus2 = os.date(dateFmt,os.time{day=3, 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
}
djdevil wrote: ↑Thursday 16 July 2020 10:50
Hi I'm using this script to extrapolate the Khw of the past two months.
But my invoice always starts from the 3rd of every month, so it would be necessary for the script to calculate like this from 03.05.2020 to 03.07.2020. How can I do thanks
At what day of the month are you going to schedule the script? Somewhere in July after the 3rd ? or something different?
djdevil wrote: ↑Thursday 16 July 2020 19:36
hello waaren thanks for the help, from the 3rd of every month the script should start! and the calculation of the last two months always from day 3
local scriptVar = 'twoMonthTotal'
return
{
on =
{
timer =
{
'on 4/* at 04:17' -- if you get data from previous months you only have to do this once a month at a quiet time
},
httpResponses =
{
scriptVar .. '*' ,
},
},
logging =
{
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = scriptVar
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(580) -- Replace xxxx with ID of energyDevice you want to track
twoMonthTotals = dz.devices(725) -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local function getDates(month, day )
local day = day or 3
local endMonth = month or dz.time.month
local beginMonth = (endMonth + 10) % 12
local beginYear = dz.time.year
if beginMonth > endMonth then beginYear = beginYear - 1 end
local endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(endMonth,2) .. '-' .. dz.utils.leadingZeros(day,2)
local beginDate = beginYear .. '-' .. dz.utils.leadingZeros(beginMonth,2) .. '-' .. dz.utils.leadingZeros(day,2)
return beginDate, endDate
end
local function triggerJSON(id, period)
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 = scriptVar .. '_' .. period})
end
local function calculateTwoMonthTotal(rt, firstDate, lastDate)
local twoMonthTotal = 0
local beginDate, endDate = getDates()
for id, result in ipairs(rt) do
if ( result.d >= beginDate) and ( result.d < endDate ) then
dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
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
local twoMonthTotal = calculateTwoMonthTotal(item.json.result)
dz.log('twoMonthTotal = ' .. twoMonthTotal,dz.LOG_DEBUG)
twoMonthTotals.update(0,twoMonthTotal)
else
dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')' ,dz.LOG_ERROR)
dz.log(item.data,dz.LOG_DEBUG)
end
end
}
djdevil wrote: ↑Thursday 16 July 2020 19:36
hello waaren thanks for the help, from the 3rd of every month the script should start! and the calculation of the last two months always from day 3
local scriptVar = 'twoMonthTotal'
return
{
on =
{
timer =
{
'on 4/* at 04:17' -- if you get data from previous months you only have to do this once a month at a quiet time
},
httpResponses =
{
scriptVar .. '*' ,
},
},
logging =
{
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = scriptVar
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(580) -- Replace xxxx with ID of energyDevice you want to track
twoMonthTotals = dz.devices(725) -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local function getDates(month, day )
local day = day or 3
local endMonth = month or dz.time.month
local beginMonth = (endMonth + 10) % 12
local beginYear = dz.time.year
if beginMonth > endMonth then beginYear = beginYear - 1 end
local endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(endMonth,2) .. '-' .. dz.utils.leadingZeros(day,2)
local beginDate = beginYear .. '-' .. dz.utils.leadingZeros(beginMonth,2) .. '-' .. dz.utils.leadingZeros(day,2)
return beginDate, endDate
end
local function triggerJSON(id, period)
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 = scriptVar .. '_' .. period})
end
local function calculateTwoMonthTotal(rt, firstDate, lastDate)
local twoMonthTotal = 0
local beginDate, endDate = getDates()
for id, result in ipairs(rt) do
if ( result.d >= beginDate) and ( result.d < endDate ) then
dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
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
local twoMonthTotal = calculateTwoMonthTotal(item.json.result)
dz.log('twoMonthTotal = ' .. twoMonthTotal,dz.LOG_DEBUG)
twoMonthTotals.update(0,twoMonthTotal)
else
dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')' ,dz.LOG_ERROR)
dz.log(item.data,dz.LOG_DEBUG)
end
end
}
Hello i try the code this the log debug it seems that we miss the day 3-07 inside, in fact it stops at 02-07 how come?
djdevil wrote: ↑Friday 17 July 2020 10:27
Hello i try the code this the log debug it seems that we miss the day 3-07 inside, in fact it stops at 02-07 how come?
djdevil wrote: ↑Friday 17 July 2020 10:27
Hello i try the code this the log debug it seems that we miss the day 3-07 inside, in fact it stops at 02-07 how come?
if ( result.d >= beginDate) and ( result.d < endDate ) then
dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
twoMonthTotal = twoMonthTotal + result.v
end
I assume you need to either exclude the last or the first date (otherwise you will pay this day in two periods.
You control this by using
> or >= ; greater then or greater or equal
< or <= ; smaller then or smaller or equal
hello thanks for replying, so to include also the 3 of the last month, how should I do? or is your code already included? because my bills always start from the 3rd day of the last two months
the total carried out by hand from 529.164 khw of the previous two months from 3-05 to 3-07 inclusive
djdevil wrote: ↑Friday 17 July 2020 12:06
hello thanks for replying, so to include also the 3 of the last month, how should I do? or is your code already included? because my bills always start from the 3rd day of the last two months
if ( result.d >= beginDate) and ( result.d <= endDate ) then
dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
twoMonthTotal = twoMonthTotal + result.v
end
But this not make sense. One day is now part of the total in two periods
period 1: 3/1 -3/3
period 2: 3/3 - 3/5 ==>> 3/3 is part of the total in period 1 and in period 2
period 3: 3/5 - 3/7 ==>> 3/5 is part of the total in period 2 and in period 3