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.

Average power for the last clock hour
#1
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
Reply
#2
I think I have one of those.. we'll talk tomorrow ?
Reply
#3
If there is more than a screenshot of half a script, then ok. Wink

No, wait, I have KNX Basic course this week.
Reply
#4
Why not use Trends for this?
Reply
#5
I agree with Admin, a nice trendview would give you what you need.
Reply
#6
Uhm.. Because I want it as a value in my other user interface. :|
Reply
#7
You can get values from trends and write them to objects: https://openrb.com/docs/trends-new.htm
Reply
#8
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?
Reply
#9
you can use 'Scheduled' en put the scipt there.
Set running for every hour at 00 minutes
Reply
#10
(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?
Reply
#11
See this: https://forum.logicmachine.net/showthrea...1#pid27441
Reply
#12
Event script:

Code:
12345678910111213141516171819
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:
12345678910111213141516171819202122
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
Reply


Forum Jump: