Page 2 of 3
Re: Updating incremental counter with TIME value / json
Posted: Friday 16 August 2019 20:18
by piotrm
waaren wrote: ↑Friday 16 August 2019 19:28
OK need some time to test if the counter incremental is best suited for this or that using a 'normal'counter is preferable. Might take a couple of days before I have these results.
I thought the solution would be to add a few lines of code, which would take you a few minutes. Thanks for your time!
I don't know which Domoticz counters a regular counter or counter incremental will be better. I would like to have daily, weekly, monthly and yearly statistics, as long as it doesn't cause you a big problem.
By the way, such functionality (counting the time of the switch) should be built into Domoticz in my opinion.
Re: Updating incremental counter with TIME value / json
Posted: Sunday 18 August 2019 23:24
by waaren
piotrm wrote: ↑Friday 16 August 2019 20:18
I would like to have daily, weekly, monthly and yearly statistics
See below script adjusted for an incremental counter. Please feel free to ask clarification if something is not clear.
Code: Select all
--getUptime.lua
local frequency = 5 -- set to desired frequency (1,2,3,4,5,6,10,12,15,20,30) -- lower number ==>> more accurate reporting
local mySwitch = 'mySwitch' -- set to name of device to be measured
local timeKeeper = 'timeKeeper' -- set to name of created virtual counter incremental
local myTimeUnit = 'minute' -- set to unit preference (second, minute or hour)
--[[ also adjust device settings of timeKeeper counter to chosen timeUnit Press [edit] on the device (on the utility tab)
Type: Counter
Divider: 0
Meter Offset: 0
Value Quantity (e.g. Weight): Count
Value Units (e.g. Kg): choose seconds, hours or minutes
]]--
return
{
on =
{
timer = {'every '.. frequency .. ' minutes'}, -- this will ensure updates to counter is done every <frequency> minutes the device is switched ON
devices = { mySwitch },
},
logging =
{
level = domoticz.LOG_ERROR,
marker = "getUptime" },
data =
{
state = { initial = "-" },
secondsOn = { initial = 0 },
base = { initial = 0 },
},
execute = function(dz, item)
local mySwitch = dz.devices(mySwitch)
local timeKeeper = dz.devices(timeKeeper)
local timeUnits = {
minute = 60,
hour = 3600,
second = 1,
}
local function updateCounter()
dz.data.secondsOn = dz.data.secondsOn + ( dz.time.dDate - dz.data.base )
if timeKeeper.count ~= dz.data.secondsOn / timeUnits[myTimeUnit] then
timeKeeper.updateCounter(dz.utils.round(dz.data.secondsOn / timeUnits[myTimeUnit]) )
end
end
if item.isTimer and mySwitch.active and dz.data.base ~= 0 then
updateCounter()
dz.data.base = dz.time.dDate
dz.data.state = mySwitch.state
elseif item.isDevice and mySwitch.active and dz.data.state ~= 'On' then
dz.data.base = dz.time.dDate
dz.data.state = 'On'
elseif item.isDevice and dz.data.state == 'On' then
updateCounter()
dz.data.state = 'Off'
elseif item.isTimer and dz.data.base == 0 then
dz.data.base = dz.time.dDate
dz.data.state = mySwitch.state
end
dz.log("State ==>> " .. mySwitch.state .. "; Date Time ==>> " .. dz.time.rawDate .." " ..
dz.time.rawTime .. "; " .. myTimeUnit .. "s On today ===>> " .. timeKeeper.counterToday ,dz.LOG_DEBUG)
end
}
Re: Updating incremental counter with TIME value / json
Posted: Tuesday 20 August 2019 1:41
by piotrm
waaren wrote: ↑Sunday 18 August 2019 23:24
See below script adjusted for an incremental counter. Please feel free to ask clarification if something is not clear.
Thanks for the script. I'll test it today and let you know.
Re: Updating incremental counter with TIME value / json
Posted: Wednesday 21 August 2019 22:19
by piotrm
waaren wrote: ↑Sunday 18 August 2019 23:24
See below script adjusted for an incremental counter. Please feel free to ask clarification if something is not clear.
The script works perfectly and even better than I expected. Very easy to set up and does its job.
After a slight modification (multiplication or division of time) for receivers with constant power (e.g. electric boiler, electric heater, light bulb) you can measure energy consumption or the cost of the device without any additional hardware. Very useful detail.
Once again, many thanks for help!!!
Re: Updating incremental counter with TIME value / json
Posted: Friday 27 March 2020 8:22
by Ayep
Hi,
First and foremost, I would like to thank the contributors for this script that has proven to be quite useful.
However, since I last updated Domoticz (the latest march version), my counter has gone haywire. It used to count correctly, an hour here and there of daily use, and a correct total increment (around 300 since I included the script).
Since the Domoticz update (on my Syno NAS), the total counter shot up to 21000, and yesterday's daily counter was up to 19000. Tried to work out numbers, wondering if it was an hours/minutes/seconds problem, but the numbers don't seem to add up.
Anyone else encountering issues since the update?
Thanks
Re: Updating incremental counter with TIME value / json
Posted: Friday 27 March 2020 9:26
by waaren
Ayep wrote: ↑Friday 27 March 2020 8:22
Since the Domoticz update (on my Syno NAS), the total counter shot up to 21000, and yesterday's daily counter was up to 19000. Tried to work out numbers, wondering if it was an hours/minutes/seconds problem, but the numbers don't seem to add up.
can you please set the script log to debug and share your devicetype(s) and other settings ? Maybe it helps in finding what is causing the issue.
Re: Updating incremental counter with TIME value / json
Posted: Friday 27 March 2020 15:14
by Ayep
OK thanks, here we go.
The timer is used to work out the duration of the pool filtration (another script, totally independent at this time, sets filtration at specific times and duration according to temperature - no variable is passed to and from though).

- Pool-filtration.png (18.23 KiB) Viewed 3157 times

- Untitled-1.png (15.94 KiB) Viewed 3157 times
I've done a manual calculation, every minute now increments 344 hours on the counter. (Why 344? Can't find any obvious link - it's divisible by 8, which gives 43, a prime number)
This is what the log comes up with, when filtration is not running (counter not incrementing) :
2020-03-27 14:45:00.537 Status: dzVents: Info: Filtration_timer: ------ Start internal script: Filtration_timer:, trigger: "every 1 minutes"
2020-03-27 14:45:00.549 Status: dzVents: Debug: Filtration_timer: Processing device-adapter for Pool Filtration: Switch device adapter
2020-03-27 14:45:00.550 Status: dzVents: Debug: Filtration_timer: Processing device-adapter for Pool filtration timer: Counter device adapter
2020-03-27 14:45:00.550 Status: dzVents: Debug: Filtration_timer: State ==>> Off; Date Time ==>> 2020-03-27 14:45:00; hours On today ===>> 19590
2020-03-27 14:45:00.551 Status: dzVents: Info: Filtration_timer: ------ Finished Filtration_timer
When filtration is running :
2020-03-27 14:57:00.165 Status: dzVents: Info: Filtration_timer: ------ Start internal script: Filtration_timer:, trigger: "every 1 minutes"
2020-03-27 14:57:00.176 Status: dzVents: Debug: Filtration_timer: Processing device-adapter for Pool Filtration: Switch device adapter
2020-03-27 14:57:00.177 Status: dzVents: Debug: Filtration_timer: Processing device-adapter for Pool filtration timer: Counter device adapter
2020-03-27 14:57:00.177 Status: dzVents: Debug: Filtration_timer: State ==>> On; Date Time ==>> 2020-03-27 14:57:00; hours On today ===>> 23030
2020-03-27 14:57:00.178 Status: dzVents: Info: Filtration_timer: ------ Finished Filtration_timer
2020-03-27 14:57:00.178 Status: EventSystem: Script event triggered: /usr/local/domoticz/dzVents/runtime/dzVents.lua
This the code ; I usually note modifications in comments.
Code: Select all
--getUptime.lua, or now "Filtration_timer.lua"
--143 = P1 smart meter
local frequency = 1 -- set to desired frequency (1,2,3,4,5,6,10,12,15,20,30) -- lower number ==>> more accurate reporting
local mySwitch = 109 --'Pool Filtration'
local timeKeeper = 151 --'Pool filtration timer' -- set to name of created virtual counter incremental
local myTimeUnit = 'hour' -- set to unit preference (second, minute or hour)
-- = os.time(domoticz.time)
local Time = require('Time')
local now = Time() -- current time
--currentTime = now.rawTime --no longer needed : this is for resetting counter.
--[[ also adjust device settings of timeKeeper counter to chosen timeUnit Press [edit] on the device (on the utility tab)
Type: Counter
Divider: 0
Meter Offset: 0
Value Quantity (e.g. Weight): Count
Value Units (e.g. Kg): choose seconds, hours or minutes
]]--
return
{
on =
{
timer = {'every '.. frequency .. ' minutes'}, -- this will ensure updates to counter is done every <frequency> minutes the device is switched ON
devices = { mySwitch },
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = "Filtration_timer" },
data =
{
state = { initial = "-" },
secondsOn = { initial = 0 },
base = { initial = 0 },
},
execute = function(dz, item)
local mySwitch = dz.devices(mySwitch)
local timeKeeper = dz.devices(timeKeeper)
local timeUnits = {
minute = 60,
hour = 3600,
second = 1,
}
local function updateCounter()
dz.data.secondsOn = dz.data.secondsOn + ( dz.time.dDate - dz.data.base )
if timeKeeper.count ~= dz.data.secondsOn / timeUnits[myTimeUnit] then
timeKeeper.updateCounter(dz.utils.round(dz.data.secondsOn / timeUnits[myTimeUnit]) )
end
end
--[[
if currentTime == '00:00:00' then
dz.log('time is ' .. currentTime .. ', and the daily filtration timer has been reset.')
timeKeeper.updateCounter('0')
end
]]--
if item.isTimer and mySwitch.active and dz.data.base ~= 0 then
updateCounter()
dz.data.base = dz.time.dDate
dz.data.state = mySwitch.state
elseif item.isDevice and mySwitch.active and dz.data.state ~= 'On' then
dz.data.base = dz.time.dDate
dz.data.state = 'On'
elseif item.isDevice and dz.data.state == 'On' then
updateCounter()
dz.data.state = 'Off'
elseif item.isTimer and dz.data.base == 0 then
dz.data.base = dz.time.dDate
dz.data.state = mySwitch.state
end
dz.log("State ==>> " .. mySwitch.state .. "; Date Time ==>> " .. dz.time.rawDate .." " ..
dz.time.rawTime .. "; " .. myTimeUnit .. "s On today ===>> " .. timeKeeper.counterToday ,dz.LOG_DEBUG)
end
}
Now, apart from the update, I have also very recently added a device, renamed a couple : but the script points to device numbers and those have not changed as far as I can tell... What on earth could be going wrong? If I am not alone, maybe then this is update related? I'm on 2020.1 (but strangely still get the notification to update to latest version). NAS is a DS415+ running on DSM 6.2.2-24922 Update 4 : is up to date, but no recent update.
Many thanks for your input.
Re: Updating incremental counter with TIME value / json
Posted: Wednesday 24 June 2020 9:10
by Ayep
Hi all,
Has anyone been able to work out a fix? The problem with this script occurred with the upgrade to 2020.1. Since then, no luck, the script seems to adding cumulative values and not counting properly.
What could have triggered this behaviour? Any idea of alternative scripts of easy fixes?
Many thanks.
Re: Updating incremental counter with TIME value / json
Posted: Friday 25 December 2020 20:18
by kitopopo
waaren wrote: ↑Tuesday 05 June 2018 10:00
Or with a more realtime approach but still not having to execute every minute..
Code: Select all
--[[ getUptime.lua for [ dzVents >= 2.4 ]
]]--
return {
on = { devices = { nnn }}, -- name enclosed in quotes or IDX number without quotes
logging = { level = domoticz.LOG_DEBUG,
marker = "getUptime" },
data = { lastState = { initial = "Off" },
secondsOn = { initial = 0 },
lastSwitchTime = { initial = "0" }},
execute = function(dz,trigger)
local myTextDevice = dz.devices("hourCounter") -- name enclosed in quotes or number without
-- ( or remove if you don't want textDevice )
local function updateSensor()
if myTextDevice then
local myText = "\nRuntime in hours for device " .. trigger.name .. " ==>> ".. dz.utils.round( tonumber(dz.data.secondsOn) / 3600 )
if myTextDevice.text ~= myText then
myTextDevice.updateText( myText )
end
end
end
if trigger.state ~= dz.data.lastState then
if trigger.state == "Off" then
local Time = require('Time')
t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
t2 = Time(dz.data.lastSwitchTime)
deltaTime = t1.compare(t2).secs
dz.data.secondsOn = dz.data.secondsOn + deltaTime
updateSensor()
end
dz.data.lastState = trigger.state
dz.data.lastSwitchTime = dz.time.rawDate .. " " .. dz.time.rawTime
end
dz.log("State ==>> " .. trigger.state .. "; Date Time ==>> " .. dz.time.rawDate .." " ..
dz.time.rawTime .. "; secondsOn ===>> " .. dz.data.secondsOn ,dz.LOG_DEBUG)
end
}
Dear @waaren,
I have also started using this script to monitor heating minutes daily. I see that the script adds up the total number of minutes the heater is on. I would like to be able to reset this counter 2 times a day for example at 09:00 am and at 21:00 pm and that before resetting the counter receive a telegram with the minutes that the boiler has consumed in the morning and the minutes that the boiler has consumed boiler at night.
no matter how much I think about it, I can't implement this. To reset the counter, I think it would be enough to enter "dz.data.secondsOn = 0", but I can't enter it in the script.
Also I have tried adding a timer and also adding today's date to the data of the script for when the date changes I reset the counter but I can't implement it. I have basic notions in dzvents I have little experience with complex scripts. Could you help me? thanks in advance. Merry Christmas
Re: Updating incremental counter with TIME value / json
Posted: Saturday 26 December 2020 12:26
by waaren
kitopopo wrote: ↑Friday 25 December 2020 20:18
Also I have tried adding a timer and also adding today's date to the data of the script for when the date changes I reset the counter but I can't implement it. I have basic notions in dzvents I have little experience with complex scripts. Could you help me? thanks in advance. Merry Christmas
Maybe something like below
Code: Select all
-- getUptime.lua for [ dzVents >= 2.4 ]
local myDevice = nnn -- name enclosed in quotes or IDX number without quotes
return
{
on =
{
devices =
{
myDevice,
},
timer =
{
'at 09:00',
'at 21:00',
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = "getUptime",
},
data =
{
lastState = { initial = "Off" },
secondsOn = { initial = 0 },
lastSwitchTime = { initial = "0" },
},
execute = function(dz, item)
local myDevice = dz.devices(myDevice)
local myTextDevice = dz.devices("hourCounter") -- name enclosed in quotes or number without
-- ( or remove if you don't want textDevice )
local function updateSensor()
if myTextDevice then
local myText = "\nRuntime in hours for device " .. myDevice.name .. " ==>> ".. dz.utils.round( tonumber(dz.data.secondsOn) / 3600 )
if myTextDevice.text ~= myText then
myTextDevice.updateText( myText )
end
end
end
if myDevice.state ~= dz.data.lastState then
if myDevice.state == "Off" then
local Time = require('Time')
t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
t2 = Time(dz.data.lastSwitchTime)
deltaTime = t1.compare(t2).secs
dz.data.secondsOn = dz.data.secondsOn + deltaTime
updateSensor()
end
dz.data.lastState = myDevice.state
dz.data.lastSwitchTime = dz.time.rawDate .. " " .. dz.time.rawTime
end
local message = "State ==>> " .. myDevice.state .. "; Date Time ==>> " .. dz.time.rawDate .." " ..
dz.time.rawTime .. "; secondsOn ===>> " .. dz.data.secondsOn
dz.log(message ,dz.LOG_DEBUG)
if item.isTimer then
dz.notify('Reset', message )
dz.data.secondsOn = 0
end
end
}
Re: Updating incremental counter with TIME value / json
Posted: Sunday 27 December 2020 17:49
by kitopopo
waaren wrote: ↑Saturday 26 December 2020 12:26
kitopopo wrote: ↑Friday 25 December 2020 20:18
Also I have tried adding a timer and also adding today's date to the data of the script for when the date changes I reset the counter but I can't implement it. I have basic notions in dzvents I have little experience with complex scripts. Could you help me? thanks in advance. Merry Christmas
Maybe something like below
Code: Select all
-- getUptime.lua for [ dzVents >= 2.4 ]
local myDevice = nnn -- name enclosed in quotes or IDX number without quotes
return
{
on =
{
devices =
{
myDevice,
},
timer =
{
'at 09:00',
'at 21:00',
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = "getUptime",
},
data =
{
lastState = { initial = "Off" },
secondsOn = { initial = 0 },
lastSwitchTime = { initial = "0" },
},
execute = function(dz, item)
local myDevice = dz.devices(myDevice)
local myTextDevice = dz.devices("hourCounter") -- name enclosed in quotes or number without
-- ( or remove if you don't want textDevice )
local function updateSensor()
if myTextDevice then
local myText = "\nRuntime in hours for device " .. myDevice.name .. " ==>> ".. dz.utils.round( tonumber(dz.data.secondsOn) / 3600 )
if myTextDevice.text ~= myText then
myTextDevice.updateText( myText )
end
end
end
if myDevice.state ~= dz.data.lastState then
if myDevice.state == "Off" then
local Time = require('Time')
t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
t2 = Time(dz.data.lastSwitchTime)
deltaTime = t1.compare(t2).secs
dz.data.secondsOn = dz.data.secondsOn + deltaTime
updateSensor()
end
dz.data.lastState = myDevice.state
dz.data.lastSwitchTime = dz.time.rawDate .. " " .. dz.time.rawTime
end
local message = "State ==>> " .. myDevice.state .. "; Date Time ==>> " .. dz.time.rawDate .." " ..
dz.time.rawTime .. "; secondsOn ===>> " .. dz.data.secondsOn
dz.log(message ,dz.LOG_DEBUG)
if item.isTimer then
dz.notify('Reset', message )
dz.data.secondsOn = 0
end
end
}
Dear @waaren,
Thanks for your help. I have tested your script. The script works correctly when the heating is off, but if the heating is on, the script doesn't reset the counter.
Maybe first we need update the counter at the trigger time, send the notification and after reset the counter and that the counter could keep counting since to the heating is on. Is possible? Thanks in advanced.
Re: Updating incremental counter with TIME value / json
Posted: Sunday 27 December 2020 23:30
by kitopopo
waaren wrote: ↑Saturday 26 December 2020 12:26
kitopopo wrote: ↑Friday 25 December 2020 20:18
Also I have tried adding a timer and also adding today's date to the data of the script for when the date changes I reset the counter but I can't implement it. I have basic notions in dzvents I have little experience with complex scripts. Could you help me? thanks in advance. Merry Christmas
Maybe something like below
Code: Select all
-- getUptime.lua for [ dzVents >= 2.4 ]
local myDevice = nnn -- name enclosed in quotes or IDX number without quotes
return
{
on =
{
devices =
{
myDevice,
},
timer =
{
'at 09:00',
'at 21:00',
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = "getUptime",
},
data =
{
lastState = { initial = "Off" },
secondsOn = { initial = 0 },
lastSwitchTime = { initial = "0" },
},
execute = function(dz, item)
local myDevice = dz.devices(myDevice)
local myTextDevice = dz.devices("hourCounter") -- name enclosed in quotes or number without
-- ( or remove if you don't want textDevice )
local function updateSensor()
if myTextDevice then
local myText = "\nRuntime in hours for device " .. myDevice.name .. " ==>> ".. dz.utils.round( tonumber(dz.data.secondsOn) / 3600 )
if myTextDevice.text ~= myText then
myTextDevice.updateText( myText )
end
end
end
if myDevice.state ~= dz.data.lastState then
if myDevice.state == "Off" then
local Time = require('Time')
t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
t2 = Time(dz.data.lastSwitchTime)
deltaTime = t1.compare(t2).secs
dz.data.secondsOn = dz.data.secondsOn + deltaTime
updateSensor()
end
dz.data.lastState = myDevice.state
dz.data.lastSwitchTime = dz.time.rawDate .. " " .. dz.time.rawTime
end
local message = "State ==>> " .. myDevice.state .. "; Date Time ==>> " .. dz.time.rawDate .." " ..
dz.time.rawTime .. "; secondsOn ===>> " .. dz.data.secondsOn
dz.log(message ,dz.LOG_DEBUG)
if item.isTimer then
dz.notify('Reset', message )
dz.data.secondsOn = 0
end
end
}
Dear @waaren,
I apologize, I did not test the script well. It works perfectly, the reset is not displayed until the next heater on and off cycle. If the heating is on at the time of reset, the counter continues to count perfectly until it is turned off. You are a teacher, you have helped me a lot. I am very grateful
Re: Updating incremental counter with TIME value / json
Posted: Thursday 01 April 2021 11:32
by 19leva89
Good afternoon, with the help of your code I have set up a Counter for myself, which counts the total working time of light bulbs in the house. Tell me, is it possible to somehow calculate the total energy consumption, and take into account in the code that each light bulb has a different consumption in W?
Re: Updating incremental counter with TIME value / json
Posted: Thursday 01 April 2021 11:49
by waaren
19leva89 wrote: ↑Thursday 01 April 2021 11:32
Good afternoon, with the help of your code I have set up a Counter for myself, which counts the total working time of light bulbs in the house. Tell me, is it possible to somehow calculate the total energy consumption, and take into account in the code that each light bulb has a different consumption in W?
Yes that's possible.
Please share your script between code tags </> or if you cannot find these as plain text. That will help me showing you how it can be done.
Re: Updating incremental counter with TIME value / json
Posted: Thursday 01 April 2021 11:53
by 19leva89
Code: Select all
--[[ getUptime.lua for [ dzVents >= 2.4 ] ]]--
return {
on = { devices = { 260, 350, 366, 368, 370, 369, 371, 74, 88 }}, -- name enclosed in quotes or IDX number without quotes
logging = { level = domoticz.LOG_DEBUG,
marker = "getUptime" },
data = { lastState = { initial = "Off" },
secondsOn = { initial = 0 },
lastSwitchTime = { initial = "0" }},
execute = function(dz,trigger)
local count = dz.devices(418)
local round_to_4 = tonumber(dz.utils.round ((dz.data.secondsOn / 3600),4,4) )
count.updateCounter(round_to_4)
if trigger.state ~= dz.data.lastState then
if trigger.state == "Off" then
local Time = require('Time')
t1 = Time(dz.time.rawDate .." " .. dz.time.rawTime)
t2 = Time(dz.data.lastSwitchTime)
deltaTime = t1.compare(t2).secs
dz.data.secondsOn = dz.data.secondsOn + deltaTime
end
dz.data.lastState = trigger.state
dz.data.lastSwitchTime = dz.time.rawDate .. " " .. dz.time.rawTime
end
dz.log("State ==>> " .. trigger.state .. "; Date Time ==>> " .. dz.time.rawDate .." " ..
dz.time.rawTime .. "; secondsOn ===>> " .. dz.data.secondsOn ,dz.LOG_DEBUG)
end
}
Re: Updating incremental counter with TIME value / json
Posted: Thursday 01 April 2021 15:56
by waaren
19leva89 wrote: ↑Thursday 01 April 2021 11:32
Good afternoon, with the help of your code I have set up a Counter for myself, which counts the total working time of light bulbs in the house. Tell me, is it possible to somehow calculate the total energy consumption, and take into account in the code that each light bulb has a different consumption in W?
A possible solution below and interested in your findings.
Please feel free to ask for clarification.
Code: Select all
--[[ getUptime.lua for [ dzVents >= 2.4 ] ]]--
return
{
on =
{
devices =
{
260, 350, 366, 368, 370, 369, 371, 74, 88,
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = "getUptime",
},
data =
{
devices =
{
initial = {},
},
},
execute = function(dz, item)
--- Your settings below this line
local count = dz.devices(418) -- change to device idx of your total counting device
local usage =
{
-- default is the power for devices not in this table
default = 25,
-- [device idx] = power
[260] = 12,
[350] = 33,
[366] = 66,
[368] = 99,
[370] = 101,
[369] = 112,
[371] = 113,
[74] = 123,
[88] = 133,
}
--- 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)
dz.data.devices[item.name] = dvData
end
}
Re: Updating incremental counter with TIME value / json
Posted: Thursday 01 April 2021 19:33
by 19leva89
Good evening, thanks for the script! He's almost working, he doesn't want to count the seconds.
Code: Select all
2021-04-01 20:14:15.921 Status: User: Admin initiated a switch command (368/Light-Kitchen/On)
2021-04-01 20:14:16.164 Status: dzVents: Info: Handling events for: "Light-Kitchen", value: "On"
2021-04-01 20:14:16.164 Status: dzVents: Info: getUptime: ------ Start internal script: Light-kWh: Device: "Light-Kitchen (Dummy-Sensors)", Index: 368
2021-04-01 20:14:16.166 Status: dzVents: Debug: getUptime: Processing device-adapter for test-power: Counter device adapter
2021-04-01 20:14:16.166 Status: dzVents: Debug: getUptime: Total energy counted over all devices: 0
2021-04-01 20:14:16.166 Status: dzVents: Debug: getUptime: Total seconds counted over all devices: 0
2021-04-01 20:14:16.167 Status: dzVents: Info: getUptime: ------ Finished Light-kWh
2021-04-01 20:14:37.146 Status: User: Admin initiated a switch command (368/Light-Kitchen/Off)
2021-04-01 20:14:37.278 Status: dzVents: Info: Handling events for: "Light-Kitchen", value: "Off"
2021-04-01 20:14:37.278 Status: dzVents: Info: getUptime: ------ Start internal script: Light-kWh: Device: "Light-Kitchen (Dummy-Sensors)", Index: 368
2021-04-01 20:14:37.280 Status: dzVents: Debug: getUptime: Processing device-adapter for test-power: Counter device adapter
2021-04-01 20:14:37.280 Status: dzVents: Debug: getUptime: Total energy counted over all devices: 0
2021-04-01 20:14:37.280 Status: dzVents: Debug: getUptime: Total seconds counted over all devices: 0
2021-04-01 20:14:37.281 Status: dzVents: Info: getUptime: ------ Finished Light-kWh
I am just starting to understand the language of DzVents, is there something in the code in these highlighted places?

- err.jpg (170.24 KiB) Viewed 2288 times
Re: Updating incremental counter with TIME value / json
Posted: Thursday 01 April 2021 19:53
by waaren
19leva89 wrote: ↑Thursday 01 April 2021 19:33
Good evening, thanks for the script! He's almost working, he doesn't want to count the seconds.
Just let it execute for a couple of switches. The first switch will be 0. Only when a device is switched off after a first switch On since the script was active the counter will increase.
the 'or 0' is there to ensure the value will be initialized the first time when the entry is still nil. This is standard Lua coding practice. Not dzVents specific.
The quotes around secondsOn are required because it is a string. Without the quotes it would be a variable.
Re: Updating incremental counter with TIME value / json
Posted: Friday 02 April 2021 8:10
by 19leva89
Today I tried to turn on and off different switches several times, the seconds are still at zero
Code: Select all
2021-04-02 08:54:59.219 Status: dzVents: Info: Handling events for: "Light-Bathroom", value: "Off"
2021-04-02 08:54:59.219 Status: dzVents: Info: getUptime: ------ Start internal script: Light-kWh: Device: "Light-Bathroom (Dummy-Sensors)", Index: 369
2021-04-02 08:54:59.221 Status: dzVents: Debug: getUptime: Processing device-adapter for test-power: Counter device adapter
2021-04-02 08:54:59.221 Status: dzVents: Debug: getUptime: Total energy counted over all devices: 0
2021-04-02 08:54:59.221 Status: dzVents: Debug: getUptime: Total seconds counted over all devices: 0
2021-04-02 08:54:59.222 Status: dzVents: Info: getUptime: ------ Finished Light-kWh
2021-04-02 08:55:02.552 Status: dzVents: Info: Handling events for: "Light-Сorridor", value: "Off"
2021-04-02 08:55:02.552 Status: dzVents: Info: getUptime: ------ Start internal script: Light-kWh: Device: "Light-Сorridor (Dummy-Sensors)", Index: 366
2021-04-02 08:55:02.554 Status: dzVents: Debug: getUptime: Processing device-adapter for test-power: Counter device adapter
2021-04-02 08:55:02.554 Status: dzVents: Debug: getUptime: Total energy counted over all devices: 0
2021-04-02 08:55:02.554 Status: dzVents: Debug: getUptime: Total seconds counted over all devices: 0
2021-04-02 08:55:02.555 Status: dzVents: Info: getUptime: ------ Finished Light-kWh
2021-04-02 08:55:09.298 EventSystem: Event triggered: Light-Bedroom_2
2021-04-02 08:55:09.619 Status: dzVents: Info: Handling events for: "Light-Bedroom", value: "Off"
2021-04-02 08:55:09.619 Status: dzVents: Info: getUptime: ------ Start internal script: Light-kWh: Device: "Light-Bedroom (Zigbee2MQTT+HUB)", Index: 260
2021-04-02 08:55:09.621 Status: dzVents: Debug: getUptime: Processing device-adapter for test-power: Counter device adapter
2021-04-02 08:55:09.621 Status: dzVents: Debug: getUptime: Total energy counted over all devices: 0
2021-04-02 08:55:09.621 Status: dzVents: Debug: getUptime: Total seconds counted over all devices: 0
2021-04-02 08:55:09.622 Status: dzVents: Info: getUptime: ------ Finished Light-kWh
2021-04-02 08:55:12.070 Status: dzVents: Info: Handling events for: "Light-Bathroom", value: "On"
2021-04-02 08:55:12.070 Status: dzVents: Info: getUptime: ------ Start internal script: Light-kWh: Device: "Light-Bathroom (Dummy-Sensors)", Index: 369
2021-04-02 08:55:12.072 Status: dzVents: Debug: getUptime: Processing device-adapter for test-power: Counter device adapter
2021-04-02 08:55:12.072 Status: dzVents: Debug: getUptime: Total energy counted over all devices: 0
2021-04-02 08:55:12.072 Status: dzVents: Debug: getUptime: Total seconds counted over all devices: 0
2021-04-02 08:55:12.073 Status: dzVents: Info: getUptime: ------ Finished Light-kWh
2021-04-02 08:55:46.462 Status: dzVents: Info: Handling events for: "Light-Bathroom", value: "Off"
2021-04-02 08:55:46.462 Status: dzVents: Info: getUptime: ------ Start internal script: Light-kWh: Device: "Light-Bathroom (Dummy-Sensors)", Index: 369
2021-04-02 08:55:46.464 Status: dzVents: Debug: getUptime: Processing device-adapter for test-power: Counter device adapter
2021-04-02 08:55:46.464 Status: dzVents: Debug: getUptime: Total energy counted over all devices: 0
2021-04-02 08:55:46.464 Status: dzVents: Debug: getUptime: Total seconds counted over all devices: 0
2021-04-02 08:55:46.465 Status: dzVents: Info: getUptime: ------ Finished Light-kWh
Re: Updating incremental counter with TIME value / json
Posted: Friday 02 April 2021 12:26
by waaren
19leva89 wrote: ↑Friday 02 April 2021 8:10
Today I tried to turn on and off different switches several times, the seconds are still at zero
Tested on my system and works as expected and without issues
Can you share the script as you have it now between </> tags? Then I will add some debug lines to help finding where the differences between our setups are.