Re: Updating incremental counter with TIME value / json
Posted: Friday 02 April 2021 13:04
Code: Select all
--[[ getUptime.lua for [ dzVents >= 2.4 ] ]]--
return
{
on =
{
devices =
{
74, 88, 260, 350, 366, 368, 369, 370, 371,
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = "getUptime",
},
data =
{
devices =
{
initial = {},
},
},
execute = function(dz, item)
--- Your settings below this line
local count = dz.devices(420) -- change to device idx of your total counting device
local usage =
{
-- default is the power for devices not in this table
default = 10,
-- [device idx] = power
[74] = 10, -- Bedside-Lamp
[88] = 10, -- Backlight-Kitchen
[260] = 12, -- Light-Bedroom
[350] = 20, -- Light-Loggia
[366] = 10, -- Light-Сorridor
[368] = 5, -- Light-Kitchen
[369] = 11, -- Light-Bathroom
[370] = 6, -- Light-Kitchen-Extract
[371] = 20, -- Backlight-Bathroom
}
--- No changes required below this line
local dvData = dz.data.devices[item.name] or {}
local function getDataTotal(field)
local total = 0
for device, record in pairs(dz.data.devices) do
total = total + record[field]
end
return total
end
local function setData(item, delta)
local secondsOn = (dvData.secondsOn and ( dvData.secondsOn + delta ) ) or 0
local energy = (dvData.energy and ( dvData.energy + ( delta * ( usage[item.idx] or usage.default ) ) ) ) or 0 -- Watt seconds
dvData =
{
lastState = item.state,
secondsOn = secondsOn,
lastSwitchSeconds = os.time(),
lastSwitchTime = dz.time.rawDate .. " " .. dz.time.rawTime,
energy = energy,
}
end
if next(dvData) == nil then
setData(item, 0)
else
if item.state ~= dvData.lastState then
local deltaTime = 0
if item.state == "Off" then
deltaTime = os.time() - dvData.lastSwitchSeconds
end
setData(item, deltaTime)
local round_to_4 = dz.utils.round( getDataTotal('secondsOn'), 4 )
count.updateCounter(round_to_4)
end
dz.log('Device ' .. item.name .. ", State ==>> " .. item.state .. "; Date Time ==>> " .. dvData.lastSwitchTime .. "; secondsOn ===>> " ..
dvData.secondsOn .. "; Energy ===>> " .. dz.utils.round( ( dvData.energy / 3600 / 1000 ), 4 ) .. ' kWh' , dz.LOG_DEBUG)
end
dz.log('Total energy counted over all devices: ' .. getDataTotal('energy'), dz.LOG_DEBUG)
dz.log('Total seconds counted over all devices: ' .. getDataTotal('secondsOn'), dz.LOG_DEBUG)
end
}