the new text-to-speech functionality is great, but using it on SONOS can be a little bit annoying, since the Speaker always “remembers” the last played media. So if you say something e.g in the kitchen over TTS and then go back to the kicthen and press play on SONOS to listen to music witch was load previously, it plays your TTS string again instead of the music you expected.
The good thing: The SONOS componente has snapshot and restore functions for issues like this. The bad thing: I don’t want to blow up my automation actions with snapshots and restores, since its allways the same.
Therefore I created a small but flexible and helpful “sonos tts” script using template variables:
script:
# > SONOS TTS
say:
alias: SONOS TTS
sequence:
- service: media_player.sonos_snapshot
data_template:
entity_id: "{{ where }}"
- service: tts.google_say
data_template:
entity_id: "{{ where }}"
message: "{{ what }}"
# Add a delay in a length which you think fits best for all the things you want to say over TTS
- delay: '00:00:07'
- service: media_player.sonos_restore
data_template:
entity_id: "{{ where }}"
Automations using TTS over SONOS now only need to call the fowwling action:
I am not having any luck with the sonos_restore command. It plays the text on my Sonos speaker, but does not resume playing music. I’ve tried it with My Library music playing and with Pandora. Any ideas?
I found that the restore will run, even if the tts is still playing and the output will be cut off.
This is is how i solved it.
alias: SONOS TTS
sequence:
- service: media_player.sonos_snapshot
data_template:
entity_id: "media_player.{{ where }}"
- service: tts.google_say
data_template:
entity_id: "media_player.{{ where }}"
message: "{{ message }}"
- delay: '00:00:{{ states.media_player[where].attributes.media_duration | int }}'
- service: media_player.sonos_restore
data_template:
entity_id: "media_player.{{ where }}"
Unfortunately the delay only works for me when being specified within the 00:00:00 notation and not in seconds: {{ code }}. Because of that it will obviously only work for durations less than 60 seconds
@domlen After I investigated some more I found I was getting a lot of locked database lines in the log. I even found that when I stopped fiddling with the system after 4-5 minutes it did restore the music to the Sonos. I’m assuming this is related to the database??
Why doesn’t HA install with a better database or have information in the getting started to setup a better database? I’ve found a few links in the forum on setting up a MySQL DB for HA and I am going to go and try that. If that doesn’t work I’ll attempt cyberkov’s method that he posted below.
If i understand the code correctly the database is not involved in the snapshot process because it is just using a mechanism of the underlying SoCo library. It seems to be kept in memory. But unfortunately I do not speak python neither do I have enough knowledge about the hass internals to be 100% sure about it.
Thanks guys for amaizing job.
I am having a problem regarding this line: - delay: '00:00:{{ states.media_player[where].attributes.media_duration | int }}'
After the TTS message the Sonos stops and on pressing Play it replays TTS message.
My message is 2 seconds long. I am afraid that the delay line would be incorrect for
messages under 10 seconds, because it will be, in my case : delay: '00:00:2'
I tried a 0 (zero) in front of {{ states.media_player …, but it does not work.
But, if I am setting delay: '00:00:05' it works as intended.
Is anyone else having the same issue ?
17-01-16 10:23:34 ERROR (Thread-1) [homeassistant.util.yaml] while parsing a block mapping
in "/home/hass/.homeassistant/scripts/sonos_tts.yaml", line 26, column 9
expected <block end>, but found '<scalar>'
in "/home/hass/.homeassistant/scripts/sonos_tts.yaml", line 26, column 28
and then I tested (double quotes outside and singe quotes inside of the template)
17-01-16 10:24:41 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [script]: [delay] is an invalid option for [script]. Check: script->script->say->sequence->2->delay.
Thanks for the quick answer.
Indeed the first answer is incorrect due to single quotes.
So, it is working fine with double quotes: - delay: "00:00:{{ ('0' ~ states.media_player.living_room.attributes.media_duration) | reverse | truncate(2, True, '') | reverse }}"
First I got the same error (invalid config…) but I had a mistake in the script: -delay: - delay: {00:00:00}
So, recheck the file for typos or bad identation.