Here is my example of fading in a hue bulb over 30 minutes at sunset. I have a switch called AstroSunCivilDuskStart that is turned on by an astro event at the start of Civil Dusk. Once the switch is triggered this rule fires off and first sets a counter variable to 0 and a max brightness variable to 100. The rule reads the current hsb value from the bulb which in my home at this point in the day is probably (0,0,0). I then specify 3 more variables for hue, sat, and brightness that are combined into the the light variable that is actually sent to the bulb. During each loop the brightness parameter is incremented by 1 to give the slow fade in. The counter in incremented by 1 and the loop sleeps for the calculated number of seconds.
I’d like to add a group or array of bulbs to this rule instead of creating an entirely new duplicate rule for the additional bulbs. I haven’t quite figured that part out yet but wanted to post my solution for fading in a hue bulb at the start of Civil Dusk.
Enjoy
rule "Hue Fade On Living Room Right Lamp"
when
Item AstroSunCivilDuskStart changed from OFF to ON
then
var counter = 0
var maxbrightness = 100
while(counter < maxbrightness) {
// read initial hsb value from bulb
var hsb = LivingRoomLeftLamp_Color.state as HSBType
// sepcify new hsb parameters, 30 and 60 are set for bulbs to end in reading mode
var DecimalType hue = new DecimalType(030)
var PercentType sat = new PercentType(060)
// the brightness hsb value increases by 1 for each looop iteration
var PercentType bright = new PercentType(hsb.brightness.intValue + 1)
// new variable created to send to light
var HSBType light = new HSBType(hue,sat,bright)
LivingRoomLeftLamp_Color.send(light)
counter = counter + 1
Thread::sleep(18000) //18 seconds because 1800 seconds in 30 minute divided by maxbrightness set ticks
}
end
Your rule will be running for a very long time (30mins) and mostly sleeping. OpenHAB cannot run many rules in parallel, and you will run quickly out of rules threads. More details here:
I believe you can use timers for this, but I am not capable of giving you the code. This will give you a start though:
Thank you for your contribution!!
Keep them going.
However as has been pointed out your rule will monopolise a thread for up to 1/2 an hour and that’s not good practice
Try using timers:
This way your rule will only take a few millis seconds to run and thereafter all the processing will take place in a scheduled timer thread which itself will only take a few milliseconds to run
var Timer myTimer = null // At the TOP of the rules files (after imports)
var maxbrightness = 100 // At the TOP of the rules files (after imports)
rule "Hue Fade On Living Room Right Lamp"
when
Item AstroSunCivilDuskStart changed from OFF to ON
then
if (myTimer !== null) { // if the timer doesn't exist already, carry on
myTimer = createTimer(now, [ | // start timer now
// read initial hsb value from bulb
var hsb = LivingRoomLeftLamp_Color.state as HSBType
// specify new hsb parameters, 30 and 60 are set for bulbs to end in reading mode
var DecimalType hue = new DecimalType(030)
var PercentType sat = new PercentType(060)
// the brightness hsb value increases by 1 for each loop iteration
var PercentType bright = new PercentType(hsb.brightness.intValue + 1)
// new variable created to send to light
var HSBType light = new HSBType(hue,sat,bright)
LivingRoomLeftLamp_Color.send(light)
if (bright < maxbrightness) {
myTimer.reschedule(now.plusSeconds(18)) // run the timer again in 18s
} else {
myTimer = null // cancel the timer
}
])
}
end
@vzorglub has a good solution for you with a timer, and here’s another one I made, using a cron rule that fires every minute. Mine will also start from the current dim level (similar to current HSB value you pull).
Wow I am shocked at the outpouring of community support! Cant wait to give these a go and turn on more of the learning faucet. This openhab noob greatly appreciates it!