Skip to content

Instantly share code, notes, and snippets.

@Didgeridrew
Last active October 21, 2024 02:02
Show Gist options
  • Save Didgeridrew/1c6741a6046fb2f738bda0c1cee4b782 to your computer and use it in GitHub Desktop.
Save Didgeridrew/1c6741a6046fb2f738bda0c1cee4b782 to your computer and use it in GitHub Desktop.
Home Assistant Alexa Command Console and TTS Card
# This is a variation of Alexa Media Player - Broadcasting with a custom card (https://github.com/custom-components/alexa_media_player/wiki/Examples:-Broadcasting-with-a-custom-card)
### PRE-REQUISITES -------------------------
# You will need to install the following integrations and add-ons:
# - Alexa Media Player (https://github.com/custom-components/alexa_media_player)
# - Text-input-row card *(only needed for the example card)
# https://github.com/gadgetchnnel/lovelace-text-input-row
#
# You will need to create the following helpers:
# - input_select.alexa_state
# This input_select needs to have an entry for every echo device and/or group that you want to use.
# You can make these entries whatever you want, we will convert them to the entity_ids with the alexa_selector sensor
# - input_text.cmd_text
# - input_text.announcement_text
### Template Sensor -----------------------------
template:
sensor:
- name: "Alexa Selector"
state: >-
{% set room = states('input_select.alexa_state') -%}
{% set echo_select = {
'Kitchen Dot: 'kitchen_dot',
'All': 'everywhere',
'Living Room Dot': 'a_s_living_room_dot',
'Upstairs Bathroom Dot': 'upstairs_bathroom_dot'
} %}
{%- if room in echo_select -%}
media_player.{{ echo_select.get(room) }}
{%- endif %}
# NOTE:
# You can use the following template in the Developer Tools template tool
# to iterate over your Alexa devices and return the value to paste into the
# template sensor above for the value of echo_select:
# {% for a in integration_entities('alexa_media')
# | select('match', 'media_player.') if states[a].attributes.source %}
# "{{ states[a]['name']}}": "{{ states[a]['object_id'] }}",
# {%- endfor %}
### SCRIPTS -----------------------------
# Most Lovelace/Dashboard Cards do not accept templates,
# so the logic and templating needs to be placed in a script.
# Then you can call the script from the card.
script:
send_alexa_command:
alias: Send Alexa command
sequence:
- service: media_player.play_media
data:
entity_id: '{{ states("sensor.alexa_selector") }}'
media_content_id: '{{ states("input_text.cmd_text") }}'
media_content_type: custom
mode: single
send_alexa_announcement:
alias: Send Alexa Announcement
fields:
type:
name: Type
description: Select between either tts or announcement
default: announce
sequence:
- service: homeassistant.update_entity
data: {}
target:
entity_id: input_select.alexa_state
- service: homeassistant.update_entity
data: {}
target:
entity_id: input_text.announcement_text
- service: notify.alexa_media
data:
target: "{{ states('sensor.alexa_selector') }}"
message: "{{ states('input_text.announcement_text') }}"
data:
type: "{{ type }}"
mode: single
stop_alexa_command:
alias: STOP Alexa command
sequence:
- service: media_player.play_media
data:
entity_id: '{{ states("sensor.alexa_selector") }}'
media_content_id: stop
media_content_type: custom
mode: single
### SAMPLE LOVELACE CARD #1-----------------------------
type: entities
entities:
- entity: input_select.alexa_state
name: Select Alexa device
- entity: input_text.cmd_text
name: 'Type question/command here:'
type: 'custom:text-input-row'
- action_name: Run Command/Ask Question...
icon: 'mdi:voice'
name: ' '
service: script.send_alexa_command
type: call-service
- action_name: Stop Alexa
icon: 'mdi:voice'
name: ' '
service: script.stop_alexa_command
type: call-service
- entity: input_text.announcement_text
name: 'Type announcement text here:'
type: 'custom:text-input-row'
- type: call-service
name: ' '
icon: 'mdi:voice'
action_name: Announce It...
service: script.send_alexa_announcement
service_data:
type: announce
### Sample Card 2
# Requires Mushroom and Stack-in-card custom cards available in HACS
# Adds separate buttons so a message can be sent as either a plain TTS or announcement
#
# Note: If you have speaker groups included in your target you must use the
# announcement button when sending to those groups.
type: custom:stack-in-card
cards:
- type: custom:mushroom-title-card
title: Alexa Console
- type: entities
entities:
- entity: input_select.alexa_state
name: Select Alexa Device
- entity: input_text.cmd_text
name: 'Type question/command here:'
type: custom:text-input-row
- type: grid
columns: 2
square: false
cards:
- type: button
color_type: theme
icon_height: 28px
icon: mdi:close-octagon-outline
tap_action:
action: call-service
service: script.stop_alexa_command
- type: button
color_type: theme
icon: mdi:apple-keyboard-command
icon_height: 28px
tap_action:
action: call-service
service: script.send_alexa_command
- entities:
- entity: input_text.announcement_text
name: 'Type announcement text here:'
type: custom:text-input-row
type: entities
header:
type: buttons
entities: []
- type: grid
columns: 2
square: false
cards:
- type: button
color_type: theme
icon_height: 28px
icon: mdi:account-voice
tap_action:
action: call-service
service: script.send_alexa_announcement
data:
type: tts
- type: button
color_type: theme
icon: mdi:bullhorn-outline
icon_height: 28px
tap_action:
action: call-service
service: script.send_alexa_announcement
data:
type: announce
@Ni3kjm
Copy link

Ni3kjm commented Apr 14, 2023

this is great thanks for this, exactly what i was looking for, unfortunately giving me a head-scratcher. it works initially then seems to change all my scripts for example from -

      data:
        data:
          type: announce
          method: speak
        target: '{{ states("sensor.alexa_selector") }}'
        message: '{{ states("input_text.announcement_text") }}'

to

    data:
      data:
        type: announce
        method: speak
      target:
        target:
          "[object Object]": null
        message:
          "[object Object]": null 

or

alias: Send Alexa Command
sequence:
  - service: media_player.play_media
    data:
      entity_id:
        "[object Object]": null
      media_content_id:
        "[object Object]": null
      media_content_type: custom

any ideas why this might happen?

@Didgeridrew
Copy link
Author

Usually that is a sign that you have improperly used the same type of quotation marks both inside and outside the template.

@GLightstone
Copy link

GLightstone commented Oct 19, 2024

Trying to enter in the script, but I keep getting this error:

Message malformed: extra keys not allowed @ data['send_alexa_command']

Section I'm copying:

SCRIPTS -----------------------------

Most Lovelace/Dashboard Cards do not accept templates,
so the logic and templating needs to be placed in a script.
Then you can call the script from the card.

send_alexa_command:
alias: Send Alexa command
sequence:
- service: media_player.play_media
data:
entity_id: '{{ states("sensor.alexa_selector") }}'
media_content_id: '{{ states("input_text.cmd_text") }}'
media_content_type: custom
mode: single

Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment