# Set hue xy_color depending on lux level

Hi there,

I’ve been trying to set up an automation that set the brightness and xy_color of my hue lights depending on the amount lux between 4000 and 0. For the brightness I’ve created the following action:

``````  action:
- service: script.turn_on
entity_id: script.controllightwithlux
data_template:
variables:
brightness_lv: >
{%- set lux = states.sensor.lux | int -%}
{%- set perc = (lux / 4000) | int -%}
{{ (75 * perc) | int }}
``````

And this is my script:

``````controllightwithlux:
sequence:
- service: light.turn_on
data:
entity_id: light.linksvoor
transition: 5
data_template:
brightness: '{{ brightness_lv }}'
xy_color: '{{ color_lv}}
``````

But the part that I cant seem to figure out is how to create the same data_template for the xy_color of my hue lights. I’m trying to create a color fade between two states depending on the lux value:

State 1, when lux is 4000: (0.335, 0.325)
State 2, when lux is 0: (0.514, 0.421)

How can I calculate the xy_value when the lux level is per example at 2000 (50%)? Sadly my limited knowledge of YAML has left me stranded. But my initial thought was to:

1. Calculate lux percentage the same way as I did with brightness.
2. Set variable x_color by multiplying the lux percentage by 0.179 (the difference between the first numbers from state 1 and 2)
3. Set variable y_color by multiplying the lux percentage by 0.096 (the difference between the second numbers from state 1 and 2)
4. Remove the ( ) brackets from the current xy attribute value and split at ", " which would leave me with a seperate current_x variable and current_y variable.
5. Add current_x value by x_color and round off to three decimal digits.
6. Add current_y value by y_color and round off to three decimal digits.
7. Combine the outcome of step 5 & 6 in color_lv variable and add the ', ’ seperator and ( ) brackets again.

I tried explaining my brainfart as best as I could, sorry if it’s still a bit vague. And I’m not sure if my steps would even work! But I would be ever so grateful if someone has a solution (if there is one) and is willing to help me out.

Cheers!

Managed to figure it out. Came up with the following two templates:

Template for color depending on lux level (from white to warm):

``````      value_template: '[{{ (((((4000-states.sensor.lux.state|float)/4000) * 179) + 335) / 1000) | round(3) }}, {{ (((((4000-states.sensor.lux.state|float)/4000) * 96) + 325) / 1000) | round(3) }}]'
``````

Template for brightness of lamp depending on lux level:

`value_template: '{{ (((4000-states.sensor.lux.state|float)/4000) * 75) | round(0) }}'`

Thank you for the info Giel88,
I’m a rooky in HA. I’m now searching for controlling lights the way you do.
I understand your published automation and script of your initial post. Your solution was to change the data template to a value template, if I not mis understood. Can you publish the automation and the script again with the values that works for you?
That makes it for me a lot easier for a solution. I’m not lazy but HA is a steep learning curve for me because I have zero programming skills.

Have since changed it a lot. Using variables and such so I dont have to change every single script for all my lights. I’ll try to explain it as best as I can. But bear with me! It’s a basic math calculation where I calculate a percentage and add that to the starting value.

I’ve set a single sensor for:

• Time remaining sunlight (sensor.sunlight)
This calculates the amount of sunlight by using wunderground solar radiation.

I’ve set a variable for:

• Sunlight trigger. When Sunlight amount hits below this the automation will trigger.
• Start xy_color which is a white color (var.start_xy)
• End xy_color, a warm color (var.end_xy)

For xy_color create a sensor with this calculation. I’ve split the x and y but you can choose not to.

``````platform: template
sensor:
x_color:
value_template: >-
{%- set begin = states('var.start_xy').split(', ')[0] | float %}
{%- set end = states('var.end_xy').split(', ')[0] | float %}
{%- set sunlight = states('sensor.sunlight') | float %}
{%- set trigger = states('var.min_sunlight') | float %}
{%- set color = (((((trigger - sunlight ) / trigger ) * (end - begin)) + begin) / 1000) | round(3) %}
{% if sunlight == 0 %}
{{ end/1000 | round(3) }}
{% else %}
{{ color | round(3) }}
{% endif %}
y_color:
value_template: >-
{%- set begin = states('var.start_xy').split(', ')[1] | float %}
{%- set end = states('var.end_xy').split(', ')[1] | float %}
{%- set sunlight = states('sensor.sunlight') | float %}
{%- set trigger = states('var.min_sunlight') | float %}
{%- set color = (((((trigger - sunlight ) / trigger ) * (end - begin)) + begin) / 1000) | round(3) %}
{% if sunlight == 0 %}
{{ end/1000 | round(3) }}
{% else %}
{{ color | round(3) }}
{% endif %}
``````

Then for brightness create a sensor with this calculation:

``````platform: template
sensors:
brightness_linksvoor:
value_template: >-
{%- set maxbrightness = states('var.max_brightness_linksvoor') | float %}
{%- set sunlight = states('sensor.sunlight') | float %}
{%- set trigger = states('var.min_sunlight') | float %}
{%- set brightness = ((trigger - sunlight ) / trigger ) * maxbrightness %}
{{ brightness | round(0) }}
``````

Now load these into a script by using the following sequence:

``````lamps_on:
alias: "Lamps on"
sequence:
- service: light.turn_on
entity_id: light.livingroom
data_template:
brightness: "{{ states.sensor.brightness_keuken.state | int }}"
xy_color: ["{{ states.sensor.x_color.state | float }}","{{ states.sensor.y_color.state | float }}"]
``````

thank you so much for sharing and your time. Ill try this weekend to implement.

1 Like