Page 1 of 1

different ways to determine average temperature ?

Posted: Thursday 15 September 2022 20:42
by willemd
What is the best way to determine average temperature of a day in dzVents, recorded by a temperature sensor? What is most efficient, with lowest system load?

Method 1: Currently I have one virtual sensor with the addition of all temperatures of a day, and another sensor with the count of the number of temperatures. I then divide one by the other and store the result on a third sensor as avg. At the end of the day a reset is done.

Method 2: I could do the same with user variables.

Method 3: Or with persistent variables.

Method 4: Or with historic variables as show in the example in the dzVents documentation, but there is a warning in the documentation about possible system load with historic variables.

Method 5: I saw that Domoticz is anyway already calculating the average and storing it in the Temperature_Calendar, so I could run a SQL query from the command line to find that value. Seems very efficient although disadvantage here is that this can only be done the next day, not during the current day.

Any comments? Any other ways to do this?

The script for method 5 is as follows. It works.

Code: Select all

local avgtemp='avgtemp'
return {
	on = {
		timer = {
			'every minute'  -- to be adjusted, should be done once per day, timing to be determined
		},
		shellCommandResponses =
        {
            avgtemp,
        },
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'avg tmp sql',
	},
	execute = function(domoticz, item)
	    if item.isTimer then
    		local yesterday= domoticz.time.addDays(-1).rawDate
    		domoticz.executeShellCommand({
    		    -- 4 is the idx of the temperature sensor
    		    command = 'sqlite3 /home/pi/domoticz/domoticz.db \"select Temp_Avg from Temperature_Calendar  where DeviceRowId=4 and Date=\'' .. yesterday .. '\'\"',
                callback = avgtemp,
                timeout = 5,
            })
        else 
            domoticz.log('******  avgtemp found in database : ' .. tonumber(item.data) .. ' ***********', domoticz.LOG_INFO)
        end
	end
}

Re: different ways to determine average temperature ?

Posted: Friday 16 September 2022 11:02
by Pjedr
Did that years ago, also for humidity, wanted to calculate when i need to water the plans based on temp and hum, some code:

Code: Select all

#!/bin/bash
DomoServer="nnn.nnn.nnn.nn:8080"; strUrl="http://$DomoServer/json.htm?type=graph&sensor=temp&idx=52&range=day" # TempVochtBaro Binnen
#strCurl=`nice -n 10 curl -s -i -m 5 -H"Accept: application/json" "$strUrl"`; echo "strCurl=" $strCurl; exit 0
today=`date +%Y-%m-%d`; teller=0; metingen=0; tempsom=0; tempgem=0; humsom=0; humgem=0
for i in `curl -s "$strUrl" | jq -r '.result[]| .te, .hu, .d'`; do
 teller=$((teller+1)); echo $teller": i="$i
 if [ $teller == 1 ]; then temp=$i;  elif [ $teller == 2 ]; then hum=$i;  elif [ $teller == 3 ]; then date=$i
 elif [ $teller == 4 ]; then time=$i; teller=0;   if [ $today == $date ]; then
   metingen=$((metingen+1)); tempsom=`echo $tempsom + $temp | bc`; tempgem=`echo "scale=2; $tempsom / $metingen" | bc`
   humsom=`echo $humsom + $hum | bc`; humgem=`echo "scale=2; $humsom / $metingen" | bc`
   #echo meting $metingen: date=$date $time temp=$temp hum=$hum tempsom=$tempsom tempgem=$tempgem humsom=$humsom humgem=$humgem
  fi
 fi
done
echo temp vandaag $today $metingen metingen: totaal=$tempsom gemiddeld=$tempgem graden
echo hum vandaag $today $metingen metingen: totaal=$humsom gemiddeld=$humgem procent

Re: different ways to determine average temperature ?

Posted: Friday 16 September 2022 11:22
by willemd
So that is method 6: a shell script reading and processing the values used by Domoticz for the 24 hour graph of the sensor? Nice.
How did you find/determine this URL "http://$DomoServer/json.htm?type=graph&sensor=temp&idx=52&range=day" ?

Re: different ways to determine average temperature ?

Posted: Saturday 17 September 2022 8:03
by aleph0
A simpler method is to use a low pass filter. After a while it becomes equivalent to the average value.

Eg, to get the average over 60 values, loop on this formula each time you get a new value :
Tavg=(59*Tavg+T)/60

Envoyé de mon moto g(6) en utilisant Tapatalk


Re: different ways to determine average temperature ?

Posted: Saturday 17 September 2022 10:15
by willemd
aleph0 wrote: Saturday 17 September 2022 8:03 A simpler method is to use a low pass filter. After a while it becomes equivalent to the average value.

Eg, to get the average over 60 values, loop on this formula each time you get a new value :
Tavg=(59*Tavg+T)/60

Envoyé de mon moto g(6) en utilisant Tapatalk
That is a running average which just filters out the fluctuations and provides a delayed smoothing effect.
It would only become equivalent to an average value is the temperature fluctuates around that average.
That's not what I need, I need an average of the day.

Re: different ways to determine average temperature ?

Posted: Thursday 22 December 2022 12:52
by WvdM78
Another way of averaging historical values is via JSON.

I don't have it working right now, but are still trying to. I assume this is the most efficient way, because calculating every 5 or 10 minutes a values is quite task intensive.

For instance this one could give some clues:
viewtopic.php?t=38288

Re: different ways to determine average temperature ?

Posted: Thursday 22 December 2022 13:02
by willemd
WvdM78 wrote: Thursday 22 December 2022 12:52 Another way of averaging historical values is via JSON.

I don't have it working right now, but are still trying to. I assume this is the most efficient way, because calculating every 5 or 10 minutes a values is quite task intensive.

For instance this one could give some clues:
viewtopic.php?t=38288
see answer of Pjedr above

http://$DomoServer/json.htm?type=graph&sensor=temp&idx=52&range=day