Script KWh month whit a especific date [Solved]
Moderator: leecollings
-
- Posts: 69
- Joined: Monday 04 September 2017 17:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Script KWh month whit a especific date
Hello,
I need help in creating a script that gives me the total cost per month of KWh, but with a date starting on the day (ex 13 of each month and ending on 12 of each month).
I have OWL CM 180.
Can anyone help me?
Thank you
I need help in creating a script that gives me the total cost per month of KWh, but with a date starting on the day (ex 13 of each month and ending on 12 of each month).
I have OWL CM 180.
Can anyone help me?
Thank you
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Script KWh month whit a especific date
What is the name / deviceType and devicesubType in domoticz of this device as seen on the device tab ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 69
- Joined: Monday 04 September 2017 17:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Script KWh month whit a especific date
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Script KWh month whit a especific date
Can you check with this one?
For now it only reports the costs to the domoticz log. If you want it in a device or variable let me know.
Code: Select all
--[[
script calculates the cost of energy of one kwH device
KwH costs need to be set in domoticz meters / coiunters settings
]]--
local scriptVar = 'getCosts'
return
{
on =
{
timer =
{
'at 08:00' , --- change to your liking
},
devices =
{
scriptVar,
},
httpResponses =
{
scriptVar .. '_*',
},
},
data =
{
KwHCost = { initial = 0 },
},
logging =
{
level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all on
marker = scriptVar,
},
execute = function(dz, item)
local sourceDeviceIndex = 2872 -- change to device you want to measure
local startDayOfMonth = 13
if item.isTimer or item.isDevice then
costURL = dz.settings['Domoticz url'] .. '/json.htm?type=settings' -- to get cost setting
countURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. sourceDeviceIndex -- to get cost setting
dz.openURL(
{
url = costURL,
callback = scriptVar .. '_costs',
})
dz.openURL(
{
url = countURL,
callback = scriptVar .. '_logCount',
}).afterSec(5)
return
end
local function makeMonth(delta)
if math.floor(delta) ~= delta or delta < - 11 or delta > 11 then
dz.log('Only integers -11 - +11 allowed',dz.LOG_ERROR)
return
end
local year = dz.time.year
if delta < 0 and dz.time.month + delta < 0 then year = dz.time.year - 1 end
if delta > 0 and dz.time.month + delta > 12 then year = dz.time.year + 1 end
return ( dz.time.month + delta) % 12, year
end
local function getCost(t, day)
local startMonth, startYear = makeMonth(-1)
if dz.time.day < day then startMonth, startYear = makeMonth(-2) end
startDate = startYear .. '-' .. dz.utils.leadingZeros(startMonth, 2) .. '-' .. dz.utils.leadingZeros(day, 2)
endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(( startMonth + 1 ) % 12 , 2) .. '-' .. dz.utils.leadingZeros((day - 1), 2)
local minCounter = 10^32
local maxCounter = 0
for _,r in ipairs(t) do
if tonumber(r.c) > maxCounter and r.d >= startDate and r.d <= endDate then maxCounter = tonumber(r.c) end
if tonumber(r.c) < minCounter and r.d >= startDate and r.d <= endDate then minCounter = tonumber(r.c) end
end
return dz.utils.round( ( maxCounter - minCounter ) * dz.data.KwHCost, 2 ), startDate, endDate
end
if item.ok and item.isJSON then
if item.trigger:find('_costs') then
dz.data.KwHCost = item.json.CostEnergy
else
local energyCost, startDate, endDate = getCost(item.json.result, startDayOfMonth)
dz.log('Energycost for device ' .. dz.devices(sourceDeviceIndex).name .. ' between ' .. startDate ..' and ' .. endDate .. ' is: € ' .. energyCost, dz.LOG_FORCE )
end
else
dz.log('There was a problem handling' .. item.trigger, dz.LOG_ERROR)
dz.log(item, dz.LOG_DEBUG)
end
end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 69
- Joined: Monday 04 September 2017 17:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Script KWh month whit a especific date
Hello Waarenwaaren wrote: ↑Tuesday 25 August 2020 14:37Can you check with this one?
For now it only reports the costs to the domoticz log. If you want it in a device or variable let me know.
Code: Select all
--[[ script calculates the cost of energy of one kwH device KwH costs need to be set in domoticz meters / coiunters settings ]]-- local scriptVar = 'getCosts' return { on = { timer = { 'at 08:00' , --- change to your liking }, devices = { scriptVar, }, httpResponses = { scriptVar .. '_*', }, }, data = { KwHCost = { initial = 0 }, }, logging = { level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all on marker = scriptVar, }, execute = function(dz, item) local sourceDeviceIndex = 2872 -- change to device you want to measure local startDayOfMonth = 13 if item.isTimer or item.isDevice then costURL = dz.settings['Domoticz url'] .. '/json.htm?type=settings' -- to get cost setting countURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. sourceDeviceIndex -- to get cost setting dz.openURL( { url = costURL, callback = scriptVar .. '_costs', }) dz.openURL( { url = countURL, callback = scriptVar .. '_logCount', }).afterSec(5) return end local function makeMonth(delta) if math.floor(delta) ~= delta or delta < - 11 or delta > 11 then dz.log('Only integers -11 - +11 allowed',dz.LOG_ERROR) return end local year = dz.time.year if delta < 0 and dz.time.month + delta < 0 then year = dz.time.year - 1 end if delta > 0 and dz.time.month + delta > 12 then year = dz.time.year + 1 end return ( dz.time.month + delta) % 12, year end local function getCost(t, day) local startMonth, startYear = makeMonth(-1) if dz.time.day < day then startMonth, startYear = makeMonth(-2) end startDate = startYear .. '-' .. dz.utils.leadingZeros(startMonth, 2) .. '-' .. dz.utils.leadingZeros(day, 2) endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(( startMonth + 1 ) % 12 , 2) .. '-' .. dz.utils.leadingZeros((day - 1), 2) local minCounter = 10^32 local maxCounter = 0 for _,r in ipairs(t) do if tonumber(r.c) > maxCounter and r.d >= startDate and r.d <= endDate then maxCounter = tonumber(r.c) end if tonumber(r.c) < minCounter and r.d >= startDate and r.d <= endDate then minCounter = tonumber(r.c) end end return dz.utils.round( ( maxCounter - minCounter ) * dz.data.KwHCost, 2 ), startDate, endDate end if item.ok and item.isJSON then if item.trigger:find('_costs') then dz.data.KwHCost = item.json.CostEnergy else local energyCost, startDate, endDate = getCost(item.json.result, startDayOfMonth) dz.log('Energycost for device ' .. dz.devices(sourceDeviceIndex).name .. ' between ' .. startDate ..' and ' .. endDate .. ' is: € ' .. energyCost, dz.LOG_FORCE ) end else dz.log('There was a problem handling' .. item.trigger, dz.LOG_ERROR) dz.log(item, dz.LOG_DEBUG) end end }
thank you very much for the script, works good!
Yes, I really wanted to pass the values to a device, dummy text, and see current month in between the days! you can help me how?
ty
Last edited by Nefsolive on Wednesday 26 August 2020 12:08, edited 1 time in total.
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Script KWh month whit a especific date
This one should do that
Code: Select all
--[[
script calculates the cost of energy of one kwH device
KwH costs need to be set in domoticz meters / counters settings
]]--
local scriptVar = 'getCosts'
return
{
on =
{
timer =
{
'at 08:00' , --- change to your liking
},
devices =
{
scriptVar,
},
httpResponses =
{
scriptVar .. '_*',
},
},
data =
{
KwHCost = { initial = 0 },
},
logging =
{
level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all on
marker = scriptVar,
},
execute = function(dz, item)
local sourceDeviceIndex = 2872 -- change to device you want to measure
local startDayOfMonth = 13
local textDevice = dz.devices('getCosts' ) -- change to devicename you want to see the text message (virtual text device)
if item.isTimer or item.isDevice then
costURL = dz.settings['Domoticz url'] .. '/json.htm?type=settings' -- to get cost setting
countURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. sourceDeviceIndex -- to get cost setting
dz.openURL(
{
url = costURL,
callback = scriptVar .. '_costs',
})
dz.openURL(
{
url = countURL,
callback = scriptVar .. '_logCount',
}).afterSec(5)
return
end
local function makeMonth(delta)
if math.floor(delta) ~= delta or delta < - 11 or delta > 11 then
dz.log('Only integers -11 - +11 allowed',dz.LOG_ERROR)
return
end
local year = dz.time.year
if delta < 0 and dz.time.month + delta < 0 then year = dz.time.year - 1 end
if delta > 0 and dz.time.month + delta > 12 then year = dz.time.year + 1 end
return ( dz.time.month + delta) % 12, year
end
local function getCost(t, day)
local startMonth, startYear = makeMonth(-1)
if dz.time.day < day then startMonth, startYear = makeMonth(-2) end
startDate = startYear .. '-' .. dz.utils.leadingZeros(startMonth, 2) .. '-' .. dz.utils.leadingZeros(day, 2)
endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(( startMonth + 1 ) % 12 , 2) .. '-' .. dz.utils.leadingZeros((day - 1), 2)
local minCounter = 10^32
local maxCounter = 0
for _,r in ipairs(t) do
if tonumber(r.c) > maxCounter and r.d >= startDate and r.d <= endDate then maxCounter = tonumber(r.c) end
if tonumber(r.c) < minCounter and r.d >= startDate and r.d <= endDate then minCounter = tonumber(r.c) end
end
return dz.utils.round( ( maxCounter - minCounter ) * dz.data.KwHCost, 2 ), startDate, endDate
end
if item.ok and item.isJSON then
if item.trigger:find('_costs') then
dz.data.KwHCost = item.json.CostEnergy
else
local energyCost, startDate, endDate = getCost(item.json.result, startDayOfMonth)
dz.log('Energycost for device ' .. dz.devices(sourceDeviceIndex).name .. ' between ' .. startDate ..' and ' .. endDate .. ' is: € ' .. energyCost, dz.LOG_FORCE )
textDevice.updateText('Energycost for device ' .. dz.devices(sourceDeviceIndex).name .. '\n between ' .. startDate ..' and ' .. endDate .. '\n is: € ' .. energyCost)
end
else
dz.log('There was a problem handling' .. item.trigger, dz.LOG_ERROR)
dz.log(item, dz.LOG_DEBUG)
end
end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 69
- Joined: Monday 04 September 2017 17:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Script KWh month whit a especific date
Works perfect Waaren!waaren wrote: ↑Wednesday 26 August 2020 11:31This one should do thatCode: Select all
--[[ script calculates the cost of energy of one kwH device KwH costs need to be set in domoticz meters / counters settings ]]-- local scriptVar = 'getCosts' return { on = { timer = { 'at 08:00' , --- change to your liking }, devices = { scriptVar, }, httpResponses = { scriptVar .. '_*', }, }, data = { KwHCost = { initial = 0 }, }, logging = { level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all on marker = scriptVar, }, execute = function(dz, item) local sourceDeviceIndex = 2872 -- change to device you want to measure local startDayOfMonth = 13 local textDevice = dz.devices('getCosts' ) -- change to devicename you want to see the text message (virtual text device) if item.isTimer or item.isDevice then costURL = dz.settings['Domoticz url'] .. '/json.htm?type=settings' -- to get cost setting countURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. sourceDeviceIndex -- to get cost setting dz.openURL( { url = costURL, callback = scriptVar .. '_costs', }) dz.openURL( { url = countURL, callback = scriptVar .. '_logCount', }).afterSec(5) return end local function makeMonth(delta) if math.floor(delta) ~= delta or delta < - 11 or delta > 11 then dz.log('Only integers -11 - +11 allowed',dz.LOG_ERROR) return end local year = dz.time.year if delta < 0 and dz.time.month + delta < 0 then year = dz.time.year - 1 end if delta > 0 and dz.time.month + delta > 12 then year = dz.time.year + 1 end return ( dz.time.month + delta) % 12, year end local function getCost(t, day) local startMonth, startYear = makeMonth(-1) if dz.time.day < day then startMonth, startYear = makeMonth(-2) end startDate = startYear .. '-' .. dz.utils.leadingZeros(startMonth, 2) .. '-' .. dz.utils.leadingZeros(day, 2) endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(( startMonth + 1 ) % 12 , 2) .. '-' .. dz.utils.leadingZeros((day - 1), 2) local minCounter = 10^32 local maxCounter = 0 for _,r in ipairs(t) do if tonumber(r.c) > maxCounter and r.d >= startDate and r.d <= endDate then maxCounter = tonumber(r.c) end if tonumber(r.c) < minCounter and r.d >= startDate and r.d <= endDate then minCounter = tonumber(r.c) end end return dz.utils.round( ( maxCounter - minCounter ) * dz.data.KwHCost, 2 ), startDate, endDate end if item.ok and item.isJSON then if item.trigger:find('_costs') then dz.data.KwHCost = item.json.CostEnergy else local energyCost, startDate, endDate = getCost(item.json.result, startDayOfMonth) dz.log('Energycost for device ' .. dz.devices(sourceDeviceIndex).name .. ' between ' .. startDate ..' and ' .. endDate .. ' is: € ' .. energyCost, dz.LOG_FORCE ) textDevice.updateText('Energycost for device ' .. dz.devices(sourceDeviceIndex).name .. '\n between ' .. startDate ..' and ' .. endDate .. '\n is: € ' .. energyCost) end else dz.log('There was a problem handling' .. item.trigger, dz.LOG_ERROR) dz.log(item, dz.LOG_DEBUG) end end }
Thank u!
And how i make for the current month like beetwin, August 13 and September 12?
ty
-
- Posts: 69
- Joined: Monday 04 September 2017 17:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Script KWh month whit a especific date
I've managed to place the script between the days of the current month and the next.
My thanks for all the support Waaren.
My thanks for all the support Waaren.
-
- Posts: 69
- Joined: Monday 04 September 2017 17:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Script KWh month whit a especific date
Hi waaren,waaren wrote: ↑Wednesday 26 August 2020 11:31This one should do thatCode: Select all
--[[ script calculates the cost of energy of one kwH device KwH costs need to be set in domoticz meters / counters settings ]]-- local scriptVar = 'getCosts' return { on = { timer = { 'at 08:00' , --- change to your liking }, devices = { scriptVar, }, httpResponses = { scriptVar .. '_*', }, }, data = { KwHCost = { initial = 0 }, }, logging = { level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all on marker = scriptVar, }, execute = function(dz, item) local sourceDeviceIndex = 2872 -- change to device you want to measure local startDayOfMonth = 13 local textDevice = dz.devices('getCosts' ) -- change to devicename you want to see the text message (virtual text device) if item.isTimer or item.isDevice then costURL = dz.settings['Domoticz url'] .. '/json.htm?type=settings' -- to get cost setting countURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. sourceDeviceIndex -- to get cost setting dz.openURL( { url = costURL, callback = scriptVar .. '_costs', }) dz.openURL( { url = countURL, callback = scriptVar .. '_logCount', }).afterSec(5) return end local function makeMonth(delta) if math.floor(delta) ~= delta or delta < - 11 or delta > 11 then dz.log('Only integers -11 - +11 allowed',dz.LOG_ERROR) return end local year = dz.time.year if delta < 0 and dz.time.month + delta < 0 then year = dz.time.year - 1 end if delta > 0 and dz.time.month + delta > 12 then year = dz.time.year + 1 end return ( dz.time.month + delta) % 12, year end local function getCost(t, day) local startMonth, startYear = makeMonth(-1) if dz.time.day < day then startMonth, startYear = makeMonth(-2) end startDate = startYear .. '-' .. dz.utils.leadingZeros(startMonth, 2) .. '-' .. dz.utils.leadingZeros(day, 2) endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(( startMonth + 1 ) % 12 , 2) .. '-' .. dz.utils.leadingZeros((day - 1), 2) local minCounter = 10^32 local maxCounter = 0 for _,r in ipairs(t) do if tonumber(r.c) > maxCounter and r.d >= startDate and r.d <= endDate then maxCounter = tonumber(r.c) end if tonumber(r.c) < minCounter and r.d >= startDate and r.d <= endDate then minCounter = tonumber(r.c) end end return dz.utils.round( ( maxCounter - minCounter ) * dz.data.KwHCost, 2 ), startDate, endDate end if item.ok and item.isJSON then if item.trigger:find('_costs') then dz.data.KwHCost = item.json.CostEnergy else local energyCost, startDate, endDate = getCost(item.json.result, startDayOfMonth) dz.log('Energycost for device ' .. dz.devices(sourceDeviceIndex).name .. ' between ' .. startDate ..' and ' .. endDate .. ' is: € ' .. energyCost, dz.LOG_FORCE ) textDevice.updateText('Energycost for device ' .. dz.devices(sourceDeviceIndex).name .. '\n between ' .. startDate ..' and ' .. endDate .. '\n is: € ' .. energyCost) end else dz.log('There was a problem handling' .. item.trigger, dz.LOG_ERROR) dz.log(item, dz.LOG_DEBUG) end end }
The script is working, but it is not giving me the correct values between the 13th and 12th of the following month! If you check the switch report, the cost values do not correspond to the script cost values!
I would like him to sum the values to me from the 13th to the 12th of the next month! Like today is 15, so sum the values 13th too 15th, and next day show me the total cost, like 13th too 16th...and finish too cost 0 on 12th day next month

On my log i see;
2020-09-15 17:51:00.333 Status: dzVents: Debug: getCosts: Processing device-adapter for Custo Mensal: Text device
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: url = http://*******/json.htm?type=settings
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: method = GET
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: post data = nil
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: headers = nil
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: callback = getCosts_costs
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: url = http://*******/json.htm?type=graph&sensor=counter&range=year&idx=1384
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: method = GET
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: post data = nil
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: headers = nil
2020-09-15 17:51:00.334 Status: dzVents: Debug: getCosts: OpenURL: callback = getCosts_logCount
2020-09-15 17:51:00.336 Status: dzVents: Info: getCosts: ------ Finished mensal.lua
2020-09-15 17:51:00.338 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-09-15 17:51:00.348 Status: Incoming connection from: 127.0.0.1
2020-09-15 17:51:02.305 Status: dzVents: Info: Handling httpResponse-events for: "getCosts_costs"
2020-09-15 17:51:02.305 Status: dzVents: Info: getCosts: ------ Start external script: mensal.lua: HTTPResponse: "getCosts_costs"
2020-09-15 17:51:02.322 Status: dzVents: Debug: getCosts: Processing device-adapter for Custo Mensal: Text device
2020-09-15 17:51:02.323 Status: dzVents: Info: getCosts: ------ Finished mensal.lua
2020-09-15 17:51:05.581 Status: dzVents: Info: Handling httpResponse-events for: "getCosts_logCount"
2020-09-15 17:51:05.581 Status: dzVents: Info: getCosts: ------ Start external script: mensal.lua: HTTPResponse: "getCosts_logCount"
2020-09-15 17:51:05.619 Status: dzVents: Debug: getCosts: Processing device-adapter for Custo Mensal: Text device
2020-09-15 17:51:05.620 Status: dzVents: !Info: getCosts: 2020-09-13 e 2020-10-12 é de: € 3.0
2020-09-15 17:51:05.622 Status: dzVents: Info: getCosts: ------ Finished mensal.lua
2020-09-15 17:51:05.622 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
Can you help me whit, please?
Ty
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Script KWh month whit a especific date
I can't see any issue with the script. It just sum up the relevant values and calculate the total by multiplying it with the cost / kWh.
Maybe you can show the report and point out what you expect and where the difference is?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 69
- Joined: Monday 04 September 2017 17:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Script KWh month whit a especific date [Solved]
Hi Waaren,
Thanks for all the support.
I made some changes to the script and found another way to get the calculation, including the relative taxes of energy consumption.
I submit the changed script.
kind regards
Code: Select all
--[[
script calculates the cost of energy of one kwH device
KwH costs need to be set in domoticz meters / counters settings
]]--
local scriptVar = 'getCosts'
return
{
on =
{
timer =
{
'at 07:00' , --- change to your liking
},
devices =
{
scriptVar,
},
httpResponses =
{
scriptVar .. '_*',
},
},
data =
{
KwHCost = { initial = 0 },
potencia = { initial = 0},
IEC = { initial = 0}
},
logging =
{
level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all on
marker = scriptVar,
},
execute = function(dz, item)
local sourceDeviceIndex = 1384 -- change to device you want to measure
local startDayOfMonth = 13
local textDevice = dz.devices('Custo Mensal' ) -- change to devicename you want to see the text message (virtual text device)
if item.isTimer or item.isDevice then
costURL = dz.settings['Domoticz url'] .. '/json.htm?type=settings' -- to get cost setting
countURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. sourceDeviceIndex -- to get cost setting
dz.openURL(
{
url = costURL,
callback = scriptVar .. '_costs',
})
dz.openURL(
{
url = countURL,
callback = scriptVar .. '_logCount',
}).afterSec(5)
return
end
local function makeMonth(delta)
if math.floor(delta) ~= delta or delta < - 11 or delta > 11 then
dz.log('Only integers -11 - +11 allowed',dz.LOG_ERROR)
return
end
local year = dz.time.year
if delta < 0 and dz.time.month + delta < 0 then year = dz.time.year - 1 end
if delta > 0 and dz.time.month + delta > 12 then year = dz.time.year + 1 end
return ( dz.time.month + delta) % 12, year
end
local function getCost(t, day)
local startMonth, startYear = makeMonth(0)
if dz.time.day < day then startMonth, startYear = makeMonth(-1) end
startDate = startYear .. '-' .. dz.utils.leadingZeros(startMonth, 2) .. '-' .. dz.utils.leadingZeros(day, 2)
endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(( startMonth + 1 ) % 12 , 2) .. '-' .. dz.utils.leadingZeros((day - 1), 2)
local accu = 0
local daysCounter = 0
for _,r in ipairs(t) do
if r.d >= startDate and r.d <= endDate then
accu = accu + r.v
daysCounter = daysCounter + 1
end
end
local potenciaKWH = daysCounter * dz.data.potencia
local IECCost = accu * dz.data.IEC
local accuCost = dz.utils.round( (accu * dz.data.KwHCost) + potenciaKWH + 0.0861 + IECCost + 3.02, 2)
return accuCost, startDate, endDate
end
if item.ok and item.isJSON then
if item.trigger:find('_costs') then
dz.data.KwHCost = item.json.CostEnergy
dz.data.potencia = item.json.CostEnergyT2
dz.data.IEC = item.json.CostEnergyR1
else
local energyCost, startDate, endDate = getCost(item.json.result, startDayOfMonth)
dz.log('' .. startDate ..' e ' .. endDate .. ' é de: € ' .. energyCost, dz.LOG_FORCE )
textDevice.updateText(' entre ' .. startDate ..' e ' .. endDate .. ' é de: € ' .. energyCost)
end
else
dz.log('There was a problem handling' .. item.trigger, dz.LOG_ERROR)
dz.log(item, dz.LOG_DEBUG)
end
end
}
Who is online
Users browsing this forum: No registered users and 1 guest