Data_template in condition of script

Trying to leverage this solution Call scripts with parameters? which allows passing a parameter to a script.

Script below is intended to turn on a Google Home so I can ‘play’ a notification. This would be triggered via an automation passing the specific {{device}} as a parameter. Anyhow, trying to use this code to check if the specific {{device}} is awake, and play an MP3 if not to wake it up.

google_home_turn_on:
  sequence:
    - condition: state 
      data_template:
        entity_id: "{{device}}"
      state: 'off'
    - service: media_player.play_media
      data_template:
        entity_id: "{{device}}"
        media_content_id: /local/1sec.mp3
        media_content_type: music

Since I have a bunch of Google Homes - figured it’d be easier to have a single script and allow a variable to be passed vs. having a bunch of duplicated scripts (one for each device). Just unsure how to leverage this passed parameter as part of the condition (or if this is even possible).

  sequence:
    - condition: template 
      value_template: "{{ is_state( device , 'off') }}"
    - service: media_player.play_media
      data_template:
        entity_id: "{{ device }}"
        media_content_id: /local/1sec.mp3
        media_content_type: music

Great stuff, thx for the quick response @anon43302295! Posted snips of my code below for future reference if it helps anyone else out.

automation.yaml:

action:
  - service: script.google_home_turn_on
    data:
      device: media_player.XXXX

scripts.yaml:

google_home_turn_on:
  sequence:
  - condition: or
    conditions:
    - condition: template 
      value_template: "{{ is_state( device , 'off') }}"
    - condition: template 
      value_template: "{{ is_state( device , 'idle') }}"
  - service: media_player.play_media
    data_template:
      entity_id: "{{ device }}"
      media_content_id: /local/1sec.mp3
      media_content_type: music
2 Likes

this sounds very interesting… But please forgive me asking, when does the device go offline? Mine are always on when powered?
secondly, do they simply ‘wake up’ when a play_media service is called?

If so, why bother waking them up in the first place? What i mean is, if one wants to play a message, and GH can be woken by simply playing some content, it doesn’t really matter if they are on or off does it?

Simply trying to understand the logic of this, thanks!

if its of any use for you, this is my generic script, taking its variables from some templates:

  play_radio:
    alias: Play radio
    sequence:
      - service: media_player.volume_set
        data_template:
          entity_id: >
            {{states('sensor.media_player')}}
          volume_level: >
            {{ states('input_number.radio_volume')|float }}
      - service: media_player.play_media
        data_template:
          entity_id: >
            {{states('sensor.media_player')}}
          media_content_id: >
            {{states('sensor.radio_station')}}
          media_content_type: 'music'

got a stop script too :wink: ( take it this is not the ‘off’ state you are referring to in the above post, I believed you to imply the GH is in sleep-mode, not just simply idle.

  stop_radio:
    alias: Stop radio
    sequence:
      service: media_player.turn_off
      data_template:
        entity_id:
          - >
            {{states('sensor.media_player')}}
          - >
            {{states('sensor.spotify_player')}}
1 Like