Logic Machine Forum
24h Temperature average - 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: 24h Temperature average (/showthread.php?tid=4244)



24h Temperature average - kropfm - 17.09.2022

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


RE: 24h Temperature average - pioneersteffen - 18.09.2022

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


RE: 24h Temperature average - Daniel - 19.09.2022

https://forum.logicmachine.net/showthread.php?tid=2696&pid=17347#pid17347


RE: 24h Temperature average - admin - 19.09.2022

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')



RE: 24h Temperature average - kropfm - 21.09.2022

(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



RE: 24h Temperature average - kropfm - 22.09.2022

(19.09.2022, 07:28)Daniel Wrote: https://forum.logicmachine.net/showthread.php?tid=2696&pid=17347#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.