Okay, I got it working that way but the whole topic is still not clear to me.
Look at this:
licht_an:
alias: Licht an
sequence:
- service: light.turn_on
data_template: >
entity_id: light.{{ temp_group | lower }}
{% if temp_brightness is defined %}
brightness_pct: {{ temp_brightness }}
{% endif %}
{% if temp_color is defined %}
color_name: {{ temp_color }}
{% endif %}
Error on check:
Invalid config for [script]: expected a dictionary for dictionary value @ data[âscriptâ][âlicht_anâ][âsequenceâ][0][âdata_templateâ]. Got âentity_id: light.{{ temp_group | lower }} {% if temp_brightness is defined %} brightness_pct: {{ temp_brightness }} {% endif %} {% if temp_color is defined %} color_name: {{ temp_color }} {% endif %}\nâ. (See /config/configuration.yaml, line 15). Please check the docs at Scripts - Home Assistant
Without the ifs I can use it like that, or to say it different: All possible outputs of that if / else would be valid for light.turn_on - so that means there canât be a problem with mixing data types or something.
What I expect: When the if is true, the according line will be printed below data template, when not than not.
To me it looks like all the if else stuff is only working behind the parameters:
data_template:
brightness_pct: '{% if bla bla %}...{% endif %}'
and not right after data_template: to make those parameters optional. The problem is when I pass the brightness_pct to light.turn_on it insists in getting a float, an empty value will result in an error. It makes zero sense to me that I canât option out whole lines in the data_template.
I mean those parameters are optional, why canât they be an option in my data_templates. It also makes the syntax absolutely inconsistent wit service_template for my feeling (and many others feel the same when I search the forum for this issue).
I could solve this by creating additional scripts, one that includes the brightness, one that includes the color and one with both and call them accordingly but how stupid is that?
Do I have a general lack of understanding here??