I managed to specify a dynamic delay that should be about the duration of the spoken text by the following script. Apparently the initial 1s delay is necessary for the duration in the state to be filled.
alias: Sonos Text To Speech
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: "{{ what }}"
- delay:
seconds: 1
- delay: >-
{% set duration = states.media_player[where].attributes.media_duration %}
{% if duration > 0 %}
{% set duration = duration - 1 %}
{% endif %}
{% set seconds = duration % 60 %}
{% set minutes = (duration / 60)|int % 60 %}
{% set hours = (duration / 3600)|int %}
{{ [hours, minutes, seconds]|join(':') }}
- service: media_player.sonos_restore
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
{% set duration = states.media_player[where].attributes.media_duration %}
does this mean you are setting your where variable to “.living_room” with the leading period? How did you manage to pass the variable into the set statement?
Okay, I was able to figure out the nuances here. I think I’ve got it. I’m using a similar format to create a recursive script to transition lights that don’t support it natively.
Thanks for the great script! I added some features to really make sure that the message is played only in the specific media player (unjoin first) and not to the whole group that it is connected to. On top of that I changed the volume so it wouldnt use the same volume as the speaker was set to.
# > SONOS TTS
say:
alias: SONOS TTS
sequence:
- service: media_player.sonos_snapshot
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
with_group: yes
- service: media_player.sonos_unjoin
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
- service: media_player.volume_set
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
volume_level: 0.45
- service: tts.google_say
data_template:
entity_id: "{{ 'media_player.' ~ 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:
seconds: 1
- delay: >-
{% set duration = states.media_player[where].attributes.media_duration %}
{% if duration > 0 %}
{% set duration = duration - 1 %}
{% endif %}
{% set seconds = duration % 60 %}
{% set minutes = (duration / 60)|int % 60 %}
{% set hours = (duration / 3600)|int %}
{{ [hours, minutes, seconds]|join(':') }}
- service: media_player.sonos_restore
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
with_group: yes
Cant get this to work It complaining about invalid config. It dosnt like the automation part i think?
17-02-19 23:49:24 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [automation]: [service] is an invalid option for [automation]. Check: automation->service. (See /home/hass/.homeassistant/configuration.yaml, line 163). Please check the docs at https://home-assistant.io/components/automation/
Failed config
Heres my config
coniguration.yaml
# Google Text to speech
tts:
- platform: google
cache: true
cache_dir: /tmp/tts
time_memory: 300
language: 'sv'
# Include
group: !include groups.yaml
sensor: !include sensors.yaml
switch: !include switches.yaml
script: !include_dir_merge_named script
automation: !include_dir_merge_list automation
script/sonos_tts.yaml
# > SONOS TTS
say:
alias: SONOS TTS
sequence:
- service: media_player.sonos_snapshot
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
with_group: yes
- service: media_player.sonos_unjoin
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
- service: media_player.volume_set
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
volume_level: 0.45
- service: tts.google_say
data_template:
entity_id: "{{ 'media_player.' ~ 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:
seconds: 1
- delay: >-
{% set duration = states.media_player[where].attributes.media_duration %}
{% if duration > 0 %}
{% set duration = duration - 1 %}
{% endif %}
{% set seconds = duration % 60 %}
{% set minutes = (duration / 60)|int % 60 %}
{% set hours = (duration / 3600)|int %}
{{ [hours, minutes, seconds]|join(':') }}
- service: media_player.sonos_restore
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
with_group: yes
automation/sonos_test.yaml
- service: script.turn_on
entity_id: script.say
data:
variables:
where: 'kontor'
what: 'Hej där!'
I couldn’t get it to work with the {{ where }} and {{ what }} either.
Now that I have placed the “where” and the “what” within the actual scripts, it works fine again…
@Toni: This is not even not a half automation ;). If something should be automated there are two things essential: what shall be automated an when - you are totaly missing the when part. I would recommend walking through the hole automation tutorial setp by step, since this is the most essential thing within HA and home automation: Automating Home Assistant - Home Assistant
As you can see, I have not quite got the hang of this yet I will have second look at the getting started It was not mention to be a full automation just something simple i could manually trigger from the frontend.
What about the following automation? This automation works fine without the last sonos part. Here im trying to send a tts message to one sonos speaker “media_media_player.kontor” after 2 email has been sent.
- alias: "mail water leak"
initial_state: True
hide_entity: False
trigger:
- platform: state
entity_id: switch.fuktdiskbank
to: 'on'
action:
- service: notify.gmailtoni
data_template:
title: "Water leaks under kitchen sink"
message: "Possible water leak under kitchen sink."
- service: notify.gmailmalin
data_template:
title: "Water leaks under kitchen sink"
message: "Possible water leak under kitchen sink."
- service: script.turn_on
entity_id: script.say
data:
variables:
where: 'kontor'
what: 'Hello!'
# SONOS TTS
say:
alias: SONOS TTS
sequence:
- service: media_player.sonos_snapshot
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
with_group: yes
- service: media_player.sonos_unjoin
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
- service: media_player.volume_set
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
volume_level: 0.45
- service: tts.google_say
data_template:
entity_id: "{{ 'media_player.' ~ 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:
seconds: 1
- delay: >-
{% set duration = states.media_player[where].attributes.media_duration %}
{% if duration > 0 %}
{% set duration = duration - 1 %}
{% endif %}
{% set seconds = duration % 60 %}
{% set minutes = (duration / 60)|int % 60 %}
{% set hours = (duration / 3600)|int %}
{{ [hours, minutes, seconds]|join(':') }}
- service: media_player.sonos_restore
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
with_group: yes
The error I get:
17-02-19 23:49:24 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [automation]: [service] is an invalid option for [automation]. Check: automation->service. (See /home/hass/.homeassistant/configuration.yaml, line 163). Please check the docs at https://home-assistant.io/components/automation/
Failed config