
- platform: template
  lights:
    kitchen_ceiling_cct:
      unique_id: 4af558a4-1b04-496f-9312-2b62afc42b3c
      friendly_name: "Kitchen: Main"

      value_template: >-
        {{ is_state('light.kitchen_main_ww','on') or is_state('light.kitchen_main_cw','on') }}

      # "echte" Helligkeit = Mittelwert
      level_template: >-
        {% set ww = state_attr('light.kitchen_main_ww','brightness')|int(0) %}
        {% set cw = state_attr('light.kitchen_main_cw','brightness')|int(0) %}
        {{ ((ww + cw) / 2) | round(0) }}

      # mired aus aktuellem Mix: 333 (warm) .. 167 (kalt)
      temperature_template: >-
        {% set ww = state_attr('light.kitchen_main_ww','brightness')|int(0) %}
        {% set cw = state_attr('light.kitchen_main_cw','brightness')|int(0) %}
        {% set total = ww + cw %}
        {% if total == 0 %}
          250
        {% else %}
          {% set frac_cw = cw / total %}
          {{ (333 - frac_cw * (333 - 167)) | round(0) }}
        {% endif %}

      min_mireds_template: "167"
      max_mireds_template: "333"

      turn_on:
        - service: light.turn_on
          target:
            entity_id:
              - light.kitchen_main_ww
              - light.kitchen_main_cw
        # neutraler Start, falls beide 0
        - service: light.turn_on
          target: { entity_id: light.kitchen_main_ww }
          data:
            brightness: >-
              {% set ww = state_attr('light.kitchen_main_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_main_cw','brightness')|int(0) %}
              {{ 180 if ww==0 and cw==0 else ww }}
        - service: light.turn_on
          target: { entity_id: light.kitchen_main_cw }
          data:
            brightness: >-
              {% set ww = state_attr('light.kitchen_main_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_main_cw','brightness')|int(0) %}
              {{ 180 if ww==0 and cw==0 else cw }}

      turn_off:
        - service: light.turn_off
          target:
            entity_id:
              - light.kitchen_main_ww
              - light.kitchen_main_cw

      # Level setzen: beide Kanäle zum Ziellevel hin "zusammenziehen"
      set_level:
        - service: light.turn_on
          target: { entity_id: light.kitchen_main_ww }
          data:
            brightness: >-
              {% set level = brightness|int(0) %}
              {% set ww = state_attr('light.kitchen_main_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_main_cw','brightness')|int(0) %}
              {% set delta = ww - cw %}
              {% set k = 1 - (level / 255) %}
              {% set raw = level + (k * delta / 2) %}
              {{ [0, [raw|round(0), 255] | min] | max }}
        - service: light.turn_on
          target: { entity_id: light.kitchen_main_cw }
          data:
            brightness: >-
              {% set level = brightness|int(0) %}
              {% set ww = state_attr('light.kitchen_main_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_main_cw','brightness')|int(0) %}
              {% set delta = ww - cw %}
              {% set k = 1 - (level / 255) %}
              {% set raw = level - (k * delta / 2) %}
              {{ [0, [raw|round(0), 255] | min] | max }}

      # Farbtemp setzen: Gesamtlevel (Mittelwert) beibehalten, Mix neu
      set_temperature:
        - service: light.turn_on
          target: { entity_id: light.kitchen_main_ww }
          data:
            brightness: >-
              {% set ct = color_temp|int(250) %}
              {% set ww = state_attr('light.kitchen_main_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_main_cw','brightness')|int(0) %}
              {% set base = (((ww + cw) / 2) | round(0)) | int %}
              {% set base = 180 if base == 0 else base %}
              {% set frac_cw = (333 - ct) / (333 - 167) %}
              {% set frac_ww = 1 - frac_cw %}
              {% set raw = base * frac_ww %}
              {{ [0, [raw|round(0), 255] | min] | max }}
        - service: light.turn_on
          target: { entity_id: light.kitchen_main_cw }
          data:
            brightness: >-
              {% set ct = color_temp|int(250) %}
              {% set ww = state_attr('light.kitchen_main_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_main_cw','brightness')|int(0) %}
              {% set base = (((ww + cw) / 2) | round(0)) | int %}
              {% set base = 180 if base == 0 else base %}
              {% set frac_cw = (333 - ct) / (333 - 167) %}
              {% set raw = base * frac_cw %}
              {{ [0, [raw|round(0), 255] | min] | max }}


- platform: template
  lights:
    kitchen_hob_cct:
      unique_id: 24a4a866-e30f-4b40-ac1e-86c823d8889f
      friendly_name: "Kitchen: Kochfeld"

      value_template: >-
        {{ is_state('light.kitchen_hob_ww','on') or is_state('light.kitchen_hob_cw','on') }}

      # "echte" Helligkeit = Mittelwert
      level_template: >-
        {% set ww = state_attr('light.kitchen_hob_ww','brightness')|int(0) %}
        {% set cw = state_attr('light.kitchen_hob_cw','brightness')|int(0) %}
        {{ ((ww + cw) / 2) | round(0) }}

      # mired aus aktuellem Mix: 333 (warm) .. 167 (kalt)
      temperature_template: >-
        {% set ww = state_attr('light.kitchen_hob_ww','brightness')|int(0) %}
        {% set cw = state_attr('light.kitchen_hob_cw','brightness')|int(0) %}
        {% set total = ww + cw %}
        {% if total == 0 %}
          250
        {% else %}
          {% set frac_cw = cw / total %}
          {{ (333 - frac_cw * (333 - 167)) | round(0) }}
        {% endif %}

      min_mireds_template: "167"
      max_mireds_template: "333"

      turn_on:
        - service: light.turn_on
          target:
            entity_id:
              - light.kitchen_hob_ww
              - light.kitchen_hob_cw
        # neutraler Start, falls beide 0
        - service: light.turn_on
          target: { entity_id: light.kitchen_hob_ww }
          data:
            brightness: >-
              {% set ww = state_attr('light.kitchen_hob_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_hob_cw','brightness')|int(0) %}
              {{ 180 if ww==0 and cw==0 else ww }}
        - service: light.turn_on
          target: { entity_id: light.kitchen_hob_cw }
          data:
            brightness: >-
              {% set ww = state_attr('light.kitchen_hob_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_hob_cw','brightness')|int(0) %}
              {{ 180 if ww==0 and cw==0 else cw }}

      turn_off:
        - service: light.turn_off
          target:
            entity_id:
              - light.kitchen_hob_ww
              - light.kitchen_hob_cw

      # Level setzen: beide Kanäle zum Ziellevel hin "zusammenziehen"
      set_level:
        - service: light.turn_on
          target: { entity_id: light.kitchen_hob_ww }
          data:
            brightness: >-
              {% set level = brightness|int(0) %}
              {% set ww = state_attr('light.kitchen_hob_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_hob_cw','brightness')|int(0) %}
              {% set delta = ww - cw %}
              {% set k = 1 - (level / 255) %}
              {% set raw = level + (k * delta / 2) %}
              {{ [0, [raw|round(0), 255] | min] | max }}
        - service: light.turn_on
          target: { entity_id: light.kitchen_hob_cw }
          data:
            brightness: >-
              {% set level = brightness|int(0) %}
              {% set ww = state_attr('light.kitchen_hob_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_hob_cw','brightness')|int(0) %}
              {% set delta = ww - cw %}
              {% set k = 1 - (level / 255) %}
              {% set raw = level - (k * delta / 2) %}
              {{ [0, [raw|round(0), 255] | min] | max }}

      set_temperature:
        - service: light.turn_on
          target: { entity_id: light.kitchen_hob_ww }
          data:
            brightness: >-
              {% set ct = color_temp|int(250) %}
              {% set ww = state_attr('light.kitchen_hob_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_hob_cw','brightness')|int(0) %}
              {% set base = (((ww + cw) / 2) | round(0)) | int %}
              {% set base = 180 if base == 0 else base %}
              {% set frac_cw = (333 - ct) / (333 - 167) %}
              {% set frac_ww = 1 - frac_cw %}
              {% set raw = base * frac_ww %}
              {{ [0, [raw|round(0), 255] | min] | max }}
        - service: light.turn_on
          target: { entity_id: light.kitchen_hob_cw }
          data:
            brightness: >-
              {% set ct = color_temp|int(250) %}
              {% set ww = state_attr('light.kitchen_hob_ww','brightness')|int(0) %}
              {% set cw = state_attr('light.kitchen_hob_cw','brightness')|int(0) %}
              {% set base = (((ww + cw) / 2) | round(0)) | int %}
              {% set base = 180 if base == 0 else base %}
              {% set frac_cw = (333 - ct) / (333 - 167) %}
              {% set raw = base * frac_cw %}
              {{ [0, [raw|round(0), 255] | min] | max }}
