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
}