waaren wrote: ↑Wednesday 26 August 2020 11:31
Nefsolive wrote: ↑Wednesday 26 August 2020 10:54
I really wanted to pass the values to a device dummy text, you can help me how.
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
}
Hi waaren,
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