Help with hue sunrise rule

Hi, I want to create a rule that creates a sunrise with the hue. I use openhab2. I found this example on reddit, but get an error message. Can someone help me? I have a 0210 Hue bulb according to https://github.com/eclipse/smarthome/blob/master/extensions/binding/org.eclipse.smarthome.binding.hue/README.md#full-example

Here is the reddit post:
"I made a nice script to bring my Hue lights up slowly in the morning like a sunrise. This is the best way to wake up that I’ve ever had!
Over the course of half an hour, one light (in my case an upright lamp) turns from 0% brightness red to 100% brightness yellow-white. Then one of the ceiling lights turns on, and after 15 minutes the other one.

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
"
The last part I should be able to delete because I only want to use a lamp. Right?

Does not seame like i can import the two liberaries?

My first error is at “sendCommand(Color_BdrmWash, light)”. I have changed it to my color channel item:
Ambiguous feature call. The methods sendCommand(Item, Number) in BusEvent and sendCommand(Item, Command) in BusEvent both match.

My second error is at “DimmerCheck = Dimmer” inside the if:
Cannot refer to the non-final variable DimmerCheck inside a lambda expression

An HSBType is both a Number and a Command at the same time and the language doesn;t know which you really want. append a .toString and that will eliminate the ambiguity: Also, it is better to call the sendCommand method on the
item than the sendCommand actions:

Color_BdrmWash.sendCommand(light.toString)

The second error is a limitation of the language. It basically means that by the time the Timer executes, this rule will have exited and Dimmer and DimmerCheck will no longer exist. Timers run in the background. Do you really want to use Thread::sleep(60000)?

thanks! Im trying your recommendations right now and it seems to work.

Hey @Larsson24 can you post the code if you got it working? Thanks!

Hello, I hope this works for you too. It worked for me, but I dont have it up and running right now after i moved. One tip is to fix a way to abort the script while it is running, otherwise you can not interrupt the ignition of the lamp and you have to wait half an hour until the script is finished.

rule "Morning light"

when 
	Time cron "0 0 7 ? * MON-FRI" 
then
//ON/OFF button	
if(Morgonlampor_Sovrum_Vardagar.state==ON){
		callScript("morgonlampa 2")
	}

end

Script morgonlampa 2

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 = 3
HueVal = 2
SatVal = 99
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)
      Light3_Color.sendCommand(light.toString)
      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
		 
        Thread::sleep(60000)
        DimmerCheck = Dimmer
            }
        }

2 Likes
1 Like

Thanks! Got it working this morning its great! What do i need to tweak to get it to white light in the end? I have milight rgbw bulbs that supports real white color? Very nice, ive been looking for this for a while!