Template and if statement position

I have written a template sensor and that the moment it looks like that:

  platform: template
  sensors:
      # Template created to display either a green or black bin whichever is next
      bin:
        value_template: >-
            {% if this %}
                State1
            {% else %}
                State2
            {% endif %}
        friendly_name_template: >-
            {% if this %}
                Name1
            {% else%}
                Name2
            {% endif %}
        icon_template: >-
            {% if this %}
                icon1
            {% else %}
                icon2
            {% endif %}

The if statement is the same for each template. Is there a way to “simplify” the code so that the if statement is only used once and the template are surrounded?

I have tried several way of formatting it but the check config always come back saying it is not correct

nope, not possible. Jinja does not work across multiple fields. You’ll have to live with the long templates or find a way to shorten them without using if statements.

EDIT: for example…

        {% if this %}
            State1
        {% else %}
            State2
        {% endif %}

is equivalent to

 {{ 'State1' if this else 'State2' }}

Fair enough!

Thank you for your other way of presenting the if statement I will try it