A simplified sunrise light with scripts and no sleep

After i discovered the magic of scripts, i translated several functions of my smarthome to scripts. Before using scripts, this sunrise was implemented as a complex lambda with 5 parameters. Ugly.

Now it is not reusable anymore, but you can just copy it and adjust the light bulb items. Simplified by large.

This strategy can be reused wherever you need to loop over a longer duration and do not want to use Thread.sleep. With the second script as the loop body you omit the writing of a complex CreateTimer statement. Much easier to test and debug.

The items:

Color WZL_COLOR "WZ Farbe" { channel="hue:0210:0017884f6873:1:color" }
Switch WZL_TOGGLE "WZ Licht Ein/Aus"	<light> { channel="hue:0210:0017884f6873:1:color" } 

Sunrise_WZ.script

WZL_COLOR.sendCommand("0,99,3")
WZL_TOGGLE.sendCommand(ON)

createTimer(now.plusSeconds(60)) [ | callScript("Sunrise_WZ_loop.script") ]

Sunrise_WZ_loop.script

var String[] oldlight
var String newlight

var Integer hue // 0=rot,60=gelb,120=grün,180=türkis,240=blau,300=lila
var Integer sat // 0-100
var Integer bright=3 // 0-100

if (bright <= 99 && WZL_TOGGLE.state == ON)
{
    oldlight = WZL_COLOR.state.toString.split(",")	

    hue = Integer::parseInt(oldlight.get(0))+2
    sat = Integer::parseInt(oldlight.get(1))-1
    bright = Integer::parseInt(oldlight.get(2))+3
    newlight = String::format("%d,%d,%d",hue,sat,bright)

    WZL_COLOR.sendCommand(newlight)

    createTimer(now.plusSeconds(60)) [ | callScript("Sunrise_WZ_loop.script") ]
} 
6 Likes

Hi,
Many thanks for sharing. I have one question: what is the use of the timer in Sunrise_WZ.script?

Ludovic

Every light state stays for 60 seconds. Due to the timer in the starter script, this is also true for the first state.

Ok, thanks a lot