This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

Run tunable white (HCL)
#1
I made a script that changes the tunable white based on time of day. Maybe someone else can need it in the future. The data for the tunable white parameters was lifted from Schneiders DALI gateway. 

First run this once to store values in storage object HCL (note theres a lot of data, so if you frequent storage a lot maybe store data somewhere else):

Code:
hclTable = {
[0] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
[1] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
[2] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
[3] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
[4] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
[5] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
[6] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2200,
    m2v = 2400,
    m3v = 2500,
    m4v = 2600
},
[7] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2700,
    m2v = 3200,
    m3v = 4000,
    m4v = 5000
},
[8] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 6500,
    m2v = 6390,
    m3v = 6281,
    m4v = 6062
},
[9] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 5840,
    m2v = 5625,
    m3v = 5513,
    m4v = 5400
},
[10] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 5177,
    m2v = 4963,
    m3v = 4750,
    m4v = 4312
},
[11] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 4093,
    m2v = 3875,
    m3v = 3656,
    m4v = 3328
},
[12] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 3000,
    m2v = 3437,
    m3v = 3875,
    m4v = 4750
},
[13] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 5187,
    m2v = 5843,
    m3v = 6500,
    m4v = 6062
},
[14] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 5625,
    m2v = 5187,
    m3v = 4750,
    m4v = 4312
},
[15] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 3875,
    m2v = 3437,
    m3v = 3000,
    m4v = 3437
},
[16] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 3875,
    m2v = 4312,
    m3v = 4750,
    m4v = 5625
},
[17] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 6500,
    m2v = 5625,
    m3v = 4750,
    m4v = 3875
},
[18] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 3000,
    m2v = 3000,
    m3v = 2900,
    m4v = 2900
},
[19] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2800,
    m2v = 2700,
    m3v = 2600,
    m4v = 2500
},
[20] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
[21] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
[22] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
[23] = {
    m1 = 0,
    m2 = 10,
    m3 = 30,
    m4 = 40,
    m1v = 2500,
    m2v = 2500,
    m3v = 2500,
    m4v = 2500
},
}

storage.set('HCL', hclTable)

I set the next script as a user library, user.HCL. The function get called by a scheduled script that runs every 3 minutes. It returns the value when called. Via other triggers i disable and enable this scheduled script on demand. Note there is a Philips HUE true or false setting. If you have pure Kelvin values on your tunable whites, please set this to false. Setting it to true will convert to Mired color temperature. Also, testMode set to true will only output to log and not return value. debugMode will output to log and value if set to true.

Code:
function HCL()

    -- Fetches HCL table from storage --
data = storage.get('HCL', 0)

    -- Time and minutes right now --
local date = os.date('*t')
local hour = date.hour
local min = date.min

    -- Testmode, debugmode and Philips HUE states --
testMode = false
debugMode = false
philipsHue = false

    -- Kelvin values from HCL table --
value1 = data[hour].m1v
value2 = data[hour].m2v
value3 = data[hour].m3v
value4 = data[hour].m4v

    -- Sets color values to Mired Color Temperature if Philips Hue is used --

if philipsHue == true then
 value1 = math.floor(1000000 / data[hour].m1v)
 value2 = math.floor(1000000 / data[hour].m2v)
 value3 = math.floor(1000000 / data[hour].m3v)
 value4 = math.floor(1000000 / data[hour].m4v)
end

    -- Time in minutes is between 40 and 59 --
if min <= 59 and data[hour].m4 <= min then
 if testMode == true then
   log('Testmode: The time is: '..hour..':'..min..'. Fetching Kelvin value for between 40 to 59 mins. Value: '..value4)
 else
   value = value4
     if debugMode == true then
     log('Debug: The time is: '..hour..':'..min..'. Fetching Kelvin value for between 40 to 59 mins. Value: '..value4)
     end
 end

 -- Time in minutes is between 30 and 40 --
elseif min <= data[hour].m4 and data[hour].m3 <= min then
 if testMode == true then
   log('Testmode: The time is: '..hour..':'..min..'. Fetching Kelvin value for between 30 to 40 mins. Value: '..value3)
 else
   value = value3
   if debugMode == true then
     log('Debug: The time is: '..hour..':'..min..'. Fetching Kelvin value for between 30 to 40 mins. Value: '..value3)
   end
 end

 -- Time in minutes is between 10 and 30 --
elseif min <= data[hour].m3 and data[hour].m2 <= min then
 if testMode == true then
   log('Testmode: The time is: '..hour..':'..min..'. Fetching Kelvin value for between 10 to 30 mins. Value: '..value2)
 else
   value = value2
   if debugMode == true then
     log('Debug: The time is: '..hour..':'..min..'. Fetching Kelvin value for between 10 to 30 mins. Value: '..value2)
   end
 end
 
 -- Time in minutes is between 0 and 10 --
elseif min <= data[hour].m2 and data[hour].m1 <= min then
 if testMode == true then
   log('Testmode: The time is: '..hour..':'..min..'. Fetching Kelvin value for between 0 to 10 mins. Value: '..value1)
 else
   value = value1
   if debugMode == true then
     log('Debug: The time is: '..hour..':'..min..'. Fetching Kelvin value for between 0 to 10 mins. Value: '..value1)
   end
 end
 
 -- Error, time was not detected properly --
else
 log('HCL: ERROR, time value was not detected.')
if philipsHue == true then
     value = math.floor(1000000 / 2700)
   else
       value = 2700
   end
end

return value
 
end
Reply
#2
This can be simplified by having a table with time/color temperature values. Script can also do a transition between values.
Reply
#3
Here's my version with linear transition. Values at 0:00 and 24:00 are mandatory, values in between can be added as needed.

Code:
function tomins(hour, min)
  return hour * 60 + min
end

points = {
  { hour = 0, min = 0, value = 2500 },
  { hour = 7, min = 30, value = 2500 },
  { hour = 8, min = 0, value = 3000 },
  { hour = 9, min = 0, value = 5000 },
  { hour = 16, min = 0, value = 5000 },
  { hour = 17, min = 0, value = 3000 },
  { hour = 18, min = 30, value = 2500 },
  { hour = 24, min = 0, value = 2500 },
}

now = os.date('*t')
currmins = tomins(now.hour, now.min)

for i, point in ipairs(points) do
  nextmins = tomins(point.hour, point.min)

  if nextmins >= currmins then
    nextpoint = point
    prevpoint = points[ i - 1 ] or nextpoint
    prevmins = tomins(prevpoint.hour, prevpoint.min)
    break
  end
end

deltamins = nextmins - prevmins
if deltamins > 0 then
  delta = (nextpoint.value - prevpoint.value) * (currmins - prevmins) / deltamins
else
  delta = 0
end

value = math.floor(prevpoint.value + delta)

log(now.hour, now.min, value)
Reply
#4
Wow, that's a lot simpler! I´ll try this out tonight. I´ll have to write in the conversion for Philips HUE Wink
Reply


Forum Jump: