LogicMachine Forum
Display time of object - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: OLD visualization (https://forum.logicmachine.net/forumdisplay.php?fid=9)
+--- Thread: Display time of object (/showthread.php?tid=3769)

Pages: 1 2 3


RE: Display time of object - Nir70 - 29.12.2021

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


RE: Display time of object - admin - 29.12.2021

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:
   


RE: Display time of object - Dré - 30.12.2021

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



RE: Display time of object - admin - 05.04.2022

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.


RE: Display time of object - Rauschentofft - 03.11.2022

Hi.

How can i reset the total timer?

Br
MR


RE: Display time of object - admin - 04.11.2022

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)



RE: Display time of object - Rauschentofft - 04.11.2022

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


RE: Display time of object - nmedalacabeza - 01.01.2023

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


RE: Display time of object - admin - 02.01.2023

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)



RE: Display time of object - nmedalacabeza - 02.01.2023

(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


RE: Display time of object - Daniel - 03.01.2023

Resident/Scheduled inside the if.


RE: Display time of object - nmedalacabeza - 04.01.2023

(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


RE: Display time of object - admin - 04.01.2023

Second script is event not scheduled.


RE: Display time of object - nmedalacabeza - 04.01.2023

(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


RE: Display time of object - admin - 05.01.2023

The second script starting with "value = event.getvalue()" must be an event script (script that is triggered by an object value change).
   


RE: Display time of object - nmedalacabeza - 07.01.2023

(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


RE: Display time of object - nmedalacabeza - 21.01.2023

There is no way I tried according to indications and it doesn't work for me ;(


RE: Display time of object - admin - 23.01.2023

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..


RE: Display time of object - nmedalacabeza - 23.01.2023

(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


RE: Display time of object - admin - 23.01.2023

If 3/0/10 contains the current fuel level you can calculate the remaining time based on that using an event script.