Finally had time to play a bit more with this, and came up with something which is more or less working:
light:
- platform: template
lights:
corrimano_u:
friendly_name: "Corrimano"
value_template: "{{ (state_attr('light.corrimano_caldo', 'brightness')|int + state_attr('light.corrimano_freddo', 'brightness')|int) > 0 }}"
level_template: "{{ ((state_attr('light.corrimano_caldo', 'brightness')|float + state_attr('light.corrimano_freddo', 'brightness')|float)/2)|int }}"
temperature_template: "{{ ((state_attr('light.corrimano_caldo', 'brightness')|float*6500 + state_attr('light.corrimano_freddo', 'brightness')|float*3000)/((state_attr('light.corrimano_caldo', 'brightness')|int + state_attr('light.corrimano_freddo', 'brightness')|int))) }}"
set_level:
- service: light.turn_on
data:
entity_id: light.corrimano_caldo
brightness: "{{ ((brightness*2) * (6500-state_attr('light.corrimano_u', 'temperature')|int) / (6500-3000)|int) }}"
- service: light.turn_on
data:
entity_id: light.corrimano_freddo
brightness: "{{ ((brightness*2) * (state_attr('light.corrimano_u', 'temperature')|int - 3000) / (6500-3000)|int) }}"
set_temperature:
- service: light.turn_on
data:
entity_id: light.corrimano_caldo
brightness: "{{ ((state_attr('light.corrimano_u', 'brightness')|int*2) * (6500 - 1000000/color_temp) / (6500 - 3000)|int) }}"
- service: light.turn_on
data:
entity_id: light.corrimano_freddo
brightness: "{{ ((state_attr('light.corrimano_u', 'brightness')|int*2) * (1000000/color_temp - 3000) / (6500 - 3000)|int) }}"
turn_on:
- service: light.turn_on
data:
entity_id: light.corrimano_caldo
- service: light.turn_on
data:
entity_id: light.corrimano_freddo
turn_off:
- service: light.turn_off
data:
entity_id: light.corrimano_caldo
- service: light.turn_off
data:
entity_id: light.corrimano_freddo
The following works as expected if done from the UI, see examples:
- if I switch on and set 50% brightness, then 153 mireds (6500K) only the cold one switches on, at 100% brightness, which is correct
- if I switch on and set 50% brightness, then 333 mireds (3000K) only the warm one switches on, at 100% brightness, which is correct
- if I switch on and set 50% brightness, then 210 mireds (4750K) both the warm and cold ones switch on, at 50% brightness, which is correct
Now, there is still something I don’t understand. If I change brightness only form UI, the behaviour is not logic. For example, if I’m in any state, then I change brightness, whatever is the temperature, the warm light get switched on at full brightness, the cold one gets switched off. If I then change the temperature, the brightness of each of the two lights goes back to correct relative values, but with overall brightness at 50%.
I think this is due to the template not having the temperature
attribute, in fact if I try the following in template on the developers tools, I get None
:
{{state_attr('light.corrimano_u', 'temperature')}}
It’s getting complicated