Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save imthenachoman/031ea415fb06872f70684f5d63b252cf to your computer and use it in GitHub Desktop.

Select an option

Save imthenachoman/031ea415fb06872f70684f5d63b252cf to your computer and use it in GitHub Desktop.
Home Assistant Scripts
# This script will send a message to idle Google Home speakers (with an optional speaker filter) and to mobile phones.
alias: TTS To Idle Google Speakers and Phones
description: >-
This script will send a message to idle Google Home speakers (with an optional
speaker filter) and to mobile phones.
fields:
message:
name: Message
description: The message to announce
required: true
selector:
text: null
labels:
selector:
select:
multiple: false
options:
- house alert L1
- house alert L2
- house alert L3
name: Google Home Speaker Label Filter
required: false
description: Optionally filter on Google Home Speakers in a specific label.
sequence:
- variables:
tts_message: "{{ message }}"
label_filter: "{{ label | default('', true) | lower | replace(' ', '_') }}"
idle_matched_speakers: >
{% set matched = namespace(speakers=[]) %} {% for entity in
states.media_player %}
{% set is_speaker = entity.attributes.device_class == 'speaker' %}
{% set is_idle = entity.state in ['idle', 'paused', 'off'] %}
{% set dev_id = device_attr(entity.entity_id, 'id') %}
{% set dev_labels = labels(dev_id) %}
{% set label_matches = (label_filter == '') or (label_filter in dev_labels | map('lower') | list) %}
{% if is_speaker and is_idle and label_matches %}
{% set matched.speakers = matched.speakers + [entity.entity_id] %}
{% endif %}
{% endfor %} {{ matched.speakers }}
- condition: template
value_template: "{{ idle_matched_speakers | length > 0 }}"
enabled: true
- repeat:
for_each: "{{ idle_matched_speakers }}"
sequence:
- action: media_player.volume_set
metadata: {}
data:
volume_level: 0.6
target:
entity_id: "{{ repeat.item }}"
- action: tts.speak
metadata: {}
data:
cache: true
media_player_entity_id: "{{ repeat.item }}"
message: "{{ tts_message }}"
target:
entity_id: tts.google_en_com
enabled: true
enabled: true
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
enabled: true
- repeat:
for_each: "{{ idle_matched_speakers }}"
sequence:
- action: media_player.volume_set
metadata: {}
data:
volume_level: 0.4
target:
entity_id: "{{ repeat.item }}"
- action: media_player.turn_off
metadata: {}
data: {}
target:
entity_id: "{{ repeat.item }}"
enabled: true
enabled: true
- action: notify.notify
data:
title: Home Assistant Alert
message: "{{ tts_message }}"
mode: restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment