Hi,
I’ve made some rules for simulating sunrise in the morning which I’d like to use in wake-up routine. The sunrise is triggered by a switch and can be cancelled by switching off. I use HomeKit to automate the daily wake-up so the switch is simply exposed in HomeKit.
Below is the items file:
Color Bedroom_lamp "Bedroom Ceiling Lamp" <colorwheel> { channel="hue:xxxx:xxxxxxxx:2:color" }
Switch Sunrise_switch "Bedroom Sunrise" {homekit="Switchable"}
String Sunrise_next
Below is the rules:
// Rules
var Timer timer = null
var HSBType initial = new HSBType(new DecimalType(0),new PercentType(100),new PercentType(2))
rule "Startup"
when
System started
then
logInfo("Info", "Hue: System started")
Sunrise_switch.sendCommand(OFF)
end
rule "Start sunrise"
when
Item Sunrise_switch changed from OFF to ON
then
var hsb = Bedroom_lamp.state as HSBType
var DecimalType brightness = new DecimalType(hsb.brightness.intValue)
logInfo("Info", "Hue: Sunrise switch turned on when the lamp has brightness "+ brightness)
if (brightness == 0) {
// set a timer to sligtly change the color again and again
timer = createTimer(now, [|
Sunrise_next.sendCommand(now.toString)
if (timer !== null) {
timer.reschedule(now.plusSeconds(30))
}
])
}
else {
Sunrise_switch.sendCommand(OFF)
}
end
rule "Continue sunrise"
when
Item Sunrise_next changed
then
if (Sunrise_switch.state == ON) {
// get your current color
var hsb = Bedroom_lamp.state as HSBType
// if current brightness is 0, turn on the lamp with initial colour
if (new DecimalType(hsb.brightness.intValue) == 0){
logInfo("Info", "Hue: Turn on the lamp and set the initial colour "+initial)
Bedroom_lamp.send(initial)
}
else {
// change it a little bit
var DecimalType hue = new DecimalType(hsb.hue.intValue + 1) // 0-360; 0=red, 120=green, 240=blue, 360=red(again)
if(new DecimalType(hsb.hue.intValue) >= 35){
hue = new DecimalType(35)
}
var PercentType sat = new PercentType(hsb.saturation.intValue -1) // 0-100
var PercentType bright = new PercentType(hsb.brightness.intValue + 2) // 0-100
var HSBType newHsb = new HSBType(hue,sat,bright)
if (hsb.brightness.intValue < 90){
// set the new color
logInfo("Info", "Hue: Sending command to the light " + newHsb)
Bedroom_lamp.send(newHsb)
}
else {
logInfo("Info", "Hue: Reach birghtness 90, cancel the timer")
Sunrise_switch.sendCommand(OFF)
}
}
}
else {
if (timer !== null) {
timer.cancel()
}
timer = null
}
end
rule "Cancel sunrise"
when
Item Sunrise_switch changed from ON to OFF
then
logInfo("Info", "Hue: Sunrise switch turned off")
if (timer !== null) {
timer.cancel()
}
timer = null
//Bedroom_lamp.sendCommand(OFF)
end
It kind of works but I have a few questions:
- Is the script optimal? Particularly I’d like to avoid running the rules in parallel, so am I doing things correctly?
- Is it correct to cancel the timer by " timer.cancel()"?
- For settings the values, I can’t seem to use float numbers, for example var DecimalType hue = new DecimalType(hsb.hue.intValue + 0.5) doesn’t work. Does Hue only support integers? If so how can I increment “hue” value by 1 for every 2nd iteration?
Any suggestion is appreciated!