Help with Timer and Dimming Lights

227 views
Skip to first unread message

Paul Muldoon

unread,
Apr 15, 2015, 2:57:29 PM4/15/15
to ope...@googlegroups.com
I'm trying to create a rule and learn how I can have a (bedroom) light turn on in the morning at 10%, slowly dim up to 100% over a 15 minute period, and then to turn off about an hour later.

I started out by first following this from the wiki here.

But I've now since learned that using Thread::Sleep is perhaps not the best approach and noticed the Sleep can actually vary depending on system use.

And I've now seen some Openhab Examples using CreateTimer.  But I'm not sure if you can use anykind of loop and/or Variable in Rules file?

I should also noted that I'm using Openhab to control a rooted wink hub.   The below is what I've started with, but am looking to see if can be done without using Thread::Sleep and if Creating a Timer is better?

//Test Rules

rule "BedRoom Wakeup"

when
        Time cron "0 15 15 ? * MON-FRI "
then
        callScript("dimmlight")
end

And my dimmlight script
//Morning Light Wakeup Script

var Number Dimmer

Dimmer=15

executeCommandLine("apron-relay -u -m 1 -t 2 -v 15")
executeCommandLine("apron-relay -u -m 1 -t 1 -v ON")

while(Dimmer<255 )
{
        executeCommandLine("apron-relay -u -m 1 -t 2 -v" + Dimmer)

        Dimmer=Dimmer+15
        Thread::sleep(60000)
}




Paul Muldoon

unread,
Apr 15, 2015, 3:54:17 PM4/15/15
to ope...@googlegroups.com
I'm trying to learn some more and put together the below as well, but still haven't quite gotten this to work. But I think the below is closer now.

Again, trying to have a light turn on in the morning, and then slowly dim up.  And then after reaching 100% (or Dim level of 255) to then turn off an hour later.  Ignore the Cron time for now as i'm just testing 

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


//BedRoom_Wakeup

var Timer timer = null

rule "Bedroom light wakeup"
when
Time cron "0 04 16 ? * MON-FRI"
then
var Number Dimmer
Dimmer=15
//First Turn ON the light and Set to Level 15
executeCommandLine("apron-relay -u -m 1 -t 2 -v 15")
executeCommandLine("apron-relay -u -m 1 -t 1 -v ON")
//Quickly Sync Openhab Status
Light_GF_Living_Table.state==ON
while(Dimmer<255 )  //255 is equal to 100%
{
        if(Light_GF_Living_Table.state==ON) {
        If(Dimmer==255){
// Light is ON and at Max 255 or 100% So turn off in 1 hour
timer = createTimer(now.plusMinutes(60)) [|
executeCommandLine("apron-relay -u -m 1 -t 1 -v OFF")
//Sync Openhab back to Off
Light_GF_Living_Table.state==OFF
]
}
} else {
//Increase the light level by 15 each minute.
timer = createTimer(now.plusMinutes(1)) [|
executeCommandLine("apron-relay -u -m 1 -t 2 -v" + Dimmer)
Dimmer=Dimmer+15
]
}
}
end


Alan Grainger

unread,
Aug 3, 2015, 3:24:08 PM8/3/15
to ope...@googlegroups.com
Hi Paul, I got it to work like this. Not sure if it's the most elegant delay/sleep solution, but it definitely works!

This brings one light up over the course of half an hour, from red to yellow, and then turns on the other two. I'm much happier waking up this way!


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)
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



Reply all
Reply to author
Forward
0 new messages