Template Sensor to combine 2 lights

Hello,

I am trying to combine a Yeelight which has 2 modes (normal mode and night mode) into one template light. The idea is that from Brightness 1-30% of the template light the night mode turns on and scales from 1 - 100%, from 31% to 100% of the template light the normal mode turns on and scales from 1 - 100%. I can’t use the Lightener HACS addon, as it turns off the not used lights - unfortunately a turn off service call to either of the 2 modes turns off the whole lamp.

I especially have trouble to understand the level_template and value_template.
Would be great if someone could give me some pointers, thanks a lot in advance!

light:
  - platform: template
    lights:
      ceiling_light:
        friendly_name: "Ceiling Light"
        level_template: >-
          {% set brightness1 = state_attr('light.yeelight_ceiling4_0x7d79fa4_nightlight', 'brightness') | default(0) %}
          {% set brightness2 = state_attr('light.yeelight_ceiling4_0x7d79fa4', 'brightness') | default(0) %}
          {% if brightness1 > 0 %}
            {{ (brightness1 / 255 * 30) | round(0) }}
          {% elif brightness2 > 0 %}
            {{ 30 + (brightness2 / 255 * 70) | round(0) }}
          {% else %}
            0
          {% endif %}
        turn_on:
          service: light.turn_on
          target:
            entity_id:
              - light.yeelight_ceiling4_0x7d79fa4_nightlight
        turn_off:
          service: light.turn_off
          target:
            entity_id:
              - light.yeelight_ceiling4_0x7d79fa4
        set_level:
          service: script.set_ceiling_lights
          data:
            brightness: "{{ brightness }}"

            entity_id:
              - light.yeelight_ceiling4_0x7d79fa4
        set_level:
          service: script.set_ceiling_lights
          data:
            brightness: "{{ brightness }}"



script:
  set_ceiling_lights:
    alias: Set Ceiling Lights
    icon: mdi:ceiling-fan-light
    fields:
      brightness:
        required: true
    sequence:
    - if:
      - alias: Brightness < 30%
        condition: template
        value_template: '{{ int(brightness) < 76 }}'
      then:
      - service: light.turn_on
        data:
          brightness: '{{ (int(brightness / 2.55) * 3) }}'
        target:
          entity_id: light.yeelight_ceiling4_0x7d79fa4_nightlight
      else:
      - service: light.turn_on
        data:
          brightness: '{{ ((int(brightness -30 / 2.55)) * 3) }}'
        target:
          entity_id: light.yeelight_ceiling4_0x7d79fa4
1 Like