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
(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)?
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:
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'

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')
elseif               -- <------------ and this to elseif
         storage.get(ontime_Laptop_Seat) then        --  <------------i add this line
  Last_Run = grp.getvalue(output_Laptop_Seat)
  time =' %a, %d-%m',time)
  grp.update(output_Laptop_Seat, 'Last Run: '..Last_Run..' on'..time)
Modified script version that has total and current running time.
1. Add to Common functions:
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'

  if hours > 0 then
    res[ #res + 1 ] = hours .. 'h'

  if minutes > 0 then
    res[ #res + 1 ] = minutes .. 'm'

  if seconds > 0 or #res == 0 then
    res[ #res + 1 ] = seconds .. 's'

  res = table.concat(res, ' ')
  grp.update(output, res)

2. Event script mapped to load on/off status:
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)
  if time then
    time_total = storage.exec('incrby', key_total, now - time)
    formattime(out_total, time_total)

  grp.update(out_curr, '')

3. Scheduled script that runs every minute (or a resident script with sleep time > 0 if finer time resolution is needed):
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)

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.

Forum Jump: