kubrik wrote: ↑Thursday 19 November 2020 0:02
i need help with statistical functions on persistent variables.
How can i get, for example, the AVG "temp" for the "zona" Room1 using the statistical functions avg, avgSince..etc?
You stored the historical data of multiple sensors in one table. The standard statistical dzVents functions cannot deal with that. You need to define the historical data per sensor or build your own statistical functions.
example script
Code: Select all
return
{
on =
{
devices =
{
3, 19, 471, -- temperature sensors
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = 'historical data',
},
data =
{
t3 = { history = true, maxMinutes = 60 },
t19 = { history = true, maxMinutes = 60 },
t471 = { history = true, maxMinutes = 60 },
},
execute = function(dz, item)
local idx = item.idx
-- add new data
dz.data['t' .. idx].add(item.temperature)
----------------------------------------------------------------------------------------------------------
-- Check historical storage entries
----------------------------------------------------------------------------------------------------------
if dz.data['t' .. idx].size < 3 then
dz.log('Not enough data points yet to produce something useful', dz.LOG_DEBUG)
dz.log(dz.data['t' .. idx].size .. ' entries for ' .. item.name .. ' in History', dz.LOG_DEBUG)
return
end
----------------------------------------------------------------------------------------------------------
-- Calculate
----------------------------------------------------------------------------------------------------------
-- average temperature in last 3 minutes
dz.log('Average temperature of ' .. item.name .. ' in last 3 minutes ' .. dz.utils.round(dz.data['t' .. idx].avgSince('00:03:00'),2), dz.LOG_DEBUG)
minutesStored = dz.data['t' .. idx].getOldest().time.minutesAgo
dz.log('Average temperature of ' .. item.name .. ' of all entries in history (' .. minutesStored .. ' minutes) ' ..
dz.utils.round(dz.data['t' .. idx].avg(),2), dz.LOG_DEBUG)
end
}
loglines from the example script
Code: Select all
2020-11-19 01:17:40.393 Status: dzVents: Info: historical data: ------ Start external script: history.lua: Device: "Internal Temperature (Motherboard)", Index: 3
2020-11-19 01:17:40.408 Status: dzVents: Debug: historical data: Average temperature of Internal Temperature in last 3 minutes 49.33
2020-11-19 01:17:40.408 Status: dzVents: Debug: historical data: Average temperature of Internal Temperature of all entries in history (17 minutes) 49.08
2020-11-19 01:17:40.413 Status: dzVents: Info: historical data: ------ Finished history.lua
content of datafile of example script
- Spoiler: show
Code: Select all
-- Persistent Data
local multiRefObjects = {
} -- multiRefObjects
local obj1 = {
["t3"] = {
[1] = {
["data"] = 50;
["time"] = "2020-11-19 0:17:40.245";
};
[2] = {
["data"] = 49;
["time"] = "2020-11-19 0:16:30.302";
};
[3] = {
["data"] = 49;
["time"] = "2020-11-19 0:15:20.267";
};
[4] = {
["data"] = 49;
["time"] = "2020-11-19 0:14:10.143";
};
[5] = {
["data"] = 49;
["time"] = "2020-11-19 0:13:0.233";
};
[6] = {
["data"] = 49;
["time"] = "2020-11-19 0:11:50.92";
};
[7] = {
["data"] = 49;
["time"] = "2020-11-19 0:10:40.128";
};
[8] = {
["data"] = 49;
["time"] = "2020-11-19 0:9:30.99";
};
[9] = {
["data"] = 49;
["time"] = "2020-11-19 0:8:20.46";
};
[10] = {
["data"] = 49;
["time"] = "2020-11-19 0:7:10.14";
};
[11] = {
["data"] = 49;
["time"] = "2020-11-19 0:6:0.982";
};
[12] = {
["data"] = 49;
["time"] = "2020-11-19 0:1:19.837";
};
[13] = {
["data"] = 49;
["time"] = "2020-11-19 0:0:9.803";
};
};
["t19"] = {
[1] = {
["data"] = 9.1000003814697;
["time"] = "2020-11-19 0:13:52.817";
};
[2] = {
["data"] = 9.3000001907349;
["time"] = "2020-11-19 0:3:52.300";
};
};
["t471"] = {
[1] = {
["data"] = 6.6999998092651;
["time"] = "2020-11-19 0:18:7.185";
};
[2] = {
["data"] = 6.6999998092651;
["time"] = "2020-11-19 0:13:6.263";
};
[3] = {
["data"] = 6.6999998092651;
["time"] = "2020-11-19 0:8:5.526";
};
[4] = {
["data"] = 6.6999998092651;
["time"] = "2020-11-19 0:3:4.623";
};
};
}
return obj1
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>>
dzVents wiki