this post was submitted on 14 Jun 2023
2 points (100.0% liked)

homeassistant

11372 readers
23 users here now

Home Assistant is open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts. Perfect to run on a Raspberry Pi or a local server. Available for free at home-assistant.io

founded 1 year ago
MODERATORS
 

Hi, want to share my script how you make a good tts announcement on sonos.

How it works:

  • triggered by an automation like this
          - service: script.turn_on
            entity_id: script.jarvis_speak
            data:
              variables:
                mymessage: |
                  {{ state_attr('sensor.notification_message', 'msg') }}
                myplayer: media_player.sonos_bedroom

  • the script is using an input_boolean to flip so that one announcement is done at a time, that's why I have the "jarvis_announcement_wait" boolean
  • it uses "input_number.jarvis_<>_volume to be able to adjust the volume per room for the announcements
  • it uses a little "ding" sound prior to the announcement
  • it uses piper and with trial end error I figured out how many word per minute "ryan" is speaking so that the script ends exactly at the time when the tts announcement has finished in case there are multiple announcements to be made per room
alias: jarvis_speak
sequence:
	  - repeat:
		  while:
			- condition: state
			  entity_id: input_boolean.jarvis_announcement_wait
			  state: "on"
		  sequence:
			- delay:
				hours: 0
				minutes: 0
				seconds: 1
				milliseconds: 0
	  - service: input_boolean.turn_on
		target:
		  entity_id:
			- input_boolean.jarvis_announcement_wait
		data: {}
      - service: media_player.play_media
        data:
          media_content_id: /local/jarvis-chime.wav
          media_content_type: music
          announce: true
          extra:
            volume: >-
              {{ states('input_number.jarvis_' + myplayer |
              replace('media_player.','') + '_volume')  }}
        target:
          entity_id: "{{ myplayer }}"
      - delay:
          seconds: 1
      - service: media_player.play_media
        data:
          media_content_type: music
          announce: true
          media_content_id: >-
            media-source://tts/tts.piper?message={{ mymessage  | replace('&',
            'and') }}
          extra:
            volume: >-
              {{ states('input_number.jarvis_' + myplayer |
              replace('media_player.','') + '_volume')  }}
        target:
          entity_id: "{{ myplayer }}"
      - delay:
          seconds: >
            {% set text = mymessage | replace('&', 'and') %} {{ (text.split(' ')
            | length * 60 / 150) | round(0, 'ceil') }}
	  - service: input_boolean.turn_off
		target:
		  entity_id:
			- input_boolean.jarvis_announcement_wait
		data: {}
mode: queued
max: 10

top 3 comments
sorted by: hot top controversial new old
[–] camr_on@lemmy.world 1 points 1 year ago (1 children)

This is really cool, nice work. I'll have to take a look at using this method, right now I've got a hacky Sonos API with Google translate tts giving me announcements.. this looks way more straightforward and useable

[–] SugaryBurro@lemmy.world 0 points 1 year ago (1 children)

I have the same Google TTS solution, though I've noticed sometimes it has a multi second delay.

[–] bazingabot@lemmy.world 1 points 1 year ago

the proposed solution above has only a delay after the chime if the tts to generate is very long and hasn't been generated in the past. Then, you hear "ding" ... pause pause pause pause ... announcement. but this comes all down to the processing power where you host your piper docker/engine/addon