Scene script for automatic adjustment of the light color depending on the time of day

Please note - Uncorrected machine translation

This English version of the following text is currently 100% machine translated. In case of ambiguity, please refer to the German version (which in this case is the original written by hand).

For luminaires with cold white/warm white setting it can be desirable that the light color changes automatically depending on the time of day.

With the help of the Scene Script such Dynamic Scenes (i.e. those where something changes even after the call) can be easily created.

This works with all kinds of lights, be it DALI (grouped single dimmers or DT-8 CW/WW or color lights), hue (ambiance or color) at P44-DSB-DEH/D and P44-LC-DE, RGB and RGBW SmartLED chains, DMX lights and PWM outputs to P44-xx-X, and of course color or color temperature lights created as Custom Device.

The following example adjusts the light color in the period between dawn and dusk between warm (400 mired) to cool (100 mired) - the darker it is, the warmer the light. Of course, other gradients can be programmed, and the numbers adjusted as needed.

How to enter a scene script?

Detailed instructions on how to create a scene script on a P44-DSB or P44-LC device are available here.

The script

The following script can be copied 1:1 and entered into the "Script to execute..." field in the sceneedit dialog can be entered, but of course it can also be adjusted as needed.

var updatedelay = 0 // Make the first light color adjustment immediately when the scene is called.
// From here on the script should run endlessly in the background, but nevertheless
// the scene call should be completed. Therefore the running adjustment
// is moved to the background with `concurrent`.
concurrent {
  // Endless until another scene is called
  while (true) {
     // value of 0..1 for day run: 1: dusk or dawn, 0: middle of the day
     var dayarc = abs(maprange(timeofday(), dawn(), dusk(), -1, 1))
     // color temperature between 400 (at the beginning and end of the day) and 100 (middle of the day) mired
     var mired = maprange(dayarc,1,0.5,400,100)
     // Output to log for control (comment out if necessary)
     log("dayarc = %f, mired = %d", dayarc, mired)
     // Set color temperature channel to new value,
     // (slow transition except first time)
     output.channel('colortemp', mired, updatedelay)
     // apply channels to hardware
     output.applychannels()
     // recalculate every 5 minutes from now on
     updatedelay = 0:05
     delay(updatedelay)
  }
}

Notes

  • The concurrent is important for dynamic scenes whose scene script runs all the time. A scene call is not strictly speaking complete until the scene script has executed. Therefore, an endlessly running scene script might block further scene calls (as of firmware 2.6.5 this no longer happens, but still concurrent should be used). With concurrent the endless running update loop is moved into the background (into a separate thread), so that the main scene script can finish running, but the update still continues.
  • maprange() is very handy for mapping two ranges of numbers to each other (see general functions in the p44script quick reference). In principle, this is a rule of three, but maprange makes it convenient and easy to read, and also ensures that the output range is not exceeded or undercut even if the input value is outside the specified range.
  • dawn() and dusk() specify the time of dawn and dusk, respectively, for the current day at the geographic location specified in the P44-xx system settings (approx. without taking obstacles on the horizon into account), so that the color adjustment is also based on the season.
  • with abs() a kind of "day arc" of 1..0..1 is calculated from a day course of -1..1.
  • The light color is only recalculated every 5 minutes - but so that no sudden jumps are visible, with output.channel() a slow transition is set with the third argument.

More info