23.06.2020, 05:46 (This post was last modified: 23.06.2020, 05:49 by benanderson_475.)
I want to get the highest time and then switch the groups on in the order from highest time(stored in on_time) to lowest time until all the groups are on, groups/ range i am interested in are 1/1/1 to 1/1/20
i am storing the obj.updatetime in a table with the associated address etc as below
Code:
1234567891011121314
functionchk_off()
t ={}
fori = 1, 20doaddr = '1/1/'.. tostring(i)
obj = grp.find(addr)
ifobjandobj.value == 0then--put all off obj in table with update time table.insert(t, {address = addr, level = obj.value, upd_time = obj.updatetime, on_time = os.time() -obj.updatetime } )
endendreturntend
Then i want to get the highest value from all the t.on_time/s and switch each off, in order from the longest off value (stored in on_time) until all the complete range 1/1/1 - 1/1/20 are all off
this runs from a function which is called from a scheduled script every x minutes.
This is where i am stuck, this is what i have so far, my question is how can i calculate the highest on_time value and what will happen if two possibility have the same on_time value?
I hope this is making sense....
Code:
123456789
functionload_time_off()
t = chk_off()
iftthenfork, vinipairs(t) dolog(v.on_time) -- this gives me all the on_time vals-- how to get highest on_time value?? and switch on the associated addressendendend
Or you can sort the table using custom comparison function. In this case the order of elements in not defined if on_time is the same. t[1] will contain the element with maximum on_time value.
Code:
12345
table.sort(t, function(a, b)
returna.on_time > b.on_timeend)
log(t)
(23.06.2020, 06:28)admin Wrote: You can find the maximum value like this. If two items have the same on_time then the one that appears first in the table will be returned.
Or you can sort the table using custom comparison function. In this case the order of elements in not defined if on_time is the same. t[1] will contain the element with maximum on_time value.
Code:
12345
table.sort(t, function(a, b)
returna.on_time > b.on_timeend)
log(t)
I am using the following to find max value of multiple tables with multiple tagged objects. Now I am repeating the same calculation many times in a resident script; how can I achieve the same but with something like a table containing multiple tables in and run the for loop once per table?