Text-to-Speech problem with Chromecast Audio

I ran into a problem using the method with media_player.volume_set.
When trying to set the volume to for example 0.5, when the volume was already 0.5, the issue with the sound beeing cut at the beginning was still present. And setting different sound levels, 0.51 and 0.52 for different TTS announcements feels like a bad idea.

I also tried to mute and unmute the sound before playing the TTS message, but the problem was present there too.

One solution that seem to work though is this:

tts_temp_microsoft:
  alias: Temperatur (Microsoft)
  sequence:
    - service: media_player.play_media
      entity_id: media_player.sovrum
      data:
        media_content_id: https://myserver:8123/local/audio/silence_1sec.mp3
        media_content_type: music
    - delay:
        seconds: 0.1
    - service: tts.microsoft_say
      entity_id: media_player.sovrum
      data_template:
        message: "Det är {{states('sensor.ute_temperature')}} grader ute."

In short:

  1. Play 1 second of silence.
  2. Delay for 0.1 seconds (without this delay, the problem persist)
  3. Then play the TTS announcement.

In combination with the automation from @lolouk44 the Chromecast Audio doesn’t go to sleep and the TTS are played without cut-outs (as of now). I will continue with my testing and let you now of any further failures.

2 Likes

Today I have found yet another way to solve the issue at hand. And it works without both the automation playing a 1 seconds silence every two minutes (which by the way stops the music when playing from a music app on the iPhone) and without changing the volume before every TTS announcment.

For some reason I cannot explain it works perfect :slight_smile: (at least in my setup)

tts_temp_microsoft:
  alias: Temperatur (Microsoft TTS)
  sequence:
    - service: media_player.play_media
      entity_id: media_player.sovrum
      data:
        media_content_id: https://server:8123/local/audio/silence_1sec.mp3
        media_content_type: music
    - wait_template: "{{ states('media_player.sovrum') != 'off' }}"
    - delay:
        seconds: 0.1
    - service: tts.microsoft_say
      entity_id: media_player.sovrum
      data_template:
        message: "Det är {{states('sensor.ute_temperature')}} grader ute."

The secret seem to be to wait until the Chromecast Audio isn’t off followed by a short delay.

If someone could try it out and hopefully have the same luck as I did it would be great.

2 Likes

if you look at my automation it’ll check that the GH is not already playing before it plays the 1 sec audio :wink:
(actually I check that each individual GH and the group with both my GH devices isn’t in playing state )
Your method with work too though you’d have an ever slight delay before you hear the announcement, and most likely you’ll also hear the connection chime which I hate…

if you look at my automation it’ll check that the GH is not already playing before it plays the 1 sec audio

Yes, I noticed that :wink: But that only work if I stream music from Home Assistant to the Chromecast Audio. When I stream music from Spotify and apps alike to the Chromecast, Home Assistant is unaware of the status of the Chromecast Audio. It is seen as off.

Your method with work too though you’d have an ever slight delay before you hear the announcement, and most likely you’ll also hear the connection chime which I hate…

That was the weird thing. When using the wait_template and the delay I don’t hear the “connection chime”, it just starts playing the TTS announcement, perfectly complete. Even if the Chromecast is in the idle or off state.

Maybe you could give it a shot and see how your device behave? There might be some difference in how the Google Home and the Chromecast Audio operate. I would be really interested anyway because I intend to buy a Google Home Mini soon :yum:

Interesting. Google Home acts as a ChromeCast audio so from from HA’s point of view it makes not difference. I actually use ChromeCasts to play music and my GH’s for announcements (and regular interactions)
I’ll see if I get some time to try your method…

1 Like

I tried the wait template. The chromecast audio did go from off to playing without the connection tones.

Also I’m going to try the turn_on service instead of the 1sec silent mp3. e.g.

  action:
  - service: media_player.turn_on
    data:
      entity_id: media_player.entrance
  - wait_template: "{{ states('media_player.entrance') != 'off' }}"
  - service: media_player.volume_set
    data:
      entity_id: media_player.entrance
      volume_level: '0.30'
  - service: media_player.play_media
    data_template:
      entity_id: media_player.entrance
      media_content_id: https:/xxxxx.duckdns.org/local/sounds/Reminder.mp3
      media_content_type: 'audio/mp3'

It looks right and HA config check passes but I’m not there to hear it at the moment so don’t know for sure.

EDIT: this doesn’t work.

1 Like

So I got around to implementing @lolouk44’s connection keep-alive automation.

It does keep my chromecast audio from turning off but I’m still missing the first part of every announcement or TTS message.

I even tried playing the 1 second silence clip before playing the file I wanted. Still clipping the first part of the message.

Can you share your code?

Sure, this is the original, without the extra silence.mp3 played first. I even tried playing the doorbell mp3 twice. No change.

#################################################################
## Audio Player Keep Alive
#################################################################

- id: keep_audio_player_alive
  alias: Keep Audio Player Alive
  trigger:
    platform: time
    minutes: '/2'
    seconds: 00
  condition:
    - condition: template
      value_template: '{{ states.media_player.entrance.state != "playing"}}'
  action:
    service: media_player.play_media
    data:
      entity_id: media_player.entrance
      media_content_id: https://mydomain.duckdns.org/local/sounds/audio1sec.mp3
      media_content_type: music

#################################################################
## Doorbell Automations
#################################################################

- id: doorbell_alert
  alias: 'Doorbell Alert'
  trigger:
    entity_id: binary_sensor.doorbell
    platform: state
    to: 'on'
  action:
  - service: camera.snapshot
    data:
      entity_id: camera.front
      filename: '/config/www/doorbell.jpg'
  - service: notify.all_ios_devices
    data:
      message: Someone has pressed the doorbell.
      data:
        push:
          sound: "Doorbell.wav"
        attachment:
          url: https://mydonmain.duckdns.org/local/doorbell.jpg
  - service: media_player.volume_set
    data:
      entity_id: media_player.entrance
      volume_level: '0.30'
  - service: media_player.play_media
    data:
      entity_id: media_player.entrance
      media_content_id: https://mydomain.duckdns.org/local/sounds/Doorbell2.mp3
      media_content_type: 'audio/mp3'
  - service: automation.turn_off
    entity_id: automation.doorbell_alert
  - delay: 00:00:05
  - service: automation.turn_on
    entity_id: automation.doorbell_alert

I tried with this as well but it made no difference.

      media_content_type: music

not sure why you’re missing the beginning.
Quick question, why do you have this:

  - service: automation.turn_off
    entity_id: automation.doorbell_alert
  - delay: 00:00:05
  - service: automation.turn_on
    entity_id: automation.doorbell_alert

Also I personally find that audio/mp4 is more reliable than other formats. Can you can try for both the doorbell play and the keep alive?
Also you mentioned TTS, but it looks like you’re playing some MP3 file. Where is the TTS?
What happens when you manually play the mp3 on the media_player via the Services tab?
image

So I dont get multiple notifications if some impatient person repeatedly presses the doorbell.

I’ll give it a go.

TTS testing was directly from the media player panel. It misses the first part of the message.

I’m not entirely sure how to specify the service data there. This doesn’t play:

{
  "entity_id": "media_player.entrance",
  "media_content_id": " https://mydomain.duckdns.org/local/sounds/Doorbell2.mp3",
  "media_content_type": "audio/mp3"
}

better add this condition to your automation:

    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(state_attr("automation.doorbell_alert", "last_triggered") | default(0)) | int > 5)}}'

Not that it should, but wondering if setting the volume makes the CC “busy”.
Have you tried to add a delay of like 1 sec between volume set and play? I don’t have that issue personally, but worth a try

For me it’s the same, not entirely sure why. I ended up loading the sound files on my webserver (apache) and linking to it using the server’s IP address rather than going online for it.

I like that! Thanks.

Also did you mean mA4 format?

I don’t seem to be able to export mp4 from audacity.

leave the file in mp3 format, but in your HA command use mp4:

  - service: media_player.play_media
    data:
      entity_id: media_player.entrance
      media_content_id: https://mydomain.duckdns.org/local/sounds/Doorbell2.mp3
      media_content_type: 'audio/mp4'
1 Like

Just as an update, I’ve given up on this.

No matter what I do the chromecast audio always misses the beginning portion of the sound, using TTS or playing a file, if called from an automation or script, even when the media player is on and idle.

I’ve tired all the suggestions listed in the thread above, I’ve improved my wifi (using unifi APs now) I’ve tried caching the audio, not caching, adding up to 5 seconds of silence to the beginning of media files (not a viable solution for a doorbell anyway), and loads more.

Fortunately I have an iPad that I can play sounds attached to messages without problem so I’m going with that for now.

Weird, I have 5 CC Audio, and 2 GH Mini’s around the house, and can play TTS through them fine without missing anything.

Don’t know if it has something to do with you using a DuckDNS address, all mine is handled locally.

I was under the impression that the url had to be accessible from the internet for the media to play?

I remember it took me a while to find the right setup.

I don’t use any port forwarding, or any dynamic DNS on my setup. I access my HA though VPN when I am away from the house.

This is my TTS config,

tts:
  - platform: google
    cache: true
    cache_dir: /config/tts
    time_memory: 300
    language: 'en-uk'

I also have a base url set as my internal ip address as I am running in a docker container,

http:
  api_password: !secret http_password
  base_url: http://10.0.0.2:8123

And that setup seem’s to work fine for me.

This is great. Thanks so much! Was having issues where it would give the notification noise on the Minis but then maybe 1 of them would actually play the notification that I had setup. Now it should play them everywhere and always when they trigger! :+1:

Finally worked out what was wrong with my set up. The powered speaker I plugged my chromecast audio into takes forever to recognise something is playing and unmute.

Nothing wrong with TTS or media player. It was the speaker!

1 Like