Queue TTS automation / Script

Hi guys,

I have found a solution how to queue TTS speech.
W having some tts messages geting aborted.
With these scripts u only have to update 1 action in all your automations

Keep in mind this is for short messages only <255 characters, i totally forget to mention see msg from 123 Queue TTS automation / Script - #2 by 123

:slight_smile:

So i decided to find a solution and i found one .

Tutorial: Setting Up a TTS Queue in Home Assistant

Introduction

Setting up a TTS queue in Home Assistant to ensure that your TTS messages are spoken sequentially without overlapping. This setup uses an input text helper to store the queue and scripts to add and process the messages.

Step 1: Set Up the Input Text Helper

  1. Open Home Assistant.
  2. Navigate to Settings > Devices & Services > Helpers.
  3. Click on Add Helper.
  4. Select Text.
  5. Name it tts_queue.

This helper will be used to store the TTS messages in a queue format.

Step 2: Create the Scripts

Script to Add Messages to the Queue

  1. Go to Settings > Automations & Scenes > Scripts.
  2. Click on Add Script.
  3. Name it add_to_tts_queue.
  4. Use the following YAML code:
add_to_tts_queue:
  sequence:
    - service: input_text.set_value
      data:
        entity_id: input_text.tts_queue
        value: "{{ states('input_text.tts_queue') + ';' + message if states('input_text.tts_queue') else message }}"
    - service: script.turn_on
      data: {}
      target:
        entity_id: script.process_tts_queue

This script appends the message to the tts_queue and triggers the processing script.

Script to Process the TTS Queue

  1. Go back to Settings > Automations & Scenes > Scripts.
  2. Click on Add Script.
  3. Name it process_tts_queue.
  4. Use the following YAML code:
process_tts_queue:
  sequence:
    - repeat:
        while:
          - condition: template
            value_template: "{{ states('input_text.tts_queue') != '' }}"
        sequence:
          - variables:
              messages: "{{ states('input_text.tts_queue').split(';') }}"
              message: "{{ messages[0] }}"
          - service: tts.google_say # also works with cloud of nabucasa
            data:
              entity_id: media_player.announcer
              message: "{{ message }}"
              language:  #change it to yours or leave it empty
          - service: input_text.set_value
            data:
              entity_id: input_text.tts_queue
              value: "{{ ';'.join(messages[1:]) }}"
          - delay: '00:00:10'  # Adjust the delay as needed to prevent overlap

This script processes each message in the queue sequentially, with a delay to prevent overlap.

Step 3: Test the Automation

Create an automation to test the TTS queue:

  1. Go to Settings > Automations & Scenes > Automations.
  2. Click on Add Automation.
  3. Set up the following YAML code:
alias: Test TTS Queue test 1
description: ""
trigger: []
condition: []
action:
  - service: script.add_to_tts_queue
    data:
      message: this is a test  5 4 3 2 1 boom 

mode: single
alias: Test TTS Queue test 2
description: ""
trigger: []
condition: []
action:
  - service: script.add_to_tts_queue
    data:
      message: Your house is not exploded by the way. 

mode: single

Run them both instantly and see it queue! don’t forget to change the delay in the process_tts_queue script!

Explanation

  • add_to_tts_queue Script:

    • This script adds the message to the queue stored in input_text.tts_queue.
    • It then triggers the process_tts_queue script to ensure the message gets processed.
  • process_tts_queue Script:

    • This script processes messages in the queue until it’s empty.
    • It uses a delay between messages to prevent them from overlapping.

So if u have multiple TTS automations just change your current to this one

  - service: script.add_to_tts_queue
    data:
      message: {{yout tts speech text]]

I assume you’re aware that the state value of all entities, including Input Text, is limited to storing 255 characters.

That means the total length of all TTS messages stored in the TTS Queue cannot exceed 255 characters.

In contrast, an entity’s attributes can store about 16K.

Yes i know, this was just a way i did for my <255 character messages.
But indeed you can do this with an entity, but for me it was not needed. :slight_smile:

You can also simply set a script or automation’s mode to queued.

While it’s busy playing one TTS message, subsequent requests to play a TTS message will be automatically queued.