Parsing and display of folded/literal jinja in HA's YAML editor

I am writing a simple automation that is supposed to keep two Switches in sync provided that a certain boolean helper flag is on. I am trying to embed simple jinja logic in the action to achieve this - similar to some examples I found in the Actions documentation, and I tested the code to work in Developer tools actions. I got the automation to work but some behaviour in the YAML editor appears strange to me.

Here is the code:

alias: Bar Audio Switch sync to Entertain
description: ""
triggers:
  - trigger: state
    entity_id:
      - switch.omnilink_bar
conditions:
  - condition: state
    entity_id: input_boolean.sync_bar_and_entertain_audio
    state:
      - "on"
  - condition: template
    value_template: "{{ states('switch.omnilink_entertain') != trigger.to_state.state }}"
actions:
  - action: >
      {% if trigger.to_state.state == 'on' %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id: switch.omnilink_entertain
mode: single

I’m not sure how the forum parser will display this, but to be safe here is a screenshot of the last block in the HA yaml interface, where you can see the entity_id color is confused with the preceding multiline jinja block.

Confusingly, if I save and reopen this automation then HA changes my folded style > to a literal | – why does it do that, is that just part of how it works?

Anyway, just checking whether I’m not doing anything wrong. Any comments on whether the code should be improved would also be appreciated.

Not sure about the editor quirks but it looks ok to me. You can simplify it though:

actions:
  - action: "switch.turn_{{ trigger.to_state.state }}"
    entity_id: switch.omnilink_entertain

You should probably include this in the trigger to prevent errors if the switch becomes unavailable:

triggers:
  - trigger: state
    entity_id:
      - switch.omnilink_bar
    to:
      - 'on'
      - 'off'
1 Like

Wow, I would have never thought of trying this. Thank you so much.