jcallaghan / home-assistant-config

My Home Assistant configuration & documentation.
https://www.jcallaghan.com/
MIT License
173 stars 8 forks source link

Text-to-speech announcement Lovelace card ⌨🔊💬🏠 🏃‍♀️ #202

Open jcallaghan opened 4 years ago

jcallaghan commented 4 years ago

Follow Matt's (@Brunty) Lovelace card example that allows text-to-speech messages to easily be broadcasted to speakers throughout the house.

image

jcallaghan commented 4 years ago

Here is the Gist Matt shared - https://gist.github.com/Brunty/df7c0c9afae703cf0f16dc3ddfb9d244

Config

# https://github.com/snarky-snark/home-assistant-variables
var:
    media_volume_living_room:
        friendly_name: "Living Room Media Volume"
        initial_value: 0
    media_volume_matt_office:
        friendly_name: "Matt Office Media Volume"
        initial_value: 0

Lovelace

cards:
  - entities:
      - entity: input_select.announce_to
        name: Announce to?
      - entity: input_number.announcement_volume
        name: Volume
      # https://github.com/gadgetchnnel/lovelace-text-input-row
      - entity: input_text.home_announcement_message
        name: Message
        type: 'custom:text-input-row'
      - action_name: Announce it
        icon: 'mdi:voice'
        icon_height: 60px
        name: ' '
        service: script.announce_message
        type: call-service
    type: entities
title: null
type: 'custom:vertical-stack-in-card'

Script

'announce_message':
    alias: Announce message
    sequence:
        # Store the current volumes of the media players
        - service: var.set
          entity_id: 'var.media_volume_living_room'
          data:
              entity_id: 'var.media_volume_living_room'
              value_template: "{{ state_attr('media_player.living_room_mini_speaker', 'volume_level')|float }}"
        - service: var.set
          entity_id: 'var.media_volume_matt_office'
          data:
              entity_id: 'var.media_volume_matt_office'
              value_template: "{{ state_attr('media_player.matt_office_mini_speaker', 'volume_level')|float }}"
        - service: media_player.volume_set
          data_template:
              entity_id: >
                  {%- if is_state('input_select.announce_to', 'Everywhere') -%}
                    media_player.living_room_mini_speaker, media_player.matt_office_mini_speaker
                  {%- elif is_state('input_select.announce_to', 'Living Room') -%}
                    media_player.living_room_mini_speaker
                  {%- elif is_state('input_select.announce_to', 'Matt Office') -%}
                    media_player.matt_office_mini_speaker
                  {%- endif -%}
              volume_level: "{{ states('input_number.announcement_volume')|float }}"
        - data_template:
              cache: false
              message: "{{ states('input_text.home_announcement_message') }}"
              entity_id: >
                  {%- if is_state('input_select.announce_to', 'Everywhere') -%}
                    media_player.living_room_mini_speaker, media_player.matt_office_mini_speaker
                  {%- elif is_state('input_select.announce_to', 'Living Room') -%}
                    media_player.living_room_mini_speaker
                  {%- elif is_state('input_select.announce_to', 'Matt Office') -%}
                    media_player.matt_office_mini_speaker
                  {%- endif -%}
          service: tts.google_translate_say
        - delay: 00:00:10
        # Set volumes back to what they were before
        - service: media_player.volume_set
          data_template:
              entity_id: media_player.living_room_mini_speaker
              volume_level: "{{ states('var.media_volume_living_room')|float }}"
        - service: media_player.volume_set
          data_template:
              entity_id: media_player.matt_office_mini_speaker
              volume_level: "{{ states('var.media_volume_matt_office')|float }}"
Gist
Message announcement in HA
Message announcement in HA. GitHub Gist: instantly share code, notes, and snippets.