This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Display time of object
#21
(29.12.2021, 16:04)admin Wrote: Make sure you use a scheduled script that runs every minute not a resident script with low or 0 sleep time.

I did not understand, I used your scrap, I should change something there (in scrap)?
Reply
#22
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:
   
Reply
#23
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'

Code:
Laptop_Seat = event.getvalue()            --'0 WCD Woonkamer - laptoplader Seat (tm)'
ontime_Laptop_Seat = 'ontime_Laptop_Seat'
output_Laptop_Seat = '32/1/202'            --'0 WCD Woonkamer - laptoplader Seat [timer on]'

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
Reply
#24
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 = {}

  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.
Reply
#25
Hi.

How can i reset the total timer?

Br
MR
Reply
#26
Use this to reset the total timer:
Code:
key_total = 'ontime_total'
out_total = '32/1/16'

storage.delete(key_total)
formattime(out_total, 0)
Reply
#27
Thanks alot.

I have another problem you maybe can help me with.

If i have two time stamps and i want to get the difference between them.

Br
MR
Reply
#28
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
 
  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) -- negative placement
end





Hello, it counts backwards but it doesn't keep the value that I have wrong? Could you put the hours to be subtracted in a group address?
Thank you

Attached Files Thumbnail(s)
   
Reply
#29
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

  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

If you have a fixed total run time then you can use this:
Code:
time_total = 22 * 3600 -- 22 hours in seconds
time_remaining = math.max(0, time_total - time_curr)
  
formattimealt(out_total, time_remaining)
Reply
#30
(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

  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

If you have a fixed total run time then you can use this:
Code:
time_total = 22 * 3600 -- 22 hours in seconds
time_remaining = math.max(0, time_total - time_curr)
 
formattimealt(out_total, time_remaining)

hello, in which part of the script would it go? I'm lost, one is for events and the other for residents?
Thank you
Reply
#31
Resident/Scheduled inside the if.
------------------------------
Ctrl+F5
Reply
#32
(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
Reply
#33
Second script is event not scheduled.
Reply
#34
(04.01.2023, 08:56)admin Wrote: Second script is event not scheduled.

Hi, I don't understand what you mean, I'm very new to lua Sad Thank you
Reply
#35
The second script starting with "value = event.getvalue()" must be an event script (script that is triggered by an object value change).
   
Reply
#36
(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
 
  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)
 
  formattime(out_total, time_remaining)
 
 
  end

Thank you
Reply
#37
There is no way I tried according to indications and it doesn't work for me ;(
Reply
#38
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..
Reply
#39
(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

Attached Files Thumbnail(s)
   
Reply
#40
If 3/0/10 contains the current fuel level you can calculate the remaining time based on that using an event script.
Reply


Forum Jump: