Template Target?

Can you template a target? I have 3 automation’s that i think i should be able to make into one with a dynamic target. The only difference is the condition of the time and the target. Thanks for your help.

 ######################################################################################################################################
 #description: Announce that the upstairs windows are open and it is about to rain
 #trigger(s):  window group open, going to rain
 #condition(s): windows open, going to rain, after 6am and before 10am
 #action: announcement everywhere but the bedroom
 ######################################################################################################################################
  - alias: "Window - Announce upstairs window open when about to rain without bedroom"
    trigger:
    - platform: state
      entity_id:  group.upstairs_windows
      to: 'on'
    - platform: template
      value_template: "{{ 'rain' in states.sensor.dark_sky_summary.state }}"
    condition:
    - condition: and
      conditions:
        - condition: state
          entity_id: group.upstairs_windows
          state: 'on'
        - condition: template
          value_template: "{{ 'rain' in states.sensor.dark_sky_summary.state }}"
        - condition: time
          after: '6:00:00'
          before: '10:00:00'
    action:
      - service: notify.alexa_media
        data:
          target: 
            - group.all_echos_but_the_bedroom
          data:
            type: announce
          message: "An Upstairs Window is currently open and it is about to rain."

 ######################################################################################################################################
 #description: Announce that the windows are open and it is about to rain
 #trigger(s):  window group open, going to rain
 #condition(s): windows open, going to rain, after 6am and before 10am
 #action: announcement everywhere
 ######################################################################################################################################
  - alias: "Window - Announce upstairs window open when about to rain"
    trigger:
    - platform: state
      entity_id:  group.upstairs_windows
      to: 'on'
    - platform: template
      value_template: "{{ 'rain' in states.sensor.dark_sky_summary.state }}"
    condition:
    - condition: and
      conditions:
        - condition: state
          entity_id: group.upstairs_windows
          state: 'on'
        - condition: template
          value_template: "{{ 'rain' in states.sensor.dark_sky_summary.state }}"
        - condition: time
          after: '10:00:00'
          before: '21:00:00'
    action:
      - service: notify.alexa_media
        data:
          target: 
            - group.all_echos
          data:
            type: announce
          message: "An Upstairs Window is currently open and it is about to rain."
          
 ######################################################################################################################################
 #description: Announce that the windows are open and it is about to rain
 #trigger(s):  window group open, going to rain
 #condition(s): windows open, going to rain, after 9pm and before 11pm
 #action: announcement everywhere
 ######################################################################################################################################
  - alias: "Window - Announce upstairs window open when about to rain downstairs only"
    trigger:
    - platform: state
      entity_id:  group.upstairs_windows
      to: 'on'
    - platform: template
      value_template: "{{ 'rain' in states.sensor.dark_sky_summary.state }}"
    condition:
    - condition: and
      conditions:
        - condition: state
          entity_id: group.upstairs_windows
          state: 'on'
        - condition: template
          value_template: "{{ 'rain' in states.sensor.dark_sky_summary.state }}"
        - condition: time
          after: '21:00:00'
          before: '23:00:00'
    action:
      - service: notify.alexa_media
        data:
          target: 
            - group.downstairs_echos
          data:
            type: announce
          message: "An Upstairs Window is currently open and it is about to rain."

Try:

  - alias: "Window - Announce upstairs window open when about to rain"
    trigger:
    - platform: state
      entity_id:  group.upstairs_windows
      to: 'on'
    - platform: template
      value_template: "{{ 'rain' in states.sensor.dark_sky_summary.state }}"
    condition:
    - condition: and
      conditions:
        - condition: state
          entity_id: group.upstairs_windows
          state: 'on'
        - condition: template
          value_template: "{{ 'rain' in states.sensor.dark_sky_summary.state }}"
        - condition: time
          after: '6:00:00'
          before: '23:00:00'
    action:
      - service: notify.alexa_media
        data_template:
          target: >
            {% set hr = now().hour %}
            {% if hr < 10 %} group.all_echos_but_the_bedroom
            {% elif hr < 21 %} group.all_echos
            {% else } group.downstairs_echos
            {% endif %}
          data:
            type: announce
          message: "An Upstairs Window is currently open and it is about to rain."

Thank you. I will give that a shot

You can template anything in a service call by using either service_template (instead of service) to template the service name, or data_template (instead of data) to template the service data.

This is well documented on the service calls documentation page.

Since this comes up in search, I’ll mention that services have been renamed to actions, but there’s no action_template because you can use templates wherever you want in action:Performing actions - Home Assistant