Hi,
Not even sure where to start on this one, so hoping some better scripters can help. Have have a number of motion activated lights, and currently I just have to manually work out levels based on time periods, what I would like to do is have a formula work out the level based on how many minutes before\after sunrise it is.
I don't want to create a wake up clock as such, I just want to light to turn on a X level when motion is detected.
For example, if I work in the bathroom 30min before sunrise, it is still dark, so if the light turns on at 100% it is blinding, so lets start with a base level of 40%, if I work in at 10min before sunrise then it could be brighter, lets say 80%?
I can play around with levels and timing later, but I would like to test just having it say go up 2% for every minute between 30min before sunrise and then sunrise?
Anyone got an idea how I can make a level formula based on a time?
Thanks,
Wob
Help with a Formula for Light Level based on Time
Moderator: leecollings
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Help with a Formula for Light Level based on Time
Please have a look at this function. If you want you can just delete the debug blocks but I left it in as that was a small puzzle in itself

Code: Select all
-- timeBasedBrightness.lua
return {
on = { devices = { "brightnessTrigger" }}, -- I use this button to activate script during development and testing
logging = { level = domoticz.LOG_INFO,
marker = "timeBasedBrightness" },
execute = function(dz, _)
local function calculateBrightnessNeeded(minutes) -- Call with parameter minutes only for testing
local minBrightness = 40
local maxBrightness = 100
local t = dz.time
local minutesSinceMidnight = minutes or (dz.utils.round(t.secondsSinceMidnight / 60))
local endDayMinutes = t.sunsetInMinutes
local beginDayMinutes = t.sunriseInMinutes
--[[ This commented part is only needed when testing debugging
local Time = require('Time')
local Bhour = math.floor (minutes / 60)
local Bmin = minutes - Bhour * 60
if tonumber(Bhour) == 0 then Bhour = "00"
elseif tonumber(Bhour) < 10 then Bhour = "0" .. tostring(Bhour) end
if tonumber(Bmin) == 0 then Bmin = "00"
elseif tonumber(Bmin) < 10 then Bmin = "0" .. tostring(Bmin) end
t = Time(t.rawDate .. " " .. Bhour .. ":" .. Bmin .. ":" .. "00")
dz.log("Time: " .. t.raw .. " ===> dayTime: " .. tostring(t.matchesRule("between sunrise and sunset")) ,dz.LOG_DEBUG)
]] --
if t.matchesRule("between 30 minutes before sunrise and sunrise") then -- Night / early morning
return math.min(math.max(minBrightness,(maxBrightness - (beginDayMinutes - minutesSinceMidnight) * 2)), maxBrightness) end
if t.matchesRule("between sunset and 30 minutes after sunset") then -- Evening
return math.min(math.max(minBrightness,(minBrightness + (minutesSinceMidnight - endDayMinutes) * 2)), maxBrightness) end
if t.matchesRule("between sunrise and sunset") then -- Daytime
return maxBrightness end
return minBrightness -- Nighttime
end
--[[ testSet only valid on June 15th 2018; expected outcome will vary based on sunset / sunrise times
dz.log("time 2:00 ===>> Brightness expected: 40 ; Brightness from function: " .. calculateBrightnessNeeded(120) ,dz.LOG_DEBUG) -- 2:00 ===>> 40
dz.log("time 10:00 ===>> Brightness expected: 100; Brightness from function: " .. calculateBrightnessNeeded(600) ,dz.LOG_DEBUG) -- 10:00 ===>> 100
dz.log("time 5:05 ===>> Brightness expected: 66; Brightness from function: " .. calculateBrightnessNeeded(305) ,dz.LOG_DEBUG) -- 5:05 ===>> 66
dz.log("time 5:21 ===>> Brightness expected: 98; Brightness from function: " .. calculateBrightnessNeeded(321) ,dz.LOG_DEBUG) -- 5:21 ===>> 98
dz.log("time 22:03 ===>> Brightness expected: 40; Brightness from function: " .. calculateBrightnessNeeded(1323),dz.LOG_DEBUG) -- 22:03 ===>> 40
dz.log("time 22:30 ===>> Brightness expected: 94; Brightness from function: " .. calculateBrightnessNeeded(1350),dz.LOG_DEBUG) -- 22:30 ===>> 94
dz.log("time 21:45 ===>> Brightness expected: 100; Brightness from function: " .. calculateBrightnessNeeded(1305),dz.LOG_DEBUG) -- 21:45 ===>> 100
dz.log("time 17:00 ===>> Brightness expected: 100; Brightness from function: " .. calculateBrightnessNeeded(1020),dz.LOG_DEBUG) -- 17:00 ===>> 100
dz.log("time 13:00 ===>> Brightness expected: 100; Brightness from function: " .. calculateBrightnessNeeded(780) ,dz.LOG_DEBUG) -- 13:00 ===>> 100
dz.log("time 12:59 ===>> Brightness expected: 100; Brightness from function: " .. calculateBrightnessNeeded(779) ,dz.LOG_DEBUG) -- 12:59 ===>> 100
dz.log("time 00:00 ===>> Brightness expected: 40; Brightness from function: " .. calculateBrightnessNeeded(0) ,dz.LOG_DEBUG) -- 00:00 ===>> 40
]]--
dz.log("date/time: " .. dz.time.raw .. "; Brightness should now be set at: " .. calculateBrightnessNeeded() .. "%",dz.LOG_INFO)
end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Re: Help with a Formula for Light Level based on Time
That looks seriously awesome, thanks so much for taking the time. I'm very interested to have a look at your test code.
Your stuff always look so neat and compact. I've not got around to learning all the shorthand.
Wob
Sent from my SM-G935F using Tapatalk
Your stuff always look so neat and compact. I've not got around to learning all the shorthand.
Wob
Sent from my SM-G935F using Tapatalk
Re: Help with a Formula for Light Level based on Time
@waaren A bit delayed, but I just wanted to say thanks, tweaked the formula and the timing to suit a couple of rooms and it has been working great.
Who is online
Users browsing this forum: No registered users and 1 guest