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 }}
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:
Calculate lux percentage the same way as I did with brightness.
Set variable x_color by multiplying the lux percentage by 0.179 (the difference between the first numbers from state 1 and 2)
Set variable y_color by multiplying the lux percentage by 0.096 (the difference between the second numbers from state 1 and 2)
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.
Add current_x value by x_color and round off to three decimal digits.
Add current_y value by y_color and round off to three decimal digits.
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.
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: