Skip to content

๐Ÿ“ฃ Custom Voice Announcement โ€‹

Plays a custom Text-to-Speech (TTS) announcement while managing silent speaker connection, day/night volume adjustments, and optional dynamic room routing based on live presence sensors (automatically skipping TVs and AV receivers).

โš™๏ธ Parameters โ€‹

FieldSelectorRequiredDefaultDescription
speakerentityYesFallback media_player used when dynamic routing is disabled or no room matches.
texttextYesMessage text to speak via TTS.
speaker_dinamicobooleanNofalseRoutes playback to areas with active motion, presence, or occupancy sensors.
modalita_dinamicaselectNoprima_stanzaMulti-room behavior: prima_stanza, tutte_le_stanze, or solo_default.

๐Ÿ” How It Works โ€‹

  1. Dynamic Speaker Discovery: when speaker_dinamico is enabled, finds all active binary_sensor entities with motion, presence, or occupancy device classes, maps them to their areas, and selects one media_player per room while filtering out TVs and receivers.
  2. Time-Based Volumes: sets announcement volume to 0.6 before 22:00 or 0.3 at night, and restores volume afterward to 0.1 between 22:00 and 09:00 or 0.5 during the day.
  3. Silent Wake-Up & Cleanup: mutes the speaker (0) before calling media_player.turn_on to avoid the cast connection chime, speaks the message with tts.speak, waits 15 seconds, restores the volume, and turns the speaker off.

TTS Target

Before saving this script in your Home Assistant instance, update the target.device_id inside the tts.speak action to match your own TTS engine entity or device.

๐Ÿงช Example Call โ€‹

yaml
action: script.annuncio_vocale_personalizzato
data:
  speaker: media_player.kitchen_speaker
  text: "The washing machine cycle is complete."
  speaker_dinamico: true
  modalita_dinamica: prima_stanza

๐Ÿ“œ Full Script YAML โ€‹

Import in Home Assistant

Go to Settings -> Automations & Scenes -> Scripts -> Add Script -> Edit in YAML and paste the configuration below. Use the language selector to switch between English and Italian.

Custom Voice AnnouncementYAML
alias: ๐Ÿ“ฃ Custom voice announcement
description: >-
  Manages connection, volume, and playback of a custom voice announcement on a
  speaker, with dynamic presence-based routing (excluding TVs).
icon: m3o:nest-audio
fields:
  speaker:
    name: ๐Ÿ“ข Default Speaker
    description: >-
      Select the default speaker (used as fallback or when dynamic routing is
      disabled)
    required: true
    selector:
      entity:
        filter:
          domain: media_player
  text:
    name: ๐Ÿ“ƒ Message text
    description: Text to play on the speaker
    required: true
    selector:
      text: {}
  speaker_dinamico:
    name: ๐Ÿง  Dynamic Speaker Routing
    description: >-
      When enabled, plays the announcement in the room where presence is
      detected
    default: false
    selector:
      boolean: {}
  modalita_dinamica:
    name: ๐Ÿ”„ Multiple Presence Mode
    description: >-
      Choose what happens when people are detected in multiple rooms at the same
      time
    default: prima_stanza
    selector:
      select:
        mode: dropdown
        options:
          - label: Play only in the first detected room
            value: prima_stanza
          - label: Play in all occupied rooms
            value: tutte_le_stanze
          - label: Use the default speaker
            value: solo_default
sequence:
  - variables:
      speakers_list: |
        {% if speaker_dinamico %}
          {% set device_classes = ['motion', 'presence', 'occupancy'] %}

          {# Find all currently active presence sensors #}
          {% set active_sensors = states.binary_sensor 
            | selectattr('state', 'eq', 'on') 
            | selectattr('attributes.device_class', 'defined')
            | selectattr('attributes.device_class', 'in', device_classes)
            | map(attribute='entity_id') | list %}

          {# Extract areas linked to these sensors #}
          {% set ns_area = namespace(areas=[]) %}
          {% for s in active_sensors %}
            {% set a = area_id(s) %}
            {% if a and a not in ns_area.areas %}
              {% set ns_area.areas = ns_area.areas + [a] %}
            {% endif %}
          {% endfor %}

          {# Scan areas to find valid speakers #}
          {% set ns_speakers = namespace(found=[]) %}
          {% for area in ns_area.areas %}
            {% set current_players = namespace(entities=[]) %}

            {% for p in area_entities(area) if p.startswith('media_player.') %}
              {% set s = states[p] %}
              {% if s is not none %}
                {% set fn = s.attributes.friendly_name | default('') | lower %}
                {% set dc = s.attributes.device_class | default('') %}

                {# Anti-TV filters #}
                {% if 'tv' not in fn 
                    and dc not in ['tv', 'receiver'] 
                    and 'source_list' not in s.attributes 
                    and 'activity_list' not in s.attributes 
                    and 'current_activity' not in s.attributes %}
                  {% set current_players.entities = current_players.entities + [p] %}
                {% endif %}
              {% endif %}
            {% endfor %}

            {# If a valid speaker is found in this area, add it to the list #}
            {% if current_players.entities | length > 0 %}
              {% set ns_speakers.found = ns_speakers.found + [current_players.entities | first] %}
            {% endif %}
          {% endfor %}

          {# Output handling #}
          {% if ns_speakers.found | length > 0 %}
            {% if ns_speakers.found | length > 1 and modalita_dinamica == 'solo_default' %}
              {{ [speaker] }}
            {% elif modalita_dinamica == 'tutte_le_stanze' %}
              {{ ns_speakers.found }}
            {% else %}
              {{ [ns_speakers.found | first] }}
            {% endif %}
          {% else %}
            {{ [speaker] }}
          {% endif %}
        {% else %}
          {{ [speaker] }}
        {% endif %}
      volume_notifica: |
        {% if now().hour < 22 %}
          0.6
        {% else %}
          0.3
        {% endif %}
      volume_ripristino: |
        {% if now().hour >= 22 or now().hour < 9 %}
          0.1
        {% else %}
          0.5
        {% endif %}
    alias: ๐Ÿงฉ Compute target speakers and volumes
  - alias: ๐Ÿ”‰ Set connection volume (mute initial chime)
    target:
      entity_id: '{{ speakers_list }}'
    data:
      volume_level: 0
    action: media_player.volume_set
  - delay:
      seconds: 1
  - action: media_player.turn_on
    target:
      entity_id: '{{ speakers_list }}'
    alias: ๐Ÿ”› Connect to speaker
    data: {}
  - delay:
      seconds: 1
  - alias: ๐Ÿ”‰ Set announcement volume
    target:
      entity_id: '{{ speakers_list }}'
    data:
      volume_level: '{{ volume_notifica }}'
    action: media_player.volume_set
  - alias: ๐Ÿ—ฃ๏ธ Play voice announcement via TTS
    target:
      device_id: 285d5f99189ca15b66cf4a976e13783c
    data:
      cache: true
      message: '{{ text }}'
      media_player_entity_id: '{{ speakers_list }}'
    action: tts.speak
  - delay:
      seconds: 15
  - alias: ๐Ÿ”„ Restore initial volume
    target:
      entity_id: '{{ speakers_list }}'
    data:
      volume_level: '{{ volume_ripristino }}'
    action: media_player.volume_set
  - action: media_player.turn_off
    target:
      entity_id: '{{ speakers_list }}'
    alias: ๐Ÿ“ด Disconnect from speaker
    data: {}