Another Dimming example leveraging cron

I have been working on this for a couple of weeks (off an on) after my phone alarm app failed to wake me up three times (as soon as I unplugged my phone and turned on the screen the alarm activated). Most mornings I have no backup to get kids to school on time, I have lamps in OH to ensure their are woke up in time.

So why use cron over the numerous timer rule examples we have; I’ve tried to implement them and never quite fit, and at one point I created a loop with the logic…coming from a heavy self taught linux background I have a better understanding of cron, so just personal preference on my part and sharing for others that might also want to test.

So lets start with my items, I have two Lyfx mini white bulbs in my bedroom and its part of a fan combo, so the fan has to be running for the bulbs to have power/wifi.

Switch  mbedroomlightV45       "Lifx bulb Switches timer"                               { expire="45s,command=OFF" }
Dimmer  mbedroomlightdimV      "Master bedroom dimmer"       { channel="lifx:whitelight:mbedroom1:brightness", channel="lifx:whitelight:mbedroom2:brightness" [profile="follow"] }
Switch  mbedroomlightV         "Lifx bulb Switches"          { channel="lifx:whitelight:mbedroom1:brightness", channel="lifx:whitelight:mbedroom2:brightness" [profile="follow"] }

Pay attention to the end of my channels (yes you read that right I have two channel on the same item) at this point you should take some time and read up on the documentation as I missed this in one of the releases - https://www.openhab.org/docs/configuration/items.html#profiles

First rule:

rule "Natural wakeup expire timer"
when
    Time cron "0 45-59 5 ? * MON,TUE,WED,THU,FRI *" or
    Time cron "0 0-30 6 ? * MON,TUE,WED,THU,FRI *"
then
    mbedroomlightV45.sendCommand(ON)
end

So this rule runs at 5:45 to 5:59 and then 6:00 to 6:30 and simply turns on the virtual switch that turns off after 45 secs, I selected 45 to give OH a 15 sec gap and avoid timing problems, could be 5 secs if you wanted. :man_shrugging:

Second rule:

rule "Natural sunrise dim"
when
    Item mbedroomlightV45 received command ON
then
    val Number hour = now.getHourOfDay
    if ( mbedroomlightV.state == OFF && hour < 6) {
        mbedroomlightdimV.sendCommand((mbedroomlightdimV.state as PercentType) + 1 )
    }
    if ( mbedroomlightV.state == ON ) {
        if ( mbedroomlightdimV.state < 45) {
            mbedroomlightdimV.sendCommand((mbedroomlightdimV.state as PercentType) + 1 )
        }
        if ( mbedroomlightdimV.state == 45 ) {
            mbedroomlightV.sendCommand(OFF)
        }
    }
end

So this rule just watches the virtual expiring switch and increases the dim level up by 1% and turns it off providing me enough time to wake up and be done getting dressed and other morning routines.

2 Likes

I’m pretty sure this one is far easier:

var Timer tWakeup = null

rule "wakup timer"
when
    Time cron "0 45 5 ? * MON-FRI"         // year is optional
then
    tWakeup?.cancel                        // cancel the timer if already running
    if(mbedroomlightV.state == OFF)
        tWakeup = createTimer(now,[|       // create the timer and start it right now
            val Number nBright = mbedroomlightdimV.state as Number + 1
            if(nBright = 46) {              // already reached brightnes so
                mbedroomlightV.sendCommand(OFF)
                tWakeup = null
                return;                     // stop timer 
            }
            mbedroomlightdimV.sendCommand(nBright)
            tWakeup.reschedule(now.plusMinutes(1))  // reschedule timer
        ])
end

One rule , no extra item, same result

Thank you for the input, but my goal was to accomplish this without the use of a timer in the rules. I personally don’t care if its one rule or two. I probably could consolidate it into a single rule and have the expire be 30 secs so the dim levels update at the 30 sec mark of a minute vs the 00 sec of a minute.