Conditional "data" in a template service call

Hi there,

haven’t found similar requests but maybe due to the case that this is not possible that way. I’m not new to programming but are to yaml and home assistant.

        set_temperature:
          - service: light.turn_on
            target:
              entity_id: light.shellyrgbw2_a903ba_channel_1
            data: >
              {% if color_temp > 262 %}
                brightness: "{{ 255 }}"
              {% else % }
                brightness: "{{ 0 }}"
              {% endif %}

I want to provide different data based on a certain condition using an IF /ELSE.
Getting this:

Invalid config for [light.template]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got '%') for dictionary value @ data['lights']['tw_light']['set_temperature'][0]['data']. Got '{% if color_temp > 262 %}\n brightness: "{{ 255 }}"\n{% else % }\n brightness: "{{ 0 }}"\n{% endif %}\n'. (See ?, line ?).

You cannot use templates to build yaml configuration…

Are you trying to set brightness or color temperature? It appears that this is a part of a template light, but then your service data seems to be setting a brightness…? It is almost always best to include the entire configuration in question so we can see how everything is supposed to be working together.

If your goal is to set a brightness when the template light’s color temperature is modified you need to adjust your template:

set_temperature:
  - service: light.turn_on
    target:
      entity_id: light.shellyrgbw2_a903ba_channel_1
    data:
      brightness: >
        {% if color_temp > 262 %}
        255
        {% else % } 
        0
        {% endif %}

Hey Drew, thanks for the reply.

In general, I’m currently combining two channels of a Shelly RGB (set in 4 channel mode) to one tunable white light, which at the moment seems to work as expected)

yes, I want to set set brightness of two shelly channels (being cold white and warm white) based on the color_temp value that have been set, which works in general with the full code below.

Just lacking YAML/ home assistant experience at the moment to set a certain brightness based on calculation, that has two different calculations depending on the color_temp

light:
  - platform: template
    lights:
      tw_light:
        friendly_name: "Tunable Light 1"
        value_template: "{{ is_state('light.shellyrgbw2_a903ba_channel_1','on') }}"
        level_template: "{{ state_attr('light.shellyrgbw2_a903ba_channel_1', 'brightness') }}"
        temperature_template: "{{ (262 - ((state_attr('light.shellyrgbw2_a903ba_channel_2', 'brightness')/255 - state_attr('light.shellyrgbw2_a903ba_channel_1', 'brightness')/255) * 109)) | round(0) }}"
        min_mireds_template: "{{ 153 }}"
        max_mireds_template: "{{ 371 }}"
        turn_on:
          - service: light.turn_on
            target:
              entity_id: light.shellyrgbw2_a903ba_channel_1
          - service: light.turn_on
            target:
              entity_id: light.shellyrgbw2_a903ba_channel_2
        turn_off:
          - service: light.turn_off
            target:
              entity_id: light.shellyrgbw2_a903ba_channel_1
          - service: light.turn_off
            target:
              entity_id: light.shellyrgbw2_a903ba_channel_2
        set_temperature:
          - service: light.turn_on
            target:
              entity_id: light.shellyrgbw2_a903ba_channel_1 #warm LED channel
            data: 
              brightness: "{{ color_temp }}" #here I want to set different valus based on the set temperature values, need two different calculations
          - service: light.turn_on 
            target:
              entity_id: light.shellyrgbw2_a903ba_channel_2 #cold LED channel
            data:
              brightness: "{{ 100 }}" #here I want to set different valus based on the set temperature values, need two different calculations 
        set_level:
          service: light.turn_on
          target:
            entity_id: light.tw_light
          data:
            brightness: "{{ brightness }}"

image

In your original code it wants

set_temperature:
          - service: light.turn_on
            target:
              entity_id: light.shellyrgbw2_a903ba_channel_1
            data: >
              {% if state_attr('light.shellyrgbw2_a903ba_channel_1', 'color_temp') > 262 %}
                brightness: "255"
              {% else % }
                brightness: "0"
              {% endif %}

That may not be exact but you need to tell it to adjust the attribute of the light so there were some small errors in how the template was written. I’m at work so I can’t dig deeper at the moment but hopefully that helps some

I think there are a couple things that might be problematic.

  1. Of the actions only turn_on and turn_off are required, so don’t add things that are unnecessary… As it is in your example, your set_level action is unnecessary.

  2. You’ve set the overall state and brightness of your light based only one of the included lights. If that light is off or it’s brightness is changed to 0, your template light will turn off… which will turn off both led channels.

  3. You have not provided defaults for your brightness-based templates. When a light’s brightness changes to 0, the light turns off and the brightness attribute renders as None, breaking your template.

Is the goal to end up with a proportional control of the color temp or just a binary warm vs. cool?