Templating whole actions

Hi,
I just tried the following and of course it doesn’t work as it is no where close to the templating feature.

- action:
  {% for i in range(21) %}
    - data:
        entity_id: light.wohnzimmer_decke
        brightness: {{ 200 - i * 9 }}
        color_temp: {{ 377 + i * 4 }}
      service: light.turn_on
    - delay: 00:01:00
  {% endfor %}
  alias: Sundown
  condition:
    condition: time
    weekday:
    - sun
    - mon
    - tue
    - wed
    - thu
  id: '1502645818972'
  trigger:
  - at: '22:00'
    platform: time

My goal is just to replace lots of repetetive lines with a loop. Is something like this possible with the templating of Home Assistant? If not, would it be possible in another way?
Thanks!

Is there any update to the above query, I am looking for the same. My use case is that when I receive a text from Telegram, I need to parse that message and call appropriate service and related actions. I can do that by repeating the if statements in service_template and the exactly the same statement in data_template. This is to ensure that for the called ‘service’ the exact corresponding ‘data’ is called. Now, this is just repeating the same conditions and I would like to see if there is something like ‘action_template’ where the service and data can be called in single if. My code below

The following code works fine

- id: '1234'
  alias: receive a text from Telegram and call appropriate service
  trigger:
    platform: event
    event_type: telegram_text
    action:
      - service_template: >
          {% if state in states.input_text.on_states.state %}
            switch.turn_on
          {% elif state in states.input_text.off_states.state %}
            media_player.turn_off
          {% endif %}           
        data_template:
          entity_id: > 
            {% if state in states.input_text.on_states.state %} # Repeated if statement from service_template
              switch.mediarelay
            {% elif state in states.input_text.off_states.state %}
              media_player.kodi
            {% endif %}

What I want to achieve is something like this

- id: '1234'
  alias: receive a text from Telegram and call appropriate service
  trigger:
    platform: event
    event_type: telegram_text
    action_template: >
      {% if state in states.input_text.on_states.state %}
        - service_template: switch.turn_on
           data_template:
             entity_id: switch.mediarelay
       {% elif state in states.input_text.off_states.state %}
         - service_template: media_player.turn_off
           data_template:
             entity_id: media_player.kodi
       {% endif %)

The scope of a template you create for a given option, like service_template, is limited to that option. It cannot be made into some kind of global macro and referenced from another option, like data_template.

Each option is its own little planet and there’s no interplanetary communication.

Noted on the above. However, is there any other way this could be achieved. The idea is not just to avoid the repeated if statement, but also to develop a generic template which could take the pair of service_template & data_template together. Like in my earlier example (shown below), the data_template for the service_template for “Switch” always expects a “entity_id”. However, if I want to use Service of ‘Notify.telegram’ , it would expect a data_template of ‘message’ and not ‘Entity_id’. Any other clever way of doing this?

    action:
      - service_template: >
          {% if state in states.input_text.on_states.state %}
            switch.turn_on
          {% elif state in states.input_text.off_states.state %}
            media_player.turn_off
          {% endif %}           
        data_template:
          entity_id: > 
            {% if state in states.input_text.on_states.state %} # Repeated if statement from service_template
              switch.mediarelay
            {% elif state in states.input_text.off_states.state %}
              media_player.kodi
            {% endif %}

no because you can’t programatically add or remove fields sent to data_templates.

I guess my original reply was too detailed so I’ll simplify it: No.