Multiple if/then statements embedded in tts.google_say service call

I have a blueprint that sends mobile notifications for updates to HA core, OS, and Supervisor. That part is working as intended. To compliment this blueprint, I wanted to add TTS notifications for the same updates. I wasn’t able to combine the two into a single automation, so I opted to split them up.

I created a new automation in the automation editor where a TTS message is sent based on the state of these same entity states. However, when I add the three if/endif statements, I get an error:

"can not read a block mapping entry; a multiline key may not be an implicit key"

What is the correct way to set the message block so it speaks the defined text of any of the update states if they are true?

- id: '1677909050513'
  alias: HA Update TTS Notifications
  description: Automation to notify of HA Core, OS, and Supervisor updates via TTS
  mode: single

  trigger:
    - platform: state
      entity_id:
        - update.home_assistant_core_update
        - update.home_assistant_operating_system_update
        - update.home_assistant_supervisor_update
      from: "off"
      to: "on"

  condition: 
    - condition: time
      after: "08:00:00"
      before: "21:00:00"

  action:
    - service: tts.google_say
      data:
        cache: false
        entity_id: group.common_area_media_players
        message: >
        There are updates available for Home Assistant.

        {% if states.update.home_assistant_core_update.state == "on" %}
        {{ states.update.home_assistant_core_update.attributes.title }} has an update available.  Release {{ state_attr('update.home_assistant_core_update', 'latest_version') }} is available for installation.
        {% endif %}

        {% if states.update.home_assistant_operating_system_update.state == "on" %}
        {{ states.update.home_assistant_operating_system_update.title }} has an update available.  Release {{ state_attr('update.home_assistant_operating_system_update', 'latest_version') }} is available for installation.
        {% endif %}

        {% if states.update.home_assistant_supervisor_update.state == "on" %}
        {{ states.update.home_assistant_supervisor_update.title }} has an update available.  Release {{ state_attr('update.home_assistant_supervisor_update', 'latest_version') }} is available for installation.
        {% endif %}

You miss the attributes in second and third if statement

{% if states.update.home_assistant_core_update.state == "on" %}
{{ states.update.home_assistant_core_update.attributes.title }} has an update available.  Release {{ state_attr('update.home_assistant_core_update', 'latest_version') }} is available for installation.
{% endif %}

{% if states.update.home_assistant_operating_system_update.state == "on" %}
{{ states.update.home_assistant_operating_system_update.attributes.title }} has an update available.  Release {{ state_attr('update.home_assistant_operating_system_update', 'latest_version') }} is available for installation.
{% endif %}

{% if states.update.home_assistant_supervisor_update.state == "on" %}
{{ states.update.home_assistant_supervisor_update.attributes.title }} has an update available.  Release {{ state_attr('update.home_assistant_supervisor_update', 'latest_version') }} is available for installation.
{% endif %}

Thanks. I added them to the second and third if statements. However, I’m still getting the same error.

Try this:

- id: '1677909050513'
  alias: HA Update TTS Notifications
  description: Automation to notify of HA Core, OS, and Supervisor updates via TTS
  mode: single

  trigger:
    - platform: state
      entity_id:
        - update.home_assistant_core_update
        - update.home_assistant_operating_system_update
        - update.home_assistant_supervisor_update
      from: "off"
      to: "on"

  condition: 
    - condition: time
      after: "08:00:00"
      before: "21:00:00"

  action:
    - service: tts.google_say
      data:
        cache: false
        entity_id: group.common_area_media_players
        message: >
          There are updates available for Home Assistant.

          {% if states('update.home_assistant_core_update') == "on" %}
            {{ state_attr('update.home_assistant_core_update', 'title') }} has an update available.  Release {{ state_attr('update.home_assistant_core_update', 'latest_version') }} is available for installation.
          {% endif %}

          {% if states('update.home_assistant_operating_system_update') == "on" %}
            {{ state_attr('update.home_assistant_operating_system_update', 'title') }} has an update available.  Release {{ state_attr('update.home_assistant_operating_system_update', 'latest_version') }} is available for installation.
          {% endif %}

          {% if states('update.home_assistant_supervisor_update') == "on" %}
            {{ state_attr('update.home_assistant_supervisor_update', 'title') }} has an update available.  Release {{ state_attr('update.home_assistant_supervisor_update', 'latest_version') }} is available for installation.
          {% endif %}

By the way, you could add another trigger to 8AM with a condition that some of the updates are available, so you get the notification in the morning if some update was made available over night.

Something like this:

alias: HA Update TTS Notifications
description: Automation to notify of HA Core, OS, and Supervisor updates via TTS
trigger:
  - platform: state
    entity_id:
      - update.home_assistant_core_update
      - update.home_assistant_operating_system_update
      - update.home_assistant_supervisor_update
    from: "off"
    to: "on"
  - platform: time
    at: "08:00:01"
condition:
  - condition: time
    after: "08:00:00"
    before: "21:00:00"
  - condition: template
    value_template: >-
      {{ expand(['update.home_assistant_core_update',
      'update.home_assistant_operating_system_update',
      'update.home_assistant_supervisor_update']) |
      selectattr('state','eq','on') | list | count > 0 }}
action:
  - service: tts.google_say
    data:
      cache: false
      entity_id: group.common_area_media_players
      message: >
        There are updates available for Home Assistant.

        {% if states('update.home_assistant_core_update') == "on" %}
          {{ state_attr('update.home_assistant_core_update','title') }} has an update available.  Release {{ state_attr('update.home_assistant_core_update', 'latest_version') }} is available for installation.
        {% endif %}

        {% if states('update.home_assistant_operating_system_update') == "on"  %}
          {{ state_attr('update.home_assistant_operating_system_update','title') }} has an update available.  Release {{ state_attr('update.home_assistant_operating_system_update', 'latest_version') }} is available for installation.
        {% endif %}

        {% if states('update.home_assistant_supervisor_update') == "on" %}
          {{ state_attr('update.home_assistant_supervisor_update','title') }} has an update available.  Release {{ state_attr('update.home_assistant_supervisor_update', 'latest_version') }} is available for installation.
        {% endif %}
mode: single

You still have to include attributes in second and third if statement otherwise it will throw an error

That’s right! Thanks!!!
I’ve fixed into the code I’ve shared above.

I’ve also changed everything to the recommended way of accessing status on templates and it handles errors better and I believe it makes easier to visualize those issues.

1 Like

Thanks @EdwardTFN! I’ve updated the code but it’s still complaining about can not read a block mapping entry; a multiline key may not be an implicit key.

I dropped the code into the template editor and it says TemplateSyntaxError: unexpected '}', expected ')'.

I checked the yaml on yamllint.com to see if I could get more detail, and it comes back with:

Implicit keys need to be on a single line at line 38, column 9
Implicit map keys need to be followed by map values at line 38, column 9

Which of the codes are you trying? And are you copying/pasting exactly as it is?

I’ve tested both and they both work fine. And I have successfully created an automation with the last one:


  • For my test I’ve changed the conditions to show the messages when the updates statuses are off as all my core items are already updated. If I leave ON (as it should be) I still don’t have any error.

Yeah, it’s strange. Your code lines up with mine. The template editor doesn’t show anything is wrong, and I see the bottom updates text when changing the states to “off”. But Studio Code Server is still giving that error after the last {% endif %}.

The full error in Studio Code Server error is:

File ‘automations/system/ha_updates_tts.yaml’ could not be parsed, it was referenced from path ‘automations/system/ha_updates_tts.yaml’.This file will be ignored. Error(s): - YAMLSyntaxError: All collection items must start at the same column - YAMLSemanticError: Implicit map keys need to be followed by map values - YAMLSemanticError: Implicit map keys need to be followed by map values - - And 14 more errors…

If I try a quick reload, I get a different error:

Cannot quick reload all YAML configurations because the configuration is not valid: Error loading /config/configuration.yaml: while scanning a simple key in “/config/automations/system/ha_updates_tts.yaml”, line 39, column 9 could not find expected ‘:’ in “/config/automations/system/ha_updates_tts.yaml”, line 41, column 9

Try sharing the full content of this file so I can try here…

#############################################################
# Home Assistant Update Notifications
# Sent to common area media players (Living Room and Bedroom)
#############################################################

- id: '1677909050513'
  alias: HA Update TTS Notifications
  description: Automation to notify of HA Core, OS, and Supervisor updates via TTS
  mode: single

  trigger:
    - platform: state
      entity_id:
        - update.home_assistant_core_update
        - update.home_assistant_operating_system_update
        - update.home_assistant_supervisor_update
      from: "off"
      to: "on"
    - platform: time
      at: "08:00:01"

  condition: 
    - condition: time
      after: "08:00:00"
      before: "21:00:00"
    - condition: template
      value_template: >-
        {{ expand(['update.home_assistant_core_update',
        'update.home_assistant_operating_system_update',
        'update.home_assistant_supervisor_update']) |
        selectattr('state', 'eq', 'on') | list | count > 0 }}

  action:
    - service: tts.google_say
      data:
        cache: false
        entity_id: group.common_area_media_players
        message: >
        There are updates available for Home Assistant

        {% if states('update.home_assistant_core_update') == "on" %}
          {{ state_attr('update.home_assistant_core_update','title') }} has an update available.  Release {{ state_attr('update.home_assistant_core_update', 'latest_version') }} is available for installation.
        {% endif %}

        {% if states('update.home_assistant_operating_system_update') == "on" %}
          {{ state_attr('update.home_assistant_operating_system_update','title') }} has an update available.  Release {{ state_attr('update.home_assistant_operating_system_update', 'latest_version') }} is available for installation.
        {% endif %}

        {% if states('update.home_assistant_supervisor_update') == "on" %}
          {{ state_attr('update.home_assistant_supervisor_update','title') }} has an update available.  Release {{ state_attr('update.home_assistant_supervisor_update', 'latest_version') }} is available for installation.
        {% endif %}

Looks like an indentation issue…

Try this:

#############################################################
# Home Assistant Update Notifications
# Sent to common area media players (Living Room and Bedroom)
#############################################################

- id: '1677909050513'
  alias: HA Update TTS Notifications
  description: Automation to notify of HA Core, OS, and Supervisor updates via TTS
  mode: single

  trigger:
    - platform: state
      entity_id:
        - update.home_assistant_core_update
        - update.home_assistant_operating_system_update
        - update.home_assistant_supervisor_update
      from: "off"
      to: "on"
    - platform: time
      at: "08:00:01"

  condition: 
    - condition: time
      after: "08:00:00"
      before: "21:00:00"
    - condition: template
      value_template: >-
        {{ expand(['update.home_assistant_core_update',
        'update.home_assistant_operating_system_update',
        'update.home_assistant_supervisor_update']) |
        selectattr('state', 'eq', 'on') | list | count > 0 }}

  action:
    - service: tts.google_say
      data:
        cache: false
        entity_id: group.common_area_media_players
        message: >
          There are updates available for Home Assistant

          {% if states('update.home_assistant_core_update') == "on" %}
            {{ state_attr('update.home_assistant_core_update','title') }} has an update available.  Release {{ state_attr('update.home_assistant_core_update', 'latest_version') }} is available for installation.
          {% endif %}

          {% if states('update.home_assistant_operating_system_update') == "on" %}
            {{ state_attr('update.home_assistant_operating_system_update','title') }} has an update available.  Release {{ state_attr('update.home_assistant_operating_system_update', 'latest_version') }} is available for installation.
          {% endif %}

          {% if states('update.home_assistant_supervisor_update') == "on" %}
            {{ state_attr('update.home_assistant_supervisor_update','title') }} has an update available.  Release {{ state_attr('update.home_assistant_supervisor_update', 'latest_version') }} is available for installation.
          {% endif %}

Thank you so much @EdwardTFN! I guess the old adage “it’s always something simple” applies here. Where was the missing indentation?

On the message block:

The indentation for the if clause is not needed, but everything that belongs to the message will require the indentation.

It was my fault on not been clear at the beginning, as I’ve noticed this in your first post.