04.01.2023, 08:54
(03.01.2023, 08:58)Daniel Wrote: Resident/Scheduled inside the if.
it would be like this ??
--------------------------------------Common functions ------------------------
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
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
grp.update(output, res)
end
---------------------------------------Scheduled-------------------
Code:
value = event.getvalue()
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)
storage.delete(key_curr)
end
grp.update(out_curr, '')
end
-------------------------------- Resident--------------------------
Code:
key_curr = 'ontime_curr'
out_curr = '33/1/2' -- Time on Grupo Electrogeno Grupo Electrogeno
key_total = 'ontime_total'
out_total = '33/1/1' -- Time on Grupo Electrogeno Grupo Electrogeno
time_total = '33/1/3' --- total hours to deduct
time_curr = storage.get(key_curr)
if time_curr then
time_curr = os.time() - time_curr
formattime(out_curr, time_curr)
time_total = 22 * 3600 -- 22 hours in seconds
time_remaining = math.max(0, time_total - time_curr)
formattimealt(out_total, time_remaining)
end
Thank you