Exit Rule if Light Manually Turned Off During Routine

I am using a “Simulated Sunrise” script that I found on a Reddit thread. Over the course of a half hour it gently brightens the lights in a simulated sunrise routine.

I would like to be able to “exit” the routine if the Color_BdrmWash.state == OFF after the routine has begun.

Not at home to test, but would the if/break section under the comment “// CANCEL ROUTINE IF LIGHT TURNED OFF” work?

OpenHAB script to create your own sunrise

import org.openhab.model.script.actions.Timer
import org.openhab.core.library.types.*

rule "Wake up slowly"
    when 
        Time cron "0 30 6 ? * MON-FRI"   // Monday-Friday, at 6:30am
        //Item Toggle_BdrmWash changed from OFF to ON
    then
        var Timer timer
        var Integer Dimmer
        var Integer DimmerCheck
        var Integer HueVal
        var Integer SatVal
        var DecimalType hue = new DecimalType(0) // 0-360; 0=red, 120=green, 240=blue, 360=red(again)
        var PercentType sat = new PercentType(100) // 0-100
        var PercentType bright = new PercentType(0)
        var HSBType light

        Dimmer = 0
        HueVal = 0
        SatVal = 100
        DimmerCheck=Dimmer

        while(Dimmer<=90) // End up at 90% brightness
        {
            if (Dimmer==DimmerCheck)
            {
                bright = new PercentType(Dimmer)
                hue = new DecimalType(HueVal)
                sat = new PercentType(SatVal)
                light = new HSBType(hue,sat,bright)

                // CANCEL ROUTINE IF LIGHT TURNED OFF
                // AFTER START 
                // WILL THIS WORK???
                if (HueVal>0 && Color_BdrmWash.state == OFF)
                {
                break;
                }
                
                sendCommand(Color_BdrmWash, light)
                Dimmer = Dimmer + 3
                HueVal = HueVal + 2 // Increment from 0 to 60 over half an hour
                SatVal = SatVal - 1 // Decrement from 100 saturation to 70% over half an hour

                timer = createTimer(now.plusSeconds(60)) [|
                    DimmerCheck = Dimmer 
            ]
            }
        }

        var PercenType ceilB = new PercentType(100)
        var DecimalType ceilH = new DecimalType(60)
        var PercentType ceilS = new PercentType(30)
        var HSBType ceilL = new HSBType(ceilH, ceilS, ceilB)
        sendCommand(Color_BdrmDesk, ceilL)
        timer = createTimer(now.plusMinutes(15)) [|
            sendCommand(Color_BdrmDoor, ceilL)
        ]
end

I think that will work. If not you can try using a return. However, I don’t think there is a much better way to do this without spiking your CPU usage to 100% for 30 minutes doing nothing.

I would do the following (NOTE: with other changes to make the code simpler and follow a more traditional coding style):

rule "Wake up slowly"
when
    Time cron "0 30 6 ? * MON-FRI"
then
    var hue = new DecimalType(0)
    var sat = new PercentType(100)
    var bright = new PercentType(0)
    var boolean stopped = false

    while(bright <= 90 && !stopped) {
        var light = new HSBType(hue, sat, bright)
        Color_BdrmWash.sendCommand(light)

        bright = new PercentType(bright + 3)
        hue = new DecimalType(hue.intValue + 2)
        sat = new PercentType(sat - 1)

        Thread::sleep(60000)
        if(HueVal>0 && Color_BdrmWash.state == OFF) stopped = true
    }

    if(!stopped) {
        val HSBType ceilL = new HSBType(new DecimalType(60), new PercentType(30), new PercentType(100))
        Color_BdrmDesk.sendCommand(ceilL)
        Thread::sleep(90000) // 15 minutes
        Color_BdrmDoor.sendCommand(ceilL)
    }
end

Brilliant, can’t wait to test it when I get home. Thanks for taking the time on this.

Hi,

I am searching for exactly such a rule to simulate a sunrise for waking up in the morning. I have one question to your rule. What happens in this section:

if(!stopped) {
        val HSBType ceilL = new HSBType(new DecimalType(60), new PercentType(30), new PercentType(100))
        Color_BdrmDesk.sendCommand(ceilL)
        Thread::sleep(90000) // 15 minutes
        Color_BdrmDoor.sendCommand(ceilL)
    }

Sorry for that question but I am not a programmer and with openhab I started my first expirience with programming. I think that the lamp BdrmDesk and BdrmDoor are switched on and set to the values given by ceilL. Is this right? What is the intention of that?

Thank you for your help.

You will have to ask the op. That is what he wanted the code to do.

What the code does is after ramping up the Color_BdrmWash, if the ride wasn’t cancelled, it turns on one light, waits 15 minutes then turns on another.