Rofo
(Ro)
July 24, 2023, 8:07pm
1
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.
Rofo
(Ro)
July 24, 2023, 8:17pm
2
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
Rofo
(Ro)
July 25, 2023, 7:27am
3
As a final update to the above, I also found a definitive answer to my original question in this thread:
At the beginning of an automation, I’m trying to create a scene around a light to keep its state, including its color (inspired by this article )
The problem is that when a light is off, it does not have a xy_color attribute so I want to only store this attribute if it’s available obviously (the article doesn’t mention that situation)
But my syntax is not accepted and I get a Message malformed : extra keys not allowed @ data['action'][0]['xy_color'] when I save
Do you …
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
petro
(Petro)
July 25, 2023, 2:33pm
4
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 %}