Template question - multiple if/then statements

I’m trying to build a TTS message out of multiple if/then statements. Here it is, broken down into two separate messages - but is there a way to combine these two messages into a single message?

    sequence:
  - service: media_player.alexa_tts
    data_template:
      entity_id: media_player.jasons_spot, media_player.kitchen_echo
      message: >
        {% if is_state('input_boolean.basement_secure', 'on') %}
          Skynet security analysis running. Basement is secure.
        {% else %}
          Skynet security analysis running. Basement is not secure.
        {% endif %}
  - delay: '00:00:04'
  - service: media_player.alexa_tts
    data_template:
      entity_id: media_player.jasons_spot, media_player.kitchen_echo
      message: >
        {% if is_state('input_boolean.first_floor_secure', 'on') %}
          First floor is secure.
        {% else %}
          First floor is not secure.
        {% endif %}

Figured it out:

          message: >
        Basement {% if is_state('input_boolean.basement_secure', 'on') %}secure{% else %}not secure{% endif %}.
        First floor {% if is_state('input_boolean.first_floor_secure', 'on') %}secure{% else %}not secure{% endif %}.

There are a number of ways you can do this. The way you did it works just as well as these below.

Lazy method:

    sequence:
  - service: media_player.alexa_tts
    data_template:
      entity_id: media_player.jasons_spot, media_player.kitchen_echo
      message: >
        {% if is_state('input_boolean.basement_secure', 'on') %}
          Skynet security analysis running. Basement is secure.
        {% else %}
          Skynet security analysis running. Basement is not secure.
        {% endif %}
        {% if is_state('input_boolean.first_floor_secure', 'on') %}
          First floor is secure.
        {% else %}
          First floor is not secure.
        {% endif %}

make a dictionary:

    sequence:
  - service: media_player.alexa_tts
    data_template:
      entity_id: media_player.jasons_spot, media_player.kitchen_echo
      message: >
        {% set statemap = {'on':'secure','off':'not secure'} %}
        Skynet security analysis running. Basement is {{ statemap[states('input_boolean.basement_secure')] }}
        First floor is {{ statemap[states('input_boolean.first_floor_secure')] }}
1 Like