Hi all,
I want to share my Sunrise emulation solution. Features:
- Has settings of full run time and step size.
- Can be interrupted, script stops if user turns off lights.
- No sleep() function usages.
- Controls group of lamps to provide smothness: first lamp slightly goes from 0% to 100%, then second from 0% to 100% and so on.
In my case it starts at 5:00 and finishes at 6:00. I used 3 Ikea zigbee-driven filament lamps (as main decoration light) and one Yeelight Wight lamp (as night light).
Code of my rule:
// Sinrise emulation
var Timer sunriseTimer = null
var Boolean sunriseStarted = false
var Integer sunriseBrightness = 0
var Integer sunriseModeValue = 0
rule "Sinrise emulation start"
when
Time cron "0 00 05 ? * MON-FRI"
//Item testSwSunrise changed
then
logWarn("light", "Sunrise emulation started")
sunriseBrightness = 0;
sunriseStarted = false;
sunriseTimer = createTimer(now, [ |
if (sunriseStarted) {
if (g_light_eg_sz_night.state == OFF) { // Group to be used as "switch-detector"
logWarn("light", "Sunrise emulation stopped: group switched off")
return
}
}
sunriseStarted = true
// How long full programm runs (seconds)
var brightPeriod = 60 * 60
// How much percent changed for every step
var brightPeriodStep = 1
// Items to set ON
// We do not use groups, we must have exact order of lamps defined
// You can replace this for group if you dont care of order (e.q. g_light_eg_sz_night)
var lightGroupItems = newArrayList(yel_white_1_brightness, ikea_led_007b_brightness, ikea_led_2203_brightness, ikea_led_2819_brightness)
// 1/100 from hour (3600 sec)
var brightLoopSize = lightGroupItems.size
// Calculate timer
var brightTimerMs = (brightPeriod * 1000) / ( (100 / brightPeriodStep) * brightLoopSize)
sunriseModeValue = sunriseBrightness
lightGroupItems.forEach [s |
var setBright = sunriseModeValue
sunriseModeValue = sunriseModeValue - 100
if (setBright < 0) {
return
}
if (setBright > 100) {
return
}
logWarn("light", "Sunrise item " + s.label + " -> " + setBright)
s.sendCommand(setBright)
]
// All items 100% ?
if (sunriseBrightness > (lightGroupItems.size * 100)) {
sunriseTimer = null
logWarn("light", "Sunrise emulation done")
} else {
// New timer loop
sunriseBrightness = sunriseBrightness + brightPeriodStep
sunriseTimer.reschedule(now.plusMillis(brightTimerMs))
}
])
end
Full source avaliable in my Github: https://github.com/petrows/smarthome-openhab/blob/master/rules/light-sunrise.rules
Tested, provided nice results =)