dannybloe wrote:Ok, cool..
dzVents provide some nice statistical functions and data smoothing.
My script stores a fixed number of light readings. Using them, I'd like to determine
the trend over a period of time, that is, if the light levels are rising or if they are declining. If the light level is below a certain point and the trend is declining, I will assume that the "day state" is "Dawn". As opposite, If the light level is above a certain point and the trend is rising I will assume that the "day state" is "Dusk". It would probably be more adequate to calculate a moving average to smooth out short-term fluctuations and highlight longer-term trends. I'm not sure how to do that though.
Just in case You or someone else have ideas how this should be done, I include the embryo of my script below.
Code: Select all
--[[
lightingMood.lua by BakSeeDaa
Version 0.0.1
--]]
local LIGHT_SENSOR = 'South-southwest Light Level'
local LIGHT_LEVEL_DARK = 60 -- At this level and below, outdoor illumination is lit and shades are closed
local LIGHT_LEVEL_BRIGHT = 860 -- No illumination needed above this value
local TEST_MODE_LIGHT_READING = 0 -- Set to a value between 1 and 55000. Set to 0 to disable test mode
-- Shady is between LIGHT_LEVEL_DARK and LIGHT_LEVEL_BRIGHT
local LIGHTLEVEL_READINGS = 20
return {
active = true,
on = {
LIGHT_SENSOR,
['timer'] = 'every minute'
},
data = {
lightLevel = { history = true, maxItems = LIGHTLEVEL_READINGS }
},
execute = function(domoticz, windDevice, triggerInfo)
local LOG_LEVEL = domoticz.LOG_INFO -- Script default log level. You may change this.
if (triggerInfo.type == domoticz.EVENT_TYPE_TIMER) then
-- Timer event occurred
-- Store the current light level value in the persistant data
lightLevelData = domoticz.data['lightLevel']
for i = 1, (lightLevelData.size == 0 and LIGHTLEVEL_READINGS or 1) do
lightLevelData.add((TEST_MODE_LIGHT_READING == 0
and domoticz.devices[LIGHT_SENSOR].lux or TEST_MODE_LIGHT_READING))
end
--domoticz.log('The latest stored light level: '..lightLevelData.getLatest().data, LOG_LEVEL)
local avg1 = lightLevelData.avg(1, lightLevelData.size/2)
local avg2 = lightLevelData.avg(lightLevelData.size/2+1, lightLevelData.size)
domoticz.log('I would like to believe that the light level trend is '..(avg1 >= avg2 and 'declining' or 'rising')..'. avg1: '.. avg1..'. avg2: '.. avg2, LOG_LEVEL)
else
-- Device event occurred
end
end
}