Automation target templating ... how to

Hello
I am trying to make a simple automation which spreads the binary output of a thermostat to the switch(s) of the room.
with 2 problems
1 some switchs are “direct” (hermostat output=on => switch must be on) some are “pilot” (thermostat output=on => switch must be off)
2 I search to have a dynamic list action
for testing i use input_boolean instead of switchs
[‘input_boolean.rad_002_pil0_2023_ex’]
and [‘input_boolean.rad_001_pil1_2023_ex’]

what i achieve, in templating : getting list of pilote or direct is working:

{% set list=
states.input_boolean 
|selectattr('entity_id', 'search', 'rad') 
|selectattr('entity_id', 'search', '2023_ex') 
|selectattr('entity_id', 'search', 'pil0')
|rejectattr('entity_id', 'search', 'masque_d_exclusion')
|map(attribute='entity_id')
|list
-%}
{{list}}    

gives the good result:

['input_boolean.rad_002_pil0_2023_ex']   

the automation with each name hardcoded is working well:

  - id: spread_bool_out_2023_ex  # distribue la sortie du thermostat vers les switch avec ou sans pilote
  # declanché par un changement d etat de out_2023_ex === sortie systematique du thermostat
  # distribue l ordre direct si switch direct
  # et inverse si pilote
  # !!!! ici en call service input_boolean.turn_on | _off
  # a remplacer par switch.turn_on |off
    alias: spread_bool_out_2023_ex
    description: ""
    mode: single
    trigger:
      - platform: state
        entity_id:
          - input_boolean.out_2023_ex
        from: "off"
        to: "on"
        id: out_on
      - platform: state
        entity_id:
          - input_boolean.out_2023_ex
        from: "on"
        to: "off"
        id: out_off
    condition: []
    action:
      - if:
          - condition: trigger
            id:
              - out_on
        then:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.rad_001_pil1_2023_ex
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.rad_002_pil0_2023_ex
      - if:
          - condition: trigger
            id:
              - out_off
        then:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.rad_001_pil1_2023_ex
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.rad_002_pil0_2023_ex

but when I try to change fixed name with template, it does not work:

...
    action:
      - if:
          - condition: trigger
            id:
              - out_on
        then:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.rad_001_pil1_2023_ex
          - service: input_boolean.turn_on
            data: {}
            data_template:
              target: >
                {% set list=
                  states.input_boolean 
                  |selectattr('entity_id', 'search', 'rad') 
                  |selectattr('entity_id', 'search', '2023_ex') 
                  |selectattr('entity_id', 'search', 'pil0')
                  |map(attribute='entity_id')
                  |list
                  -%}
                  {{list}}
...

is there a way to write this kind of automation ?
thank for your help

At least for core services, data_template was deprecated more than 2 years ago… and you still need to provide the entity_id key

- service: input_boolean.turn_on
  data:  {}
  target:
    entity_id: |
      {{ states.input_boolean 
      | selectattr('entity_id', 'search', 'rad') 
      | selectattr('entity_id', 'search', '2023_ex') 
      | selectattr('entity_id', 'search', 'pil0')
      | map(attribute='entity_id')
      | list }}

thank you for reply
you are right, my reference was old (2019):

now it works
thank you again