05.04.2022, 13:02
Modified script version that has total and current running time.
1. Add to Common functions:
2. Event script mapped to load on/off status:
3. Scheduled script that runs every minute (or a resident script with sleep time > 0 if finer time resolution is needed):
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.
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 = {}
seconds = seconds % 60
minutes = minutes % 60
hours = hours % 24
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_curr = os.time() - time_curr
formattime(out_curr, time_curr)
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.