Script issue with TTS and Sonos mediaplayer - My house thank you :p

Dear all,

I’m trying to solve a script issue for weeks now… I don’t think i’m especially dump… But for sure, I miss something…

Here is my script;

say:
alias: Sonos TTS
sequence:
- service: media_player.sonos_snapshot
data_template:
entity_id: “{{ ‘media_player.’ ~ entity_id }}”
with_group: “yes”
- service: media_player.sonos_unjoin
data_template:
entity_id: “{{ ‘media_player.’ ~ entity_id }}”
- service: media_player.volume_set
data_template:
entity_id: “{{ ‘media_player.’ ~ entity_id }}”
volume_level: “0.4”
- service: tts.google_say
data_template:
entity_id: “{{ ‘media_player.’ ~ entity_id }}”
message: “{{ message }}”
language: “{{ language }}”
- delay: >-
{% set duration = states.media_player[entity_id].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.’ ~ entity_id }}”
with_group: “yes”

And my automation:

  • alias: Motion [Office]
    trigger:
    • platform: state
      entity_id:
      • binary_sensor.office_motion
      • binary_sensor.office_field_detection
      • binary_sensor.office_line_crossing
        from: ‘off’
        to: ‘on’
        condition:
        condition: and
        conditions:
      • condition: state
        entity_id: input_boolean.someone_home
        state: ‘on’
        action:
    • service: script.turn_on
      entity_id: script.say
      data:
      variables:
      entity_id: office
      language: ‘fr’
      message: “Je t’ai vu dans le bureau!”

Script is correctly called but nothing is played. It works well of if use this:

- service: tts.google_say
  data_template: 
    entity_id: media_player.office
    cache: true
    language: "fr"
    message: "Bureau"

Something probably wrong in my script. Any idea?

My house (and my girlfriend) thank you a lot :stuck_out_tongue:

Mat