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.

Comparing value with average tendence
#1
I am monitoring the temperature in a device, and I should set an alarm if this temperature varies more than X degrees (variation) in last 4 days. 
So, (for example every hour) I have to compare the "actual temperature" with the "average temperature in last 4 days". 
But some times, the device is switched off, so its temperature is 0, and I don't want to use this value to calculate the average temperature.

To calculate the average temp, I think I should use something like:



If device if ON, then
average_temp = (average_temp + actual_temp) / 2
else
average_temp = average_temp


I should save the average_temp as a variable so the script knows its value everytime.

Later, I will have to compare the average_temp with the actual_temp and the variation_value and, if needed, start an alarm.

Can someone help me with the LUA programming?

Thanks!
Reply
#2
I don't get why you have 0 when your device is off but here is script to calculate average by using logs
https://forum.logicmachine.net/showthrea...7#pid17347
------------------------------
Ctrl+F5
Reply
#3
(25.03.2021, 15:17)Daniel. Wrote: I don't get why you have 0 when your device is off but here is script to calculate average by using logs
https://forum.logicmachine.net/showthrea...7#pid17347

It's not that I get 0. It's that I only want to consider the temp values when device is ON.

Thanks for the reply Smile
Reply
#4
I'm guessing that your device will not send any temp value when it is off. If this is the case then log will log values only which were sent. If not then you can make simple script which will write temp values to another virtual objects only when your device is on. You then use the average script on your virtual object.
------------------------------
Ctrl+F5
Reply
#5
As Daniel said you need to enable logging for this object and use data from the objects logs. Make sure that the object log size (Utilities > General configuration) is large enough to store data for the last 4 days.

Since you want to check for temperature variation you probably need to find the difference between the minimum and maximum temperature in the given period instead of using average. Here's how you can find min/max:
Code:
logaddress = '32/1/7'
obj = grp.find(logaddress)
time = os.time() - 86400 * 4 -- last 4 days

logs = db:getall('SELECT * FROM objectlog WHERE address=? AND logtime>?', obj.id, time)

for _, item in ipairs(logs) do
  value = busdatatype.decode(item.datahex, obj.datatype)
  min = min and math.min(min, value) or value
  max = max and math.max(max, value) or value
end

log(min, max)
Reply


Forum Jump: