Logic Machine Forum
Average power for the last clock hour - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Average power for the last clock hour (/showthread.php?tid=4355)



Average power for the last clock hour - Rune - 06.11.2022

Hi guys!

Part of the energy cost in Norway is defined by the three hours in the month were the user spent the most power.

Can someone help me with a script that calculate the average power of the last clock hour? It would be nice to have that to confirm if I exceeded my 5kW limit or not. Smile


RE: Average power for the last clock hour - tomnord - 06.11.2022

I think I have one of those.. we'll talk tomorrow ?


RE: Average power for the last clock hour - Rune - 06.11.2022

If there is more than a screenshot of half a script, then ok. Wink

No, wait, I have KNX Basic course this week.


RE: Average power for the last clock hour - admin - 07.11.2022

Why not use Trends for this?


RE: Average power for the last clock hour - tomnord - 07.11.2022

I agree with Admin, a nice trendview would give you what you need.


RE: Average power for the last clock hour - Rune - 07.11.2022

Uhm.. Because I want it as a value in my other user interface. :|


RE: Average power for the last clock hour - admin - 08.11.2022

You can get values from trends and write them to objects: https://openrb.com/docs/trends-new.htm


RE: Average power for the last clock hour - Rune - 12.11.2022

I couldn't understand how to transform that to a script that could be useful, so I googled lua average value and found this in another thread:

-- storage key
key = 'temp_average'
-- time between average value send (in seconds)
maxtime = 60 * 60
-- resulting average value
result = '4/3/34'

time = os.time()

data = storage.get(key)
if not data then
  data = { values = {}, time = time }
end

value = event.getvalue()
table.insert(data.values, value)

delta = time - data.time
if delta >= maxtime or delta < 0 then
  storage.delete(key)

  avg = 0
  count = #data.values
  for _, value in ipairs(data.values) do
    avg = avg + value
  end

  grp.write(result, avg / count)
else
  storage.set(key, data)
end

Is there a way to make this start the count at the beginning of the clock hour?


RE: Average power for the last clock hour - Dré - 13.11.2022

you can use 'Scheduled' en put the scipt there.
Set running for every hour at 00 minutes


RE: Average power for the last clock hour - Rune - 13.11.2022

(13.11.2022, 12:18)Dré Wrote: you can use 'Scheduled' en put the scipt there.
Set running for every hour at 00 minutes

But if I do that, since it is a event script today triggered by "4/6/1", how do I insert that GA into the script before I change it to a sceduled script?


RE: Average power for the last clock hour - admin - 13.11.2022

See this: https://forum.logicmachine.net/showthread.php?tid=4244&pid=27441#pid27441


RE: Average power for the last clock hour - Rune - 14.11.2022

Event script:

Code:
local st_name = 'average' -- Local table name
local value_in = '4/6/1' -- GA data input



local st_table = storage.get(st_name)
if not st_table then
  log('Table Created')
  local st_values = {}
  storage.set(st_name, st_values)
end



table.insert(st_table, grp.getvalue(value_in))



storage.set(st_name, st_table)

Scheduled script sent every 0 minute:
Code:
local st_name = 'average' -- Name on local table
local result_avg = '4/3/34' -- GA for result output



local st_table = storage.get(st_name)
if st_table then
  local size = 0
  local sum_val = 0
  for i, v in pairs(st_table) do
    sum_val = v + sum_val
    size = size + 1
  end
 
  local avg_val = sum_val / size
  grp.write(result_avg, avg_val)
  local reset_table = {}
  log(avg_val)
  log(size)
  storage.set(st_name, reset_table)
  log(storage.get(st_name))
end