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.

24h Temperature average
#1
Hello there

I've been trying to find a good solution to determine the average temperature of the day. My idea was it to trigger a script whenever a new value is being sent. I would then add the value to a variable and divide it by the number of values. Shortly befor midnight I would then store the value and clear counter and sum. 

I also looked a the log option, but I get the impression that I have very few log entries of any group address that I log. 

How could I achieve that? If you can point me to a similar code sample that be very helpful.

Thanks a lot!

Cheers!
Martin
Reply
#2
Hi Martin,

I made the moving average in my solution as following:

1. Create object / group address for the average value (example: 49/0/1)
2. Create time triggered script with 5min cycle time
3. Use the following code inside the script and adjust the value group address:



Code:
average=grp.getvalue('49/0/1')
value=grp.getvalue('49/0/2') —- value that should be averaged
average = (average *((24*60/5)-1) + value)/(24*60/5)
grp.checkwrite('49/0/1', average)

I hope this is working for you!

Best Regards 
Steffen
Reply
#3
https://forum.logicmachine.net/showthrea...7#pid17347
------------------------------
Ctrl+F5
Reply
#4
Another option is to use storage.

1. Event script to accumulate the values:
Code:
value = event.getvalue()
key = 'average'

storage.exec('incrbyfloat', key .. ':acc', value)
storage.exec('incr', key .. ':cnt')

2. Scheduled script to calculate the average and reset the accumulated values:
Code:
key = 'average'
acc = storage.get(key .. ':acc', 0)
cnt = storage.get(key .. ':cnt', 0)

avg = 0
if cnt > 0 then
  avg = acc / cnt
end

grp.update('1/1/1', avg)

storage.delete(key .. ':acc')
storage.delete(key .. ':cnt')
Reply
#5
(18.09.2022, 18:23)Hi SteffenThanks a lot, this looks like a neat solution. I shall try it this weekend and report.Cheers!Martinpioneersteffen Wrote: Hi Martin,

I made the moving average in my solution as following:

1. Create object / group address for the average value (example: 49/0/1)
2. Create time triggered script with 5min cycle time
3. Use the following code inside the script and adjust the value group address:



Code:
average=grp.getvalue('49/0/1')
value=grp.getvalue('49/0/2') —- value that should be averaged
average = (average *((24*60/5)-1) + value)/(24*60/5)
grp.checkwrite('49/0/1', average)

I hope this is working for you!

Best Regards 
Steffen
Reply
#6
(19.09.2022, 07:28)Daniel Wrote: https://forum.logicmachine.net/showthrea...7#pid17347

thanks Daniel for the pointer.

(19.09.2022, 08:15)admin Wrote: Another option is to use storage.

1. Event script to accumulate the values:
Code:
value = event.getvalue()
key = 'average'

storage.exec('incrbyfloat', key .. ':acc', value)
storage.exec('incr', key .. ':cnt')

2. Scheduled script to calculate the average and reset the accumulated values:
Code:
key = 'average'
acc = storage.get(key .. ':acc', 0)
cnt = storage.get(key .. ':cnt', 0)

avg = 0
if cnt > 0 then
  avg = acc / cnt
end

grp.update('1/1/1', avg)

storage.delete(key .. ':acc')
storage.delete(key .. ':cnt')

thank you very much, I shall try this too.
Reply


Forum Jump: