Syntax Questions in Configuration (Covers.yaml)

I hope, the general “configuration” category is okay for my question - if not, please give me a hint, where it should be placed :slight_smile:
Yesterday I’ve started with my first scripted template and after some trial&error, copying and testing the stuff, it’s working now.
Unfortunantely I have some problems in understanding the yaml config:

My covers.yaml

  - platform: template
    covers:
      garagedoor:
        friendly_name: 'Garage Door'
        value_template: >
          # Why I need to use the character sequences {%- and -%} at each start and beginning?
          # in general: Why I need to use the following line and why it is not scripted in the same way as it's used within the icon_template part?
          {%- if states.binary_sensor.garagedoor_open_close_z -%}
            {{ states.binary_sensor.garagedoor_open_close_z.state == 'on' }}
          {%- else -%}
            true
          {%- endif -%}
        open_cover:
          service: switch.turn_on
          entity_id: switch.garage_bbc8_switch_0
        close_cover:
          service: switch.turn_on
          entity_id: switch.garage_bbc8_switch_0
        icon_template: >
          {%- if states.binary_sensor.garagedoor_open_close_z.state == 'on' -%}
            mdi:garage-open
          {%- else -%}
            mdi:garage
          {%- endif -%}

I’ve already check the documentation, but I haven’t found the needed information.
Can anyone give me some additional information about the question provided as the comments?

Thanks :slight_smile:

The question is on Jinja2 templates, not on YAML. See here:

https://jinja.palletsprojects.com/en/latest/templates/

The extra - character in {%- suppresses whitespace (see here) but usually isn’t needed.

That code is actually wrong (or, rather, it doesn’t do what you think it does).
Simply use {{ states.binary_sensor.garagedoor_open_close_z.state == 'on' }} instead, which amounts to

          {%- if states.binary_sensor.garagedoor_open_close_z.state == 'on' -%}
            true
          {%- else -%}
            false
          {%- endif -%}

Thanks a lot for clarification :slight_smile:
@Troon “Jinja2” was the termn, which was not present for me. I will have a look into it!
@koying Actually I had no clue, what the first line did as it’s still looking wrong for me. So thanks for your confirmation that {%- if states.binary_sensor.garagedoor_open_close_z.state == 'on' -%} is the correct syntax. Interesting enough: The snipped in the first post works as expected; although the first value_template might be “wrong”

It works as expected because if states.binary_sensor.garagedoor_open_close_z is always true.

1 Like