๐ฃ 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 โ
| Field | Selector | Required | Default | Description |
|---|---|---|---|---|
speaker | entity | Yes | Fallback media_player used when dynamic routing is disabled or no room matches. | |
text | text | Yes | Message text to speak via TTS. | |
speaker_dinamico | boolean | No | false | Routes playback to areas with active motion, presence, or occupancy sensors. |
modalita_dinamica | select | No | prima_stanza | Multi-room behavior: prima_stanza, tutte_le_stanze, or solo_default. |
๐ How It Works โ
- Dynamic Speaker Discovery: when
speaker_dinamicois enabled, finds all activebinary_sensorentities withmotion,presence, oroccupancydevice classes, maps them to their areas, and selects onemedia_playerper room while filtering out TVs and receivers. - Time-Based Volumes: sets announcement volume to
0.6before22:00or0.3at night, and restores volume afterward to0.1between22:00and09:00or0.5during the day. - Silent Wake-Up & Cleanup: mutes the speaker (
0) before callingmedia_player.turn_onto avoid the cast connection chime, speaks the message withtts.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.
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: {}