Hello,
I am having a difficult time getting the service_template part of a script to work. My goal is as follows:
- Create a reusable Google TTS for all of my audio notifications.
- The automation will pass to the script the following variables:
(a) the entity_id of the speaker to use: {{ where }}
(b) the volume level: {{ volume }}
© the message {{ what }}
The bottom half of the script works (i.e. setting the volume level and speaking the announcement). However, what I would like to do is detect the state of the media_player (off, idle or playing) and then do something before playing the announcement.
The first service_template is not working, what am I missing. Any help would be appreciated. Here is the Automation and the script.
AUTOMATION:
- alias: Garage State Notification
trigger:
platform: state
entity_id: sensor.garage_status_left, sensor.garage_status_right
action:
- service: script.turn_on
entity_id: script.say
data_template:
variables:
where: "kitchen_speaker"
what: "The {{ trigger.to_state.attributes.friendly_name }} state is: {{ trigger.to_state.state }}"
volume: '0.5'
SCRIPT:
say:
alias: Google TTS
sequence:
- service_template: >
{% if is_state ('media_player."{{ where }}"' ,'off') %}
script.do_something
{% elif is_state ('media_player."{{ where }}"' ,'idle') %}
script.do_something_else
{% elif is_state ('media_player."{{ where }}"' ,'playing') %}
script.do_another_thing
{% endif %}
- service: media_player.volume_set
data_template:
entity_id: 'media_player.{{ where }}'
volume_level: "{{ volume }}"
- service: tts.google_say
data_template:
entity_id: 'media_player.{{ where }}'
message: "{{ what }}"