This solution consists of two scripts:
1. Event script which saves the time when the load has been turned on
2. Scheduled script that calculates the run time every minute
If you say that you get high CPU load then check that the second script is not a resident script with 0 sleep time.
This is how the scheduled script should be configured:
30.12.2021, 13:24 (This post was last modified: 30.12.2021, 13:26 by Dré.)
i change my event based script (a little), i had the problem, when i again get a off on my event, i got my value twice or more 'Last Run: '..Last_Run..' on'..time
so on my visualization i saw 'Last Run: Last Run: Last Run: 00:06 on Thu, 30-12 on Thu, 30-12 on Thu, 30-12'
if Laptop_Seat then
if not storage.get(ontime_Laptop_Seat) then
storage.set(ontime_Laptop_Seat, os.time())
grp.update(output_Laptop_Seat, '00:00')
end
elseif -- <------------ and this to elseif
storage.get(ontime_Laptop_Seat) then -- <------------i add this line
Last_Run = grp.getvalue(output_Laptop_Seat)
time = os.date(' %a, %d-%m',time)
grp.update(output_Laptop_Seat, 'Last Run: '..Last_Run..' on'..time)
storage.delete(ontime_Laptop_Seat)
end
Modified script version that has total and current running time.
1. Add to Common functions:
Code:
function formattime(output, seconds)
local minutes = math.floor(seconds / 60)
local hours = math.floor(minutes / 60)
local days = math.floor(hours / 24)
local res = {}
if days > 0 then
res[ #res + 1 ] = days .. 'd'
end
if hours > 0 then
res[ #res + 1 ] = hours .. 'h'
end
if minutes > 0 then
res[ #res + 1 ] = minutes .. 'm'
end
if seconds > 0 or #res == 0 then
res[ #res + 1 ] = seconds .. 's'
end
res = table.concat(res, ' ')
grp.update(output, res)
end
2. Event script mapped to load on/off status:
Code:
value = event.getvalue()
key_curr = 'ontime_curr'
out_curr = '32/1/5'
key_total = 'ontime_total'
out_total = '32/1/16'
time = storage.get(key_curr)
now = os.time()
if value then
if not time then
storage.set(key_curr, now)
formattime(out_curr, 0)
end
else
if time then
time_total = storage.exec('incrby', key_total, now - time)
formattime(out_total, time_total)
storage.delete(key_curr)
end
grp.update(out_curr, '')
end
3. Scheduled script that runs every minute (or a resident script with sleep time > 0 if finer time resolution is needed):
Code:
key_curr = 'ontime_curr'
out_curr = '32/1/5'
key_total = 'ontime_total'
out_total = '32/1/16'
time_curr = storage.get(key_curr)
if time_curr then
time_total = storage.get(key_total, 0)
formattime(out_total, time_total + time_curr)
end
In both scripts key_curr, out_curr, key_total, out_total variables should be adjusted as needed. Make sure that these variables have the same values in both scripts.
01.01.2023, 18:31 (This post was last modified: 01.01.2023, 18:53 by nmedalacabeza.)
Code:
--------------------------------------------------------------------------
value = event.getvalue()
key_curr = 'ontime_curr'
out_curr = '33/1/31'
key_total = 'ontime_total'
out_total = '33/1/30'
time = storage.get(key_curr)
now = os.time()
if value then
if not time then
storage.set(key_curr, now)
formattime(out_curr, 0)
end
else
if time then
time_total = storage.exec('incrby', key_total, now - time)
formattime(out_total, time_total)
storage.delete(key_curr)
end
grp.update(out_curr, '')
end
---- ---- resident-------------------------------
key_curr = 'ontime_curr'
out_curr = '33/1/31'
key_total = 'ontime_total'
out_total = '33/1/30'
time_curr = storage.get(key_curr)
if time_curr then
Here's a different formatting function that will only show the largest units for the time (days, hours, minutes, seconds):
Code:
function formattimealt(output, seconds)
local minutes = math.floor(seconds / 60)
local hours = math.floor(minutes / 60)
local days = math.floor(hours / 24)
local res
(02.01.2023, 08:47)admin Wrote: Here's a different formatting function that will only show the largest units for the time (days, hours, minutes, seconds):
Code:
function formattimealt(output, seconds)
local minutes = math.floor(seconds / 60)
local hours = math.floor(minutes / 60)
local days = math.floor(hours / 24)
local res
function formattimealt(output, seconds)
local minutes = math.floor(seconds / 60)
local hours = math.floor(minutes / 60)
local days = math.floor(hours / 24)
local res
seconds = seconds % 60
minutes = minutes % 60
hours = hours % 24
if days > 0 then
res = days .. 'd'
elseif hours > 0 then
res = hours .. 'h'
elseif minutes > 0 then
res = minutes .. 'm'
else
res = seconds .. 's'
end
key_curr = 'ontime_curr'
out_curr = '33/1/2' -- Time on Grupo Electrogeno
key_total = 'ontime_total'
out_total = '33/1/1' -- Total on time de Encendido Grupo Electrogeno
time_total = '33/1/3' --- total hours to deduct
time = storage.get(key_curr)
now = os.time()
if value then
if not time then
storage.set(key_curr, now)
formattime(out_curr, 0)
end
else
if time then
time_total = storage.exec('incrby', key_total, now - time)
formattime(out_total, time_total)
(05.01.2023, 08:13)admin Wrote: The second script starting with "value = event.getvalue()" must be an event script (script that is triggered by an object value change).
With your indications it works more or less for me;(, group time counter is set to 0, but the one that would have to be maintained and continue subtracting also goes to 0
-
Code:
value = event.getvalue('32/7/9')
key_curr = 'ontime_curr'
out_curr = '33/1/30 ' -- Time on Grupo Electrogeno
key_total = 'ontime_total'
out_total = '33/1/31' -- Total on time de Encendido Grupo Electrogeno
time = storage.get(key_curr)
now = os.time()
if value then
if not time then
storage.set(key_curr, now)
formattime(out_curr, 0)
end
else
if time then
time_total = storage.exec('incrby', key_total)
storage.delete(key_curr)
end
grp.update(out_curr, '')
end
-
Code:
key_curr = 'ontime_curr'
out_curr = '33/1/30'
key_total = 'ontime_total'
out_total = '33/1/31'
time_curr = storage.get(key_curr)
if time_curr then
For this to work correctly some kind of external reset is also needed when the generator is fueled again. The easiest solution is to have a reset button on the visualization so the counter starts again. But the best approach would be to have a known fuel level in the generator so the process is fully automated..
(23.01.2023, 08:16)administración Wrote: Para que esto funcione correctamente, también se necesita algún tipo de reinicio externo cuando el generador se alimenta nuevamente. La solución más sencilla es tener un botón de reinicio en la visualización para que el contador comience de nuevo. Pero el mejor enfoque sería tener un nivel de combustible conocido en el generador para que el proceso esté completamente automatizado.
This is how I have assigned the group addresses for its operation, is this what you mean? Thanks