Automatically set colortemperature on Tuneable White bulbs

Hi @All
I’ve installed Osram Tuneable White bulbs in my bedroom ceiling lamp to use as Wake-Up light. The colortemperature is automatically set over the course of day, using the ASTRO binding to provide the times for sunrise and sunset. The Astro binding is set up according to the Wiki. The Osram bulbs are controlled via the Philips Hue Hub 2.0 and the Hue Binding.

Items (only Colortemperature part shown, setup according to Hue Wiki)
Group TuneWhite Dimmer SZ_TW_Dimmer "Farbtemperatur" Dimmer SZ_TW_Dimm1 (TuneWhite) {hue="1;colorTemperature"} Dimmer SZ_TW_Dimm2 (TuneWhite) {hue="2;colorTemperature"} Dimmer SZ_TW_Dimm3 (TuneWhite) {hue="3;colorTemperature"} Dimmer SZ_TW_Dimm4 (TuneWhite) {hue="4;colorTemperature"}

Rules

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import java.util.Date
import java.util.TimeZone
import org.joda.time.*

var int ColorTemperature        // [%] current value of ColorTemperature
var int CT_cold = 0             // [%]
var int CT_warm = 63            // [%] 100% would be max, but Lightify E14 is not able to go above this value

rule "CT warmTocold"
when
         Time cron "03 01 3 ? * * *"     // To ensure that ASTRO binding has already updated   Sunrise and set/unset daylight saving is done -> run at 03:01.03
then
          var DateTime Sunrise = new DateTime(Sonnenaufgang_Time.state.toString)
          var long diff = (Sunrise.millis - now.millis)/(CT_warm-CT_cold)
          var int i = CT_warm+1
          while((i=i-1) > CT_cold-1) {
              ColorTemperature = i
              sendCommand(SZ_TW_Dimm1,i)
              sendCommand(SZ_TW_Dimm2,i)
              sendCommand(SZ_TW_Dimm3,i)
              sendCommand(SZ_TW_Dimm4,i)
              Thread::sleep(diff)
          }
          ColorTemperature = CT_cold
end

rule "CT coldTowarm" 
when
    Time cron "0 1 12 ? * * *"
then

    var DateTime Sunset = new DateTime(Sonnenuntergang_Time.state.toString)
    var long diff = (Sunset.millis - now.millis)/(CT_warm-CT_cold)
    var int i = CT_cold-1
    while((i=i+1) < CT_warm+1) {
            ColorTemperature = i
            sendCommand(SZ_TW_Dimm1,i)
            sendCommand(SZ_TW_Dimm2,i)
            sendCommand(SZ_TW_Dimm3,i)
            sendCommand(SZ_TW_Dimm4,i)
            Thread::sleep(diff)
    }
    ColorTemperature = CT_warm
end

rule "SZ EinAusschalter updaten"
when
    Item SZ_Lampe_EA1 changed
then
    postUpdate(SZ_Lampe_EA0, SZ_Lampe_EA1.state)
    postUpdate(SZ_Leselampe,OFF)
    if (SZ_Lampe_EA1.state == ON)
            Thread::sleep(2000)             // If bulbs have been switched off via wallswitch, this pause is needed to reliably set the current colortemperature after they have been switched on again
            sendCommand(SZ_TW_Dimm1,ColorTemperature)
            sendCommand(SZ_TW_Dimm2,ColorTemperature)
            sendCommand(SZ_TW_Dimm3,ColorTemperature)
            sendCommand(SZ_TW_Dimm4,ColorTemperature)
    )
end

rule "Hue Sys init"
when
    System started
then
    ColorTemperature = 0.5*(CT_cold+CT_warm)
/*      TuneWhite?.members.forEach(member | 
            sendCommand(member,ColorTemperature)
    )*/
    sendCommand(SZ_TW_Dimm1,ColorTemperature)
    sendCommand(SZ_TW_Dimm2,ColorTemperature)
    sendCommand(SZ_TW_Dimm3,ColorTemperature)
    sendCommand(SZ_TW_Dimm4,ColorTemperature)
end

`
The rule “CT warmTocold” will fade the colortemperature gradually from Warmwhite to ColdWhite from 3 am to sunrise (provided by Astro Binding).

The rule “CT coldTowarm” will fade the colortemperature gradually from ColdWhite to Warmwhite from 12 pm to sunset (provided by Astro Binding).

The rule “SZ EinAusschalter updaten” will update the OpenHAB switch state of the ceiling lamp in case the lamp has been switched outside of OpenHAB (HUE-App, Wallswitch). If the Lamp has been switched on, set the current colortemperature, as the bulbs will be set to WarmWhite if they were switched off by wallswitch.

And finally the rule “Hue Sys init” will provide an initial value for colortemperature after system startup.

This setup is working nicely giving me a cold white in the mornings and a warm white in the evenings.

Have fun.

4 Likes

Hi @kyb!

Thanks so much for this. It served as a helpful starting point to get something similar working with my LIFX bulbs. I’ve documented my current solution at Automatic sunrise and sunset color / colour temperature control (with LIFX).

Thanks again,
Simon