Code: Select all
--[[ this dzVents script collects the Used Energy in kWh for this year, this month, this week and today
History:
20200114: first release on forum (thx to OedzesG for the idea )
20200120: Some small output lay-out changes
20200205: Fixed bugs in comparing JSON date to currentdate in month and day (thx to @Kranendijk for reporting and testing )
20200205: Use native WP.counterToday
20200414: Adapted for WP energy production
20200416: Made scriptVar Unique, added logline and reverted wrong calculation of seconds per day
]]--
local scriptVar = 'Used Energy'
return
{
on =
{
timer =
{
'every 1 minutes',
},
httpResponses =
{
scriptVar .. '*',
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = scriptVar,
},
data =
{
Delivered =
{
initial = {},
},
},
execute = function(dz, item)
-- ***** Your settings below this line
local Delivered = dz.devices(1) -- change to name of your WPMeter between quotes or id without quotes
local DeliveredText = dz.devices(25) -- text device showing WP Consumption
-- ***** No changes required below this line
local function getGraphData(period, id)
if period == nil then period = 'year' end
if id == nil then id = Delivered.id end
url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
dz.openURL({ url = url, callback = scriptVar })
end
local function updateTextSensor()
local lastWeek = dz.time.week - 1
if lastWeek == 0 then lastWeek = 52 end
DeliveredText.updateText
(
'' .. string.format("%6.1f", dz.data.Delivered.year ) .. ' kWh ín ' .. os.date("%Y ",os.time()) .. '\n' ..
'' .. string.format("%6.1f", dz.data.Delivered.month ) .. ' kWh ín ' .. os.date("%B %Y ",os.time()) .. '\n' ..
'' .. string.format("%6.1f", dz.data.Delivered.week ) .. ' kWh ín ' .. 'Week ' .. dz.time.week .. ' of ' .. dz.time.year .. '\n' ..
'' .. string.format("%6.1f", dz.data.Delivered.week2 ) .. ' kWh ín ' .. 'Week ' .. lastWeek .. ' of ' .. dz.time.year .. '\n' ..
'' .. string.format("%6.1f", dz.data.Delivered.day ) .. ' kWh on ' .. os.date("%A %d %B %Y",os.time())
)
end
local function processJSON(t)
local yearVolume, monthVolume, weekVolume, week2Volume = 0, 0, 0, 0
local currentMonthIdentifier = dz.time.rawDate:sub(1,4) .. '%-' .. dz.time.rawDate:sub(6,7)
local day = 86400 -- (24 * 60 * 60)
local startofWeek = os.date("%Y-%m-%d",os.time()- ( dz.time.wday - 2 ) * day)
local startofLastWeek = os.date("%Y-%m-%d",os.time()- ( dz.time.wday + 5 ) * day)
dz.log('wday: ' .. dz.time.wday,dz.LOG_FORCE)
dz.log('startofLastWeek: ' .. startofLastWeek,dz.LOG_FORCE)
dz.log('startofWeek: ' .. startofWeek,dz.LOG_FORCE)
for index, Delivered in ipairs(t) do
if Delivered.d:match(dz.time.year) then
yearVolume = yearVolume + Delivered.v
end
if Delivered.d:match(currentMonthIdentifier) then
monthVolume = monthVolume + Delivered.v
end
if Delivered.d >= startofWeek then
weekVolume = weekVolume + Delivered.v
end
if Delivered.d >= startofLastWeek and Delivered.d < startofWeek then
dz.log(Delivered,dz.LOG_FORCE)
week2Volume = week2Volume + Delivered.v
end
end
-- getGraphData('day' )
dz.data.Delivered.day = Delivered.counterToday
dz.data.Delivered.month = monthVolume
dz.data.Delivered.year = yearVolume + 20
dz.data.Delivered.week = weekVolume
dz.data.Delivered.week2 = week2Volume
updateTextSensor()
end
-- main
if item.isHTTPResponse and item.isJSON then
processJSON(item.json.result)
elseif item.isTimer or item.isDevice then
getGraphData()
else
dz.log('Error while retrieving Delivered data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
end
end
}