Dual white / CCT LED light template from 2-channel source (Shelly RGBW2, etc.)

Hi,

I wanted to control my dual-white LED stripes (CCT) in our kitchen. The LED controller is a Shelly RGBW2 which natively only supports RGBW or single color white stripes.

The solution was to connect two LED channels (CW & WW) to two Shelly Channels, set the Shelly to white mode and have Home Assistant do work.

You need:

  • A light template for the logic and as the final CCT Light entity
  • Three helper variables

Calculated CCT Brightness > input_number.kuche_arbeitslicht_helligkeit
Value Range 0-255

Calculated CCT Color Temperature > input_number.kuche_arbeitslicht_farbtemperatur
When you want to keep orignial color values convert your stripes range in K to MIRED (e.g. 2700-6500 K equals 153-370 MIREDS)
When you want to use the full slider range in Lovelace use 153 to 500

Calculated CCT Color Ratio between CW and WW inputs > input_number.kuche_arbeitslicht_cw_ww
Value Range 0-100 (as percent)

Here is the magic:

  1. Set the template values to the helper variables, DO NOT calculate them directly in the template itself
  2. Set temperature script
    Set Helper value to the slider value (when using original color values make sure to use min/max limit because the silder always allows values between 153 and 500 MIREDS)
    Calculate Color Ratio and set helper value
    Set Cold White Channel brightness to selected temperature times Ratio Helper Value in Percent
    Set Warm White Channel brightness to selected temperature times (1 - Ratio Helper Value in Percent)
  3. Set level script
    Set helper value to selected brightness
    Set Cold Warm Channel brightness to selected brightness times Ratio Helper Value in Percent
    Set Warm White Channel brightness to selected brightness times (1 - Ratio Helper Value in Percent)
- platform: template
  lights:
    kuche_arbeitslight_cct:
      friendly_name: "Küche Arbeitslicht"
      level_template:  "{{ ( states('input_number.kuche_arbeitslicht_helligkeit' ) | int(0) ) }}"
      temperature_template: "{{ ( states('input_number.kuche_arbeitslicht_farbtemperatur' ) | int(0) ) }}"      
      value_template: >
        {% if is_state('light.kuche_arbeitslicht_cw', 'on' ) + is_state('light.kuche_arbeitslicht_ww', 'on' ) > 0 %}
          on
        {% else %}
          off
        {% endif %}
      turn_off:
        service: light.turn_off
        data: {}
        target:
          entity_id:
          - light.kuche_arbeitslicht_cw
          - light.kuche_arbeitslicht_ww  
      turn_on:
        service: light.turn_on
        data: {}
        target:
          entity_id:
          - light.kuche_arbeitslicht_cw
          - light.kuche_arbeitslicht_ww
      set_temperature:
          - service: input_number.set_value
            data:
              value: "{{ color_temp }}" 
              # with original color values set lower and upper limits: "{{ [ [ color_temp, 200 ] | max, 370 ] | min }}"
              entity_id: input_number.kuche_arbeitslicht_farbtemperatur
          - service: input_number.set_value
            data:
              value: "{{ [ [ ( -1/347 * states( 'input_number.kuche_arbeitslicht_farbtemperatur' ) | int + 500 / 347 ) * 100, 0 ] | max, 100 ] | min }}"
              # with original color values: replace 500 with upper limit, replace 347 with upper limit minus lower limit (e.g. 370-200 = 170)
              entity_id: input_number.kuche_arbeitslicht_cw_ww
          - service: light.turn_on
            data:
              brightness: "{{ ( this.attributes.brightness | int(0) ) * states( 'input_number.kuche_arbeitslicht_cw_ww' ) | int / 100 }}"
              entity_id: light.kuche_arbeitslicht_cw
          - service: light.turn_on
            data:
              brightness: "{{ ( this.attributes.brightness | int(0) ) * ( 1 - states( 'input_number.kuche_arbeitslicht_cw_ww' ) | int / 100 ) }}"
              entity_id: light.kuche_arbeitslicht_ww
      set_level:
          - service: input_number.set_value
            data:
              value: "{{ brightness }}"
              entity_id: input_number.kuche_arbeitslicht_helligkeit
          - service: light.turn_on
            data:
              brightness: "{{ brightness * states( 'input_number.kuche_arbeitslicht_cw_ww' ) | int / 100}}"
              entity_id: light.kuche_arbeitslicht_cw
          - service: light.turn_on
            data:
              brightness: "{{ brightness * ( 1 - states( 'input_number.kuche_arbeitslicht_cw_ww' ) | int / 100 ) }}"
              entity_id: light.kuche_arbeitslicht_ww       
5 Likes

Thank you so much for the code. It worked great for me. But when I move the slider of the temperature. there is no issue. When I move the temp slider fastly, it is generating an error and turning off. Any Idea about it??

my code for reference…

  • platform: template
    lights:
    hanging_light_cct:
    friendly_name: “Hanging Light CCT”
    level_template: “{{ ( states(‘input_number.cct_brightness’ ) | int(0) ) }}”
    temperature_template: “{{ ( states(‘input_number.cct_temperature’ ) | int(0) ) }}”
    value_template: >
    {% if is_state(‘light.virtual_cool_white’, ‘on’ ) + is_state(‘light.virtual_warm_white’, ‘on’ ) > 0 %}
    on
    {% else %}
    off
    {% endif %}
    turn_off:
    service: light.turn_off
    data: {}
    target:
    entity_id:
    - light.virtual_cool_white
    - light.virtual_warm_white
    turn_on:
    service: light.turn_on
    data: {}
    target:
    entity_id:
    - light.virtual_cool_white
    - light.virtual_warm_white
    set_temperature:
    - service: input_number.set_value
    data:
    value: “{{ color_temp }}”
    # with original color values set lower and upper limits: “{{ [ [ color_temp, 200 ] | max, 370 ] | min }}”
    entity_id: input_number.cct_temperature
    - service: input_number.set_value
    data:
    value: “{{ [ [ ( -1/347 * states( ‘input_number.cct_temperature’ ) | int + 500 / 347 ) * 100, 0 ] | max, 100 ] | min }}”
    # with original color values: replace 500 with upper limit, replace 347 with upper limit minus lower limit (e.g. 370-200 = 170)
    entity_id: input_number.cct_ratio
    - service: light.turn_on
    data:
    brightness: “{{ ( this.attributes.brightness | int(0) ) * states( ‘input_number.cct_ratio’ ) | int / 100 }}”
    entity_id: light.virtual_cool_white
    - service: light.turn_on
    data:
    brightness: “{{ ( this.attributes.brightness | int(0) ) * ( 1 - states( ‘input_number.cct_ratio’ ) | int / 100 ) }}”
    entity_id: light.virtual_warm_white
    set_level:
    - service: input_number.set_value
    data:
    value: “{{ brightness }}”
    entity_id: input_number.cct_brightness
    - service: light.turn_on
    data:
    brightness: “{{ brightness * states( ‘input_number.cct_ratio’ ) | int / 100}}”
    entity_id: light.virtual_cool_white
    - service: light.turn_on
    data:
    brightness: “{{ brightness * ( 1 - states( ‘input_number.cct_ratio’ ) | int / 100 ) }}”
    entity_id: light.virtual_warm_white

Hi @stefan81,

I am new to home assistant and templates. I want to achieve what you have did here.

Could you maybe show me the input_number variables? I don’t understand how should I create those calculations within the variables.

It would be much appreciated. Thanks!

Hi

I am new here and I want to do the same.
I have two entities CW/WW.

  • number.fl_licht_led_cw
  • number.fl_licht_led_ww

Can you help me with your code? How I have to prepare him?