Let's make the rabbit talk! (•ㅅ•)

Hi there guys,
I’m trying to use the Google TTS to make my smart rabbit talk but I’m not sure if it’s possible.

image

I know that every time I use the Google TTS, the component creates a MP3 file located in a directory we can set on declaring the component, so my TTS configuration is like

tts:
  - platform: google
    cache: true
    cache_dir: /config/www/vl/voice

Now, I can play audio files on the rabbit using shell commands like:

shell_command:
  nab_talk: curl http://smartrabbitip/play?u=http://homeassistantip:8123/local/vl/voice/generatedbygooglefile.mp3

So, my only problem right now is:

How can I get the name of the file generated by the Google TTS?

Usually those files have names like “f1e5cc9b91711e1c95a3e32c8d3f716324cdbc63_it_-_google.mp3” but I have no idea on how to get the file name.

If I look at the logger there I can see this:

2018-01-21 12:10:00 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=media_player, service=play_media, service_data=media_content_id=http://hassioserver.duckdns.org:8123/api/tts_proxy/f1e5cc9b91711e1c95a3e32c8d3f716324cdbc63_it_-_google.mp3, media_content_type=music, entity_id=['media_player.nabaztag'], service_call_id=1965747120-71>
2018-01-21 12:10:00 DEBUG (MainThread) [homeassistant.components.api] STREAM 1913695816 FORWARDING <Event call_service[L]: domain=media_player, service=play_media, service_data=media_content_id=http://hassioserver.duckdns.org:8123/api/tts_proxy/f1e5cc9b91711e1c95a3e32c8d3f716324cdbc63_it_-_google.mp3, media_content_type=music, entity_id=['media_player.nabaztag'], service_call_id=1965747120-71>
2018-01-21 12:10:00 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1965747120-71>
2018-01-21 12:10:00 DEBUG (MainThread) [homeassistant.components.api] STREAM 1913695816 WRITING data: {"event_type": "call_service", "data": {"domain": "media_player", "service": "play_media", "service_data": {"media_content_id": "http://hassioserver.duckdns.org:8123/api/tts_proxy/f1e5cc9b91711e1c95a3e32c8d3f716324cdbc63_it_-_google.mp3", "media_content_type": "music", "entity_id": ["media_player.nabaztag"]}, "service_call_id": "1965747120-71"}, "origin": "LOCAL", "time_fired": "2018-01-21T11:10:00.082612+00:00"}

1 Like

This might work.

Replace your shell command with a call to this bash script

#!/bin/bash

newest="$(printf "%s\n" *.mp3 | head -1)"
curl http://smartrabbitip/play?u=http://homeassistantip:8123/local/vl/voice/{$newest}

It would need to be in the cache directory. It will find the name of the newest .mp3 in that directory and append it to the curl command.

Ok, I forgot to mention that I’m on HASSIO, so, running a bash script is not really simple but… let me try.
Thank you very much @w1ll1am23

Oh yes, it works!!! The rabbit is alive!!!
I had to edit the file like this:

#!/bin/bash
cd /config/www/vl/voice/
newest="$(printf "%s\n" *.mp3 | head -1)"
curl http://smartrabbitip/play?u=http://homeassistantip:8123/local/vl/voice/{$newest}

but now it’s working. Thank you again @w1ll1am23 :wink:

uh oh, no, wait, it keeps reading only the first file generated…
If I tell the rabbit to say something else it keeps reading the first one it said…

Hmm well I guess the easiest thing to do would be to delete the file after the curl command?

Maybe do this

sleep 3
rm $newest

Ok, now, what I found out is that the Google TTS component doesn’t produce a new mp3 file if the same sentence has already been pronounced. So, deleting the files it’s not a good idea because if I want to use the rabbit in an automation (for example every time someone rings the doorbell it should say “There is someone at the door”), it will pronounce the sentence only the first time.
I know that if I use the Google TTS with a chromecast, I can read the file name in the media player attributes, so I tried to make a fake universal player like this:

media_player:
  - platform: universal
    name: nabaztag
    commands:
      turn_on:
        service: shell_command.nab_talk
      turn_off:
        service: shell_command.nab_stop

but unfortunately the TTS doesn’t write the file name in this fake media player. :sob:

hmmm so if you delete the file will it not recreate it next time?

Yes, exactly. So, if i use:

newest="$(printf "%s\n" *.mp3 | head -1)"

or

newest="$(ls -1t | head -1)"

it will read the correct file only if it has been generated for the first time
If I delete all the mp3 files I will not be able to read for more than 1 time the same sentence…

So does the cache file never get created again? How does it know that sentence was created?

I’m not sure, but I guess it’s all about the file name which is coded using the sentence I’m going to say using the TTS.
I mean, if I tell to the Google TTS to say:
“Hello”
it creates a file named:
“afef6840d5f38128127d70eb9c8f729a56297104_it_-_google”
so, every time I say “Hello” in the same day (or hour… but I don’t know how long is the time I should wait until it creates a new file), it creates a file with the same name. If it’s already been sent to my ip, probably, it’s not going to send it again, but it says to the (chromecast) player to play that file.
If I change just one character, the file name changes.

Try calling the service tts.clear_cache as the last action in your automation. Not the ideal workaround but should work for your purpose.

Ok, I’ll try @ThePapaMaan, thank you