UI automation multiple lines not working?

Hey,

Recently I converted some yaml automations to the UI, but it seems they are not all saved properly.
I have some automations with multiple lines (because for some reason the convertion to yaml breaks the lines)
The problem is that the conversion does not copy the custom yaml from my automation

This :

data_template:
  brightness: >
    '{% if states.light.bar.attributes.brightness | int == 255 %} 125 {%- else
    -%} 255 {%- endif -%}'
  rgb_color:
    - '255'
    - '255'
    - '255'
entity_id: light.bar
service: homeassistant.turn_on

becomes this:

action:
  - data_template:
      brightness: '''{% if states.light.bar.attributes.brightness | int == 255 %}
        125 {%- else -%} 255 {%- endif -%}''

        '
      rgb_color:
      - '255'
      - '255'
      - '255'
    entity_id: light.bar
    service: homeassistant.turn_on

This breaks my automation file. I can edit the automations.yaml, but if I add of edit an automation these changes are reverted.
Is this is a limitation of the UI automation creator, or am I doing something wrong?

Hope someone can enlighten me :slight_smile:

tnx!

Rutger

PS: I know I can add an additional automations file for custom automations, but for organisational reasons I would like to keep all automations in one place if possible

The problem is that you are quoting multi line templates in your original config. This:

data_template:
  brightness: >
    '{% if states.light.bar.attributes.brightness | int == 255 %} 125 {%- else
    -%} 255 {%- endif -%}'

Should be:

data_template:
  brightness: >
    {% if states.light.bar.attributes.brightness | int == 255 %} 125 {% else
    %} 255 {% endif %}

Or even better (human readable and formatted to ignore unknown state errors):

data_template:
  brightness: >-
    {% if state_attr('light.bar', 'brightness')|int == 255 %}
      125
    {% else %} 
      255 
    {% endif %}
2 Likes

Brilliant, thank you for your fast reply!

When I put your last code in the UI editor it creates this:

- data_template:
      brightness: "{% if state_attr('light.bar', 'brightness')|int == 255 %}\n  125\n\
        {% else %} \n  255 \n{% endif %}"

It works, but is not very pretty

When I copy your last code in to the automations.yaml the UI automations will not overwrite it.
That not it is a one time fix for readability, so that is great

I didnā€™t know you could write it easier, so that was a bonus tip :slight_smile:
What does the - after the > do?
Is this the reason the code is nog overwritten anymore?
With another automation I changed this (in the automations.yaml) I got a ā€œsecretā€ ā€˜Unsupported actionā€™ under my actions where my code was placed

Thanks again!

Ignores line termination characters like Line feed or Carriage return.

Very good to know, thanks!

You can optionally use what is known as an ā€œinline ifā€ to reduce the templateā€™s size:

- data_template:
    brightness: "{{ 125 if state_attr('light.bar', 'brightness') == 255 else 255 }}"
  • The template returns 125 if the test is true otherwise it returns 255.
  • It isnā€™t necessary to use the int filter in this situation because the state_attr() function will report an integer value for brightness.

So attributes arenā€™t always strings like states?

For an attribute containing digits, the state_attr() function reports an integer (or float) value:


EDIT
Iā€™ll qualify that with a lawyerly ā€œor so it seems to do in this particular situationā€. :wink:

1 Like

Huh. Well there you go. I thought everything was strings.

Also I can never remember the format of the shortened if/else. Iā€™ve bookmarked it this time.

ā€œThis, if true, else thatā€

No worries, I often forget how to do it in Javascript when Iā€™m working with Node-Red.