Smooth Lighting Transitions with OpenHAB Rules

Hey everyone, I’ve been diving into OpenHAB lately and thought I’d share my experience with controlling lighting transitions for a cinematic feel. I wanted my lights to dim or brighten gradually when I start or stop a movie in Kodi, just like in a theater. Here’s how I achieved it!

I started with two basic rules: one to dim the lights when a movie starts and another to brighten them back when the movie stops. Initially, it worked, but the change was abrupt. I knew I needed something smoother, so I decided to tweak the rules to make the transitions gradual.

Here’s the rule I came up with for dimming the lights:

java
rule “Dim Lights During Movie”
when
Item Kodi_LivingRoom_PlaybackState_Raw changed
then
var String json = (Kodi_LivingRoom_PlaybackState_Raw.state as StringType).toString
var String playback_state = transform(“JSONPATH”, “$.kodi_state”, json)
if (playback_state == “started”) {
// Gradually dim the lights over 30 seconds
var int targetBrightness = 12
var int currentBrightness = Light_Living_Room_Ceiling.state as DecimalType
var int step = 5
var int delay = 1000

    while (currentBrightness > targetBrightness) {
        currentBrightness -= step
        if (currentBrightness < targetBrightness) {
            currentBrightness = targetBrightness
        }
        sendCommand(Light_Living_Room_Ceiling, currentBrightness)
        Thread::sleep(delay)
    }
}

end

And here’s the rule for brightening the lights after the movie ends:

java
rule “Raise Lights After Movie”
when
Item Kodi_LivingRoom_PlaybackState_Raw changed
then
var String json = (Kodi_LivingRoom_PlaybackState_Raw.state as StringType).toString
var String playback_state = transform(“JSONPATH”, “$.kodi_state”, json)
if (playback_state == “stopped”) {
// Gradually brighten the lights over 30 seconds
var int targetBrightness = 80
var int currentBrightness = Light_Living_Room_Ceiling.state as DecimalType
var int step = 5
var int delay = 1000

    while (currentBrightness < targetBrightness) {
        currentBrightness += step
        if (currentBrightness > targetBrightness) {
            currentBrightness = targetBrightness
        }
        sendCommand(Light_Living_Room_Ceiling, currentBrightness)
        Thread::sleep(delay)
    }
}

end

These rules use a loop to adjust the brightness in small increments, creating a smooth transition effect. The delay between each step is set to 1 second, but you can adjust this to make the transition faster or slower. I found that 1-second intervals provide a nice balance between smoothness and responsiveness.

One thing I noticed was that the initial abrupt change was due to sending a single command with the target brightness. By breaking the change into smaller steps, the transition becomes much smoother. It’s a simple concept, but it makes a big difference in the overall experience!

I also experimented with different step sizes and delays to find the right combination for my setup. Smaller steps with shorter delays create a smoother transition, but they might increase the load on your system if you have many lights to control. For my single ceiling light, this setup works perfectly.

Overall, I’m thrilled with how this turned out. It adds a touch of professionalism to my home theater setup, and it’s incredibly satisfying to see the lights dim gracefully as the movie starts. If you’re looking to enhance your media room experience, I highly recommend giving this a try!

Happy automating!