Logic Machine Forum
Run tunable white (HCL) - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Run tunable white (HCL) (/showthread.php?tid=1529)



Run tunable white (HCL) - FatMax - 11.08.2018

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



RE: Run tunable white (HCL) - admin - 12.08.2018

This can be simplified by having a table with time/color temperature values. Script can also do a transition between values.


RE: Run tunable white (HCL) - admin - 13.08.2018

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)



RE: Run tunable white (HCL) - FatMax - 14.08.2018

Wow, that's a lot simpler! I´ll try this out tonight. I´ll have to write in the conversion for Philips HUE Wink


RE: Run tunable white (HCL) - tomnord - 09.10.2019

(13.08.2018, 11:24)admin Wrote: 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)
Could this be adjusted for use with two objects for warm and cold white?


RE: Run tunable white (HCL) - admin - 10.10.2019

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

points = {
  { hour = 0, min = 0, v1 = 25, v2 = 50 },
  { hour = 7, min = 30, v1 = 25, v2 = 50 },
  { hour = 8, min = 0, v1 = 30, v2 = 30 },
  { hour = 9, min = 0, v1 = 50, v2 = 25 },
  { hour = 16, min = 0, v1 = 40, v2 = 15 },
  { hour = 17, min = 0, v1 = 30, v2 = 30 },
  { hour = 18, min = 30, v1 = 25, v2 = 50  },
  { hour = 24, min = 0, v1 = 25, v2 = 50 },
}

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
  d1 = (nextpoint.v1 - prevpoint.v1) * (currmins - prevmins) / deltamins
  d2 = (nextpoint.v2 - prevpoint.v2) * (currmins - prevmins) / deltamins
else
  d1 = 0
  d2 = 0
end

v1 = math.floor(prevpoint.v1 + d1)
v2 = math.floor(prevpoint.v2 + d2)

log(now.hour, now.min, v1, v2)



RE: Run tunable white (HCL) - tomnordstrom - 10.10.2019

Yeah, I thought it might be something like that. I also want to have manual dimming of the values, with them still having "same" colour value. Thinking I could make an virtual object with dim value and use this value for offset?


RE: Run tunable white (HCL) - admin - 10.10.2019

I assume you have to LED lamps - warm and cold white.
This is the simplest formula that probably won't give very accurate results:
Code:
br = 100 -- brightness (0..100%)
ct = 50 -- color temperature (0..100%; 0 = warmest, 100 = coldest)

ww = (100 - ct) * br / 100 -- warm white value (0..100%)
cw = ct * br / 100 -- cold white value (0..100%)
log(ww, cw)

More complicated approach is to use a luxometer and create a table with multiple brightness points which can be used a reference to calculate warm and cold white levels for a given temperature.


RE: Run tunable white (HCL) - KoBra - 20.09.2023

(10.10.2019, 08:14)admin Wrote: I assume you have to LED lamps - warm and cold white.
This is the simplest formula that probably won't give very accurate results:
Code:
br = 100 -- brightness (0..100%)
ct = 50 -- color temperature (0..100%; 0 = warmest, 100 = coldest)

ww = (100 - ct) * br / 100 -- warm white value (0..100%)
cw = ct * br / 100 -- cold white value (0..100%)
log(ww, cw)

More complicated approach is to use a luxometer and create a table with multiple brightness points which can be used a reference to calculate warm and cold white levels for a given temperature.

Would this also be usable to use a Dali lamp with tunable white?

I have a Dali 2.0 Trilux light with tunable white but as the Logicmachine is Dali 1.0 it is seen as CW and WW seperately
I have as control panel a MDT touch screen that can steer by 3.007 for both brightness and colour and % for brightness and absolute colour temperature or % back for the colour. 

Basically a Dali 2.0 tunable white converter to Dali 1.0


RE: Run tunable white (HCL) - admin - 21.09.2023

DALI color temperature control is already supported by CANx-DALI. The only difference with DALI 2 is how the ballast feature detection works. But then the ballast should only have a single DALI address. Maybe it can be configured? We have a Meanwell ballast that can be switched between single and dual address mode via a physical switch.

You can check the ballast type/mode by sending querydevicetype command in CANx-DALI monitor to the ballast short address. What value do you get?


RE: Run tunable white (HCL) - KoBra - 21.09.2023

(21.09.2023, 05:15)admin Wrote: DALI color temperature control is already supported by CANx-DALI. The only difference with DALI 2 is how the ballast feature detection works. But then the ballast should only have a single DALI address. Maybe it can be configured? We have a Meanwell ballast that can be switched between single and dual address mode via a physical switch.

You can check the ballast type/mode by sending querydevicetype command in CANx-DALI monitor to the ballast short address. What value do you get?

72
07:58:38.656
RX
16 78 B9 05 6A 26 CE F5
0.1 DALI gateway (internal)
11 06
ACK; DATA = 06

71
07:58:38.621
TX
16 78 B9 05 6A 26 CE F5
0.1 DALI gateway (internal)
50 FF 99
BCAST; CMD = querydevicetype (153


RE: Run tunable white (HCL) - admin - 21.09.2023

06 is DT6 - basic ballast with brightness level control. It should either report 08 (DT8) or FF (DALI2 multiple types supported).


RE: Run tunable white (HCL) - KoBra - 21.09.2023

(21.09.2023, 08:22)admin Wrote: 06 is DT6 - basic ballast with brightness level control. It should either report 08 (DT8) or FF (DALI2 multiple types supported).

So i have to make it with script converting the data from KNX to Dali

Can i use this script for it? 

Code:
br = 100 -- brightness (0..100%)
ct = 50 -- color temperature (0..100%; 0 = warmest, 100 = coldest)

ww = (100 - ct) * br / 100 -- warm white value (0..100%)
cw = ct * br / 100 -- cold white value (0..100%)
log(ww, cw)


or should i try to use the light colour value and change it to 2x 0-100% like i did below in excel


RE: Run tunable white (HCL) - admin - 21.09.2023

The script does not use color temperature in K but simply 0..100% scale from warmest to coldest. It's up to you which approach to use.
If LED parameters are known you can tune the formula to get a more consistent brightness across the whole color temperature range.


RE: Run tunable white (HCL) - KoBra - 21.09.2023

(21.09.2023, 12:48)admin Wrote: The script does not use color temperature in K but simply 0..100% scale from warmest to coldest. It's up to you which approach to use.
If LED parameters are known you can tune the formula to get a more consistent brightness across the whole color temperature range.

ok i made a first try with scripting but i am not sure if it is right :-)

Code:
--Tunable White
br = event.getvalue('1/5/10')
ct = event.getvalue('1/0/14')
br = 100 -- brightness (0..100%)
ct = 50 -- color temperature (0..100%; 0 = warmest, 100 = coldest)
ww = (100 - ct) * br / 100 -- warm white value (0..100%)
cw = ct * br / 100 -- cold white value (0..100%)
value = cw
grp.write('32/6/1', value)
value = ww
grp.write('32/6/2', value)
end



RE: Run tunable white (HCL) - admin - 21.09.2023

Should be something like this:
Code:
br = grp.getvalue('1/5/10')
ct = grp.getvalue('1/0/14')

ww = (100 - ct) * br / 100 -- warm white value (0..100%)
cw = ct * br / 100 -- cold white value (0..100%)

grp.write('32/6/1', cw)
grp.write('32/6/2', ww)

You need to attach the same script to both brightness and color temp value objects. grp.getvalue should be used because event.getvalue only returns the value of the object that triggered the event.


RE: Run tunable white (HCL) - KoBra - 21.09.2023

(21.09.2023, 14:45)admin Wrote: Should be something like this:
Code:
br = grp.getvalue('1/5/10')
ct = grp.getvalue('1/0/14')

ww = (100 - ct) * br / 100 -- warm white value (0..100%)
cw = ct * br / 100 -- cold white value (0..100%)

grp.write('32/6/1', cw)
grp.write('32/6/2', ww)

You need to attach the same script to both brightness and color temp value objects. grp.getvalue should be used because event.getvalue only returns the value of the object that triggered the event.

or can i run it as a resident script with 0 seconds?

If i would like to turn out the value of dali directly instead if in percent is it right like this (reference is colour temp in K so it also works with homekit)?

CW or WW Light is 1% = 85 and 100% = 254

Code:
br = grp.getvalue('1/5/10')
ct = grp.getvalue('1/0/14')

ww = ((100 - (ct - 2700) * 0.000263157894736842) * br / 100) * 169 + 85  -- warm white value (85..254)
cw = (((ct - 2700) * 0.000263157894736842) * br / 100) * 169 + 85 -- cold white value (85..254)

grp.write('32/6/1', cw)
grp.write('32/6/2', ww)