Posts: 139
Threads: 44
Joined: Dec 2017
Reputation:
4
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: function chk_off()
t ={}
for i = 1, 20 do
addr = '1/1/'.. tostring(i)
obj = grp.find(addr)
if obj and obj.value == 0 then
--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 } )
end
end
return t
end
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: function load_time_off()
t = chk_off()
if t then
for k, v in ipairs(t) do
log(v.on_time) -- this gives me all the on_time vals
-- how to get highest on_time value?? and switch on the associated address
end
end
end
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
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.
Code: max_index = 0
max_time = 0
for index, item in ipairs(t) do
if item.on_time > max_time then
max_index = index
max_time = item.on_time
end
end
max_item = t[ max_index ]
if max_item then
log(max_item)
end
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: table.sort(t, function(a, b)
return a.on_time > b.on_time
end)
log(t)
Posts: 139
Threads: 44
Joined: Dec 2017
Reputation:
4
(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.
Code: max_index = 0
max_time = 0
for index, item in ipairs(t) do
if item.on_time > max_time then
max_index = index
max_time = item.on_time
end
end
max_item = t[ max_index ]
if max_item then
log(max_item)
end
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: table.sort(t, function(a, b)
return a.on_time > b.on_time
end)
log(t)
Many Thanks, i have it working
Posts: 133
Threads: 19
Joined: Apr 2018
Reputation:
0
Hello Admin,
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?
Code: --Actual Lux Values
lux_rest = grp.tag('Lux_Rest')
lux_hal1 = grp.tag('Lux_H1')
lux_hal2 = grp.tag('Lux_H2')
lux_hal3 = grp.tag('Lux_H3')
lux_hal4 = grp.tag('Lux_H4')
lux_hal5 = grp.tag('Lux_H5')
lux_hal6 = grp.tag('Lux_H6')
lux_hal7 = grp.tag('Lux_H7')
-----------MAX LUX VALUE--------------
-- Calculate Max Lux Value in Zone Under Restaurant
t0 = {}
for i, v in ipairs(lux_rest) do
table.insert(t0, v.value)
end
if t0 then
table.sort(t0)
local max = t0[#t0]
grp.checkwrite('3/0/0', max, 1)
end
os.sleep(0.1)
-- Calculate Max Lux Value in Hal 1
t1 = {}
for i, v in ipairs(lux_hal1) do
table.insert(t1, v.value)
end
if t1 then
table.sort(t1)
local max = t1[#t1]
grp.checkwrite('3/0/10', max, 1)
end
os.sleep(0.1)
-- Calculate Max Lux Value in Hal 2
t2 = {}
for i, v in ipairs(lux_hal2) do
table.insert(t2, v.value)
end
if t2 then
table.sort(t2)
local max = t2[#t2]
grp.checkwrite('3/0/20', max, 1)
end
os.sleep(0.1)
-- Calculate Max Lux Value in Hal 3
t3 = {}
for i, v in ipairs(lux_hal3) do
table.insert(t3, v.value)
end
if t3 then
table.sort(t3)
local max = t3[#t3]
grp.checkwrite('3/0/30', max, 1)
end
os.sleep(0.1)
-- Calculate Max Lux Value in Hal 4
t4 = {}
for i, v in ipairs(lux_hal4) do
table.insert(t4, v.value)
end
if t4 then
table.sort(t4)
local max = t4[#t4]
grp.checkwrite('3/0/40', max, 1)
end
os.sleep(0.1)
-- Calculate Max Lux Value in Hal 5
t5 = {}
for i, v in ipairs(lux_hal5) do
table.insert(t5, v.value)
end
if t5 then
table.sort(t5)
local max = t5[#t5]
grp.checkwrite('3/0/50', max, 1)
end
os.sleep(0.1)
-- Calculate Max Lux Value in Hal 6
t6 = {}
for i, v in ipairs(lux_hal6) do
table.insert(t6, v.value)
end
if t6 then
table.sort(t6)
local max = t6[#t6]
grp.checkwrite('3/0/60', max, 1)
end
os.sleep(0.1)
-- Calculate Max Lux Value in Hal 7
t7 = {}
for i, v in ipairs(lux_hal7) do
table.insert(t7, v.value)
end
if t7 then
table.sort(t7)
local max = t7[#t7]
grp.checkwrite('3/0/70', max, 1)
end
Kind regards
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
Try this:
Code: tags = {
{ tag = 'tag1', output = '1/1/1' },
{ tag = 'tag2', output = '2/1/1' },
}
function tag_max(tag)
local objects = grp.tag(tag)
local result = -math.huge
for _, object in ipairs(objects) do
result = math.max(result, object.value)
end
return result
end
for _, t in ipairs(tags) do
max = tag_max(t.tag)
grp.checkwrite(t.output, max)
os.sleep(0.1)
end
Posts: 133
Threads: 19
Joined: Apr 2018
Reputation:
0
08.04.2024, 14:32
(This post was last modified: 08.04.2024, 14:43 by manos@dynamitec.)
(08.04.2024, 14:07)admin Wrote: Try this:
Code: tags = {
{ tag = 'tag1', output = '1/1/1' },
{ tag = 'tag2', output = '2/1/1' },
}
function tag_max(tag)
local objects = grp.tag(tag)
local result = -math.huge
for _, object in ipairs(objects) do
result = math.max(result, object.value)
end
return result
end
for _, t in ipairs(tags) do
max = tag_max(t.tag)
grp.checkwrite(t.output, max)
os.sleep(0.1)
end
Thank you Admin. Works as expected .
If I want to also calculate the min value in the same script and write on diff object?
|