Logic Machine Forum
Comparing value with average tendence - 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: Comparing value with average tendence (/showthread.php?tid=3256)



Comparing value with average tendence - jmedrano - 25.03.2021

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!


RE: Comparing value with average tendence - Daniel - 25.03.2021

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/showthread.php?tid=2696&pid=17347#pid17347


RE: Comparing value with average tendence - jmedrano - 25.03.2021

(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/showthread.php?tid=2696&pid=17347#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


RE: Comparing value with average tendence - Daniel - 25.03.2021

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.


RE: Comparing value with average tendence - admin - 26.03.2021

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)