Configuring light colours using input helpers

I have an text input helper that contains a value in this format

255,255,255

and I can use this code to split that into the RGB colours of a service call:

      - service: light.turn_on
        data:
          brightness_pct: 100
          rgb_color:
            - "{{states('input_text.hall_lights_final_colour').split(',')[0]}}"
            - "{{states('input_text.hall_lights_final_colour').split(',')[1]}}"
            - "{{states('input_text.hall_lights_final_colour').split(',')[2]}}"
          transition: 1
        target:
          entity_id: light.moes_rgb_bulb_first_floor_hall_light

This all works fine.

However, the device in question is an RGBW bulb, so displaying white using RGB 255,255,255 is noticeably dimmer than using this option:

      - service: light.turn_on
        data:
          transition: 1
          kelvin: 6500
          brightness_pct: 100
        target:
          entity_id: light.moes_rgb_bulb_first_floor_hall_light

I’m looking for a way to detect if the input helper has commas in it, and if so, continue using the RGB method, but if not, to use the kelvin method. I have tried this but I can’t seem to get it to work:

service: light.turn_on
data:
  brightness_pct: 100
{%if "," in states('input_text.hall_lights_final_colour') %}
  rgb_color:
    - "{{states('input_text.hall_lights_final_colour').split(',')[0]}}"
    - "{{states('input_text.hall_lights_final_colour').split(',')[1]}}"
    - "{{states('input_text.hall_lights_final_colour').split(',')[2]}}"
{%else%}
  kelvin: "{{states('input_text.hall_lights_final_colour')}}"
{%endif%}
  transition: 1
target:
  entity_id: light.moes_rgb_bulb_first_floor_hall_light

I simply get a red bar in the editor, but no clue how to fix it. I assume it doesn’t like the if statements mucking up the service call.

Not sure if this is the best way to go about it, but I did figure out a method using an if condition:

      - if:
          - condition: template
            value_template: "{{',' in states('input_text.hall_lights_final_colour') }}"
        then:
          - service: light.turn_on
            data:
              brightness_pct: 100
              rgb_color:
                - "{{states('input_text.hall_lights_final_colour').split(',')[0]}}"
                - "{{states('input_text.hall_lights_final_colour').split(',')[1]}}"
                - "{{states('input_text.hall_lights_final_colour').split(',')[2]}}"
              transition: 1
            target:
              entity_id: light.moes_rgb_bulb_first_floor_hall_light
            enabled: true
        else:
          - service: light.turn_on
            data:
              brightness_pct: 100
              kelvin: "{{states('input_text.hall_lights_final_colour')}}"
              transition: 1
            target:
              entity_id: light.moes_rgb_bulb_first_floor_hall_light
            enabled: true

As a final update to the above, I also found a definitive answer to my original question in this thread:

Essentially the answer that @123 posted there was:

"You cannot template YAML statements.

Home Assistant processes YAML first and then processes Jinja2. Your example attempts to selectively include/exclude YAML using Jinja2 and that can never work due to the order of processing."

Which makes total sense, and is what I had suspected.

1 Like

That’s old, you can template the entire data section.


data: >
  {% set color = states('input_text.hall_lights_final_colour') %}
  {% if "," in color %}
    {{ {'brightness_pct': 100, 'rgb_color': color.split(',') | map('int') | list, 'transition': 1 } }}
  {% else %}
    {{ {'brightness_pct': 100, 'kelvin': color, 'transition': 1 } }}
  {% endif %}