Can't properly reuse template in multiply automations (Solved)

Hi! I’ve spent about 7 hours trying to make it works, but without any success. The main idea is making some automations for telegram bot that will update last bot’s message with actual statuses of lights, switches and so on.

For example trigger for user message /smallroom should return response with small room’s statuses and buttons. One of button is /smallroom_floorlight and bot’s response should be the same as for /small_room. It meance that it uses the same template. So if I want to make for example 10 buttons for one room I should copy and paste all the message template 10 times for each automation.

Example of automation for /smallroom:

alias: Telegram SmallRoom
description: ''
trigger:
  - platform: event
    event_type: telegram_callback
    event_data:
      data: /smallroom
condition: []
action:
  - service: telegram_bot.edit_message
    data_template:
      message_id: last
      chat_id: '{{ trigger.event.data.user_id }}'
      title: '{{"🛏"}} *Маленькая комната*'
      message: >
        🌡 Температура: {{states('sensor.small_room_temp_hum_temperature') }} °C

        💧 Влажность: {{ states('sensor.small_room_temp_hum_humidity')}} 

        🚶‍♂️ Движение: {{
        relative_time(states.binary_sensor.small_room_movement_occupancy.last_changed)
        }} назад 

        🔆 Освещённость: {{ states('sensor.small_room_lumi_illuminance') }} lx 

        🎶 Люмик:  {%- if is_state('media_player.lumi04','playing') %} ▶️ {%
        endif -%} {%- if is_state('media_player.lumi04','paused') %} ⏸ {% endif
        -%} {%- if is_state('media_player.lumi04','off') %} ⏹ {% endif -%} {{
        state_attr('media_player.lumi04', 'media_title') }}
      inline_keyboard:
        - >-
          {% if is_state('switch.small_room_ceiling_light','on') %}🟩 Верхний
          свет{%- else -%}🟥 Верхний свет{%- endif %}:/smallroom_ceilinglight
        - >-
          {% if is_state('light.small_room_floor_light','on') %}🟩 Торшер{%-
          else -%}🟥 Торшер {%- endif %}:/smallroom_floorlight, {% if
          is_state('light.small_room_lumi_light','on') %}🟩 Люмик{%- else -%}🟥
          Люмик {%- endif %}:/smallroom_lumilight
        - ↩️ Назад в меню:/tostart
mode: single
initial_state: 'on'

For the /smallroom_floorlight I use three actions: toggling floor lamp, delay 1 second to make template able to get new actual status in third action and updating last telegram bot’s message

....
  - service: telegram_bot.edit_message
    data_template:
      message_id: last
      chat_id: '{{ trigger.event.data.user_id }}'
.....

with the same template as for /smallroom.

I’ve tried few variants like

message: !include templates/small_room.yaml

and few other variants, but got null in message field after saving.

I’m thinking about something like custom sensor without type that will return long line with my template but I’m not sure that this template will works inside template for this sensor. It makes me crazy.

Please, propmt me how to reuse the same templates in multiply automations.

Thank’s for

I’ve just solve the problem by dividing telegram configuration to the separate package. So in this package I can reuse parts of yaml by special yaml syntax.

automation:

......

  - id: tg_smallroom                                                                                                                                                                           
    alias: TG SmallRoom                                                                                                                                                                        
    trigger:                                                                                                                                                                                   
    - platform: event                                                                                                                                                                          
      event_type: telegram_callback                                                                                                                                                            
      event_data:                                                                                                                                                                              
        data: /smallroom                                                                                                                                                                       
    action:                                                                                                                                                                                    
    - service: telegram_bot.edit_message                                                                                                                                                       
      data_template: &smallroom                                                                                                                                                                
        message_id: last                                                                                                                                                                       
        chat_id: '{{ trigger.event.data.user_id }}'                                                                                                                                            
        title: '{{"🛏 "}} *Маленькая комната*'
        message: "\n\U0001F321 Температура: {{ states('sensor.small_room_temp_hum_temperature') }} °C\n\U0001F4A7 Влажность: {{ states('sensor.small_room_temp_hum_humidity')}} %\n \
          \ \U0001F6B6 Движение: {{ relative_time(states.binary_sensor.small_room_movement_occupancy.last_changed) }} назад\n \
          \ \U0001F506 Освещённость: {{ states('sensor.small_room_lumi_illuminance') }} lx \n \ 
          \ \U0001F3B6 Люмик:  {%- if is_state('media_player.lumi04','playing') %} ▶️ {% endif -%} {%- if is_state('media_player.lumi04','paused') %} ⏸\ \
          \ {% endif -%} {%- if is_state('media_player.lumi04','off') %} ⏹ {% endif -%} {{ state_attr('media_player.lumi04', 'media_title') }}"
        inline_keyboard:           
        - '{% if is_state(''switch.small_room_ceiling_light'',''on'') %}🟩{%- else -%}🟥{%- endif %} Верхний свет:/smallroom_ceilinglight'
        - '{% if is_state(''light.small_room_floor_light'',''on'') %}🟩{%- else -%}🟥{%- endif %} Торшер:/smallroom_floorlight, {% if is_state(''light.small_room_lumi_light'',''on'')
          %}🟩{%- else -%}🟥{%- endif %} Люмик:/smallroom_lumilight'
        - ↩️  Назад в меню:/tostart
                                               
  - id: tg_smallroom_floor_light              
    alias: TG SmallRoom Floor Light
    trigger:      
    - platform: event                   
      event_type: telegram_callback
      event_data:     
        data: /smallroom_floorlight
    action:                                                                                    
    - service: light.toggle
      target:
        entity_id: light.small_room_floor_light 
    - delay:
        seconds: 1
    - service: telegram_bot.edit_message
      data_template:
        <<: *smallroom

  - id: tg_smallroom_ceiling_light
    alias: TG SmallRoom Ceiling Light
    trigger:
    - platform: event
      event_type: telegram_callback
      event_data:
        data: /smallroom_ceilinglight
    action:
    - service: switch.toggle
      target:
        entity_id: switch.small_room_ceiling_light
    - delay:
        seconds: 1
    - service: telegram_bot.edit_message
      data_template:
        <<: *smallroom

......

An alternative to using YAML anchors and aliases for this application is to create a script containing the service call to telegram_bot.edit_message. Each one of the three automations simply calls the script and passes trigger.event.data.user_id as a variable.