Tts google_say (template) problem

HI,

Only just beginning to experiment with tts and my new Google Homes, I made this script:

script:
  send_intercom:
    alias: 'Send Intercom'
    sequence:
      - service: tts.google_say
        data_template:
          language: >
            {{states('input_select.language')|lower}}
          entity_id: >
           {% if is_state('input_select.googlehome', 'Woonkamer') %} media_player.googlehome_woonkamer
           {% elif is_state('input_select.googlehome', 'Hobbykamer') %} media_player.googlehome_hobbykamer
           {% elif is_state('input_select.googlehome', 'Broadcast (all)') %} group.broadcast
           {% endif %}
          message: >
            {{states('input_text.intercom_message')}}
#          volume: >
#            {{  states('input_number.intercom_volume')|int }}

with this input_select:

input_select:
  googlehome:
    name: 'Select Google home:'
    options:
      - Woonkamer
      - Hobbykamer
      - Broadcast (all)
    initial: Woonkamer
    icon: mdi:google-home

if I select the group.broadcast, my message is sent alright, but if I select an individual media_player (which does exist, well obviously because the group is made up of these players) nothing happens.

  broadcast:
    name: 'Broadcast'
    entities:
      - media_player.googlehome_woonkamer
      - media_player.googlehome_hobbykamer

The template is correct, so please help me why this wouldnt be alright?

btw, Ive commented out the volume for now, because I suspect that to be incompatible with google home? anyone with a tip about that?

I ask because I made another script which works as expected, announce all my presence movements just fine, with the same media_players


  googlehome_announce:
    alias: 'Googlehome announce'
    sequence:
      - service: tts.google_say
        data_template:
          language: en
          entity_id: media_player.googlehome_woonkamer, media_player.googlehome_hobbykamer
          message: >
            {% set name = states[entityid.split('.')[0]][entityid.split('.')[1]].name %}
            {{as_timestamp(now()) | timestamp_custom('%X') }} :
            {% if tostate == 'not_home' %}
              {{-name }} left {{fromstate}}
            {% elif fromstate == 'not_home' %}
              {{-name }} arrived at {{tostate}}
            {% else %}
              {{-name }} left {{fromstate}} and arrived at {{tostate}}
            {% endif %}

thanks

I am using the following script which works for a single media player.

  speak_message:
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.google_home
          volume_level: 0.2
      - service: tts.google_say
        entity_id: media_player.google_home
        data_template:
          message: "{{ message }}"
          cache: false
          language: 'nl'

thanks, hadn’t realized I had to set the volume in a separate service. will add

cool, this works:

16

basic (beginners) package:

##############################################################################################################
# Package for intercom and notification messaging using Google Home media_players and Tts
# 20190402 @mariusthvdb
# version 0.03 .....
# https://www.home-assistant.io/components/media_player
# https://www.home-assistant.io/components/tts.google/
##############################################################################################################

homeassistant:
  customize:
    script.intercom_message:
      action_name: 'Announce'
      icon: mdi:text-to-speech

    input_select.language:
      templates:
        entity_picture: >
          return '/local/flags/' + state + '.png';

    input_number.intercom_volume:
      templates:
#        icon: >
#          return 'mdi:numeric-' + state*10;
        icon_color: >
          if (state === '0.0') return 'rgb(145,227,237)';
          if (state <= '0.1') return 'rgb(105,212,231)';
          if (state <= '0.2') return 'rgb(46,147,238)';
          if (state <= '0.3') return 'rgb(0,220,16)';
          if (state <= '0.4') return 'rgb(0,195,13)';
          if (state <= '0.5') return 'rgb(250,255,0)';
          if (state <= '0.6') return 'rgb(252,150,0)';
          if (state <= '0.7') return 'rgb(218,53,0)';
          if (state <= '0.8') return 'rgb(255,0,0)';
          if (state <= '0.9') return 'rgb(154,0,0)';
          if (state <= '1.0') return 'rgb(81,0,0)';

##############################################################################################################
# Component
##############################################################################################################

tts:
  - platform: google
    language: 'nl'
  - platform: google
    language: 'en'

##############################################################################################################
# Groups
##############################################################################################################

group:
  intercom:
    name: 'Intercom'
    entities:
      - input_text.message
      - input_select.googlehome
      - input_number.intercom_volume
      - input_select.language
      - script.intercom_message

  broadcast:
    name: 'Broadcast'
    control: hidden
    entities:
      - media_player.googlehome_woonkamer
      - media_player.googlehome_hobbykamer
      - media_player.googlehome_hall

##############################################################################################################
# Inputs
##############################################################################################################

input_select:
  googlehome:
    name: 'Select Google home:'
    options:
      - Woonkamer
      - Hobbykamer
      - Hall
      - Broadcast (all)
    initial: Woonkamer
    icon: mdi:google-home

  language:
    name: Language
    options:
      - En
      - De
      - Nl
      - It
      - Fr

input_text:
  message:
    name: 'Message:'

input_number:
  intercom_volume:
    name: Volume
    icon: mdi:volume-high
    #initial: 0.5
    min: 0
    max: 1
    step: 0.05
    unit_of_measurement: '%'

##############################################################################################################
# Scripts
##############################################################################################################

script:
  intercom_message:
    alias: 'Intercom message'
    sequence:
      - service: tts.google_say
        data_template:
          language: >
            {{states('input_select.language')|lower}}
          entity_id: >
            {% set state = states('input_select.googlehome') %}
            {% if state in ['Woonkamer','Hobbykamer','Hall'] %} media_player.googlehome_{{(state)|lower}}
            {% else %} group.broadcast
            {% endif %}
          message: >
            {{states('input_text.message')}}

##############################################################################################################
# Automations
##############################################################################################################

automation:
  - alias: 'Set Google Home volume'
    id: 'Set Google Home volume'
    trigger:
      platform: state
      entity_id: input_number.intercom_volume
    condition: []
    action:
      service: media_player.volume_set
      data_template:
        entity_id: >
            {% set state = states('input_select.googlehome') %}
            {% if state in ['Woonkamer','Hobbykamer','Hall'] %} media_player.googlehome_{{(state)|lower}}
            {% else %} group.broadcast
            {% endif %}
        volume_level: >
          {{  states('input_number.intercom_volume')|float }}

always funny to have these languages talk other languages :wink: Not sure I need the extra tts languages installed, since I now only have en and nl installed, and they still speak French, Italian and German 
?

3 Likes

Does it works the template if you put something like " Message {{ states.sensor.x.state}} "?
Was it for me but not anymore

yes, all my templated notifications are spoken over tts just fine. I do notice the system taking its time now and then, so if they are time critical, you have to be careful

hello,
sorry, but i don get it. :grimacing:

i have an automation for greating guests. so if they login via guest wifi (unifi contoller), i want to say hello.

i added an automation via gui, and changed to YAML Editor for the “action” section:

data_template:
  entity_id: media_player.kuche
  language: de
  message: |
    Hallo {{ trigger.entity_id }} willkommen in unserem Zuhause
service: tts.google_say

but google home remains quiet. if i change “data_template” to “data” google home at least announces the message (just without adding the correct value)

Post your full automation please , and, you probably need
trigger.to_state.entity_id

this is my proof of concept :wink:

- id: '1587576840466'
  alias: BegrĂŒĂŸe GĂ€ste
  description: ''
  trigger: []
  condition:
  - condition: device
    device_id: 660f8e0c58604c238984b6702690ae1b
    domain: device_tracker
    entity_id: device_tracker.pixel2
    type: is_home
  action:
  - data_template:
      entity_id: media_player.kuche
      language: de
      message: 'Hallo {{ trigger.entity_id }} willkommen in unserem Zuhause'
    service: tts.google_say

but there’s no trigger, so this wont ever, well, trigger


you should add the devices you want to be tracked as entity_id’s in the trigger block, with state to: ‘home’.
no condition (for now)
and then it will trigger the action

if you want I could post my automation, but maybe you have enough now to figure it out for your self, which is often the better way


thank you! ok. now, i basically understand the difference (trigger / condition)
sorry, i have only basic programming knowledge and this is one of my first automations / scripts.

maybe you can help me a bit?

- id: '1587576840466'
  alias: BegrĂŒĂŸe GĂ€ste
  description: ''
  trigger:
  - entity_id: device_tracker.holger_janning_s_pixel_2_xl
    platform: state
    to: home
  - entity_id: device_tracker.honor_8
    platform: state
    to: home
  condition: []
  action:
  - data_template:
      entity_id: media_player.kuche
      language: de
      message: Hallo {{ trigger.entity_id }} willkommen in unserem Zuhause
    service: tts.google_say

you can list the entity_ids under the same trigger platform, and use the correct trigger in the data_template, try this:

- id: '1587576840466'
  alias: BegrĂŒĂŸe GĂ€ste
  description: ''
  trigger:
    platform: state
    entity_id: 
      - device_tracker.holger_janning_s_pixel_2_xl
      - device_tracker.honor_8
    to: home
  condition: []
  action:
    service: tts.google_say
    data_template:
      entity_id: media_player.kuche
      language: de
      message: >
        Hallo {{ trigger.to_state.attributes.name }} willkommen in unserem Zuhause

just so you know wifi triggers are always very peculiar without further conditions, and keep switching on/off all the time for power savings, add this condition to prevent unneeded triggering:

      - condition: template
        value_template: >
          {{trigger.to_state.state is not none and
            trigger.from_state.state is not none and
            trigger.to_state.state != trigger.from_state.state}}

Great, thank you! It does work. i just found out, that the gui trigger-button in automation editor was not working as expected (in my basic knowledge). i had to manually set the “state” to home to test the automation :wink: thanks for your patience and your help!

iÄșl try to add the condition as well!

please note that the Gui automation editor is known for creating terrible yaml
 Is orders things in a peculiar way, and, when you want to get more detailed automations, simply doesnt fit the bill.

Best to learn how to write automations yourself, you’ll find yourself needing more than basic if then else logic soon :wink:

ok. could you help me, to find an easy documentation how the syntax works?

i do not understand when and how far I have to indent certain lines. sometimes i see examples with a “dash” in front - sometimes not. i don get the logic behind that whole yaml syntax.

so many references, start here and google
https://yaml.org/spec/1.2/spec.html and here of course

default in Yaml seems to be 2 spaces, you best check the documentation on the yaml syntax per integration. But, as long as your indents are the same for the same logic, it seems to work fine. I like to use 2, and it makes it easy for my eyes to spot the errors


the dash is an indicator for a ‘list’. If you only have 1, you dont need a list, and dont need the dash. As in the automation I posted. I love to keep my syntax as short as possible, so never use a dash before a single entity (you can use a dash, but it isn’t necessary)

if you need more entities, like in the trigger for the platform: state, you do need the dashes, to indicate multiple entities.

the other important thing in this automation, is the notion of a ‘data_template:’ . This is necessary when using templates (seems very logical
). If you dont use a template you can use ‘data:’

lastly, one of my own preferences: use multi-line notation for templates.

this is indicated by the > after the template in the automation. It means everything after the > on the next line and following will be interpreted. This takes out the need for ‘quoting’ the template, which in itself makes the quotes inside the template easier.

without the multiline indicator, you’d have to quote the template and use different inner quotes.

eg

condition: template
value_template: >
  {{is_state('device_tracker.honor_8','home')}}

versus

condition: template
value_template: "{{is_state('device_tracker.honor_8','home')}}"

this is a simple template, but believe me, they can become more complex easily, and you’ll be glad to take out each difficulty possible.

have faith!

1 Like

Hi Marius,

Heb jij toevallig ook de front-end code ? vooral benieuwd naar jouw announce button :slight_smile:

Alvast bedankt !

Grtn,

Bart

hey weemaaba,

what exactly dont you understand about that button? I fear its nothing more than:

      - entity: script.intercom_text_message
        action_name: Send

currently