How can i use other files in my script?

I have a script for making announcements that is basically the base for many other scripts.
the script does the following:

  1. take variables from the automation ie target speaker and service (either alexa or googletts)
  2. contains the announcement message for that particular automation
  3. contains the template for the speakers and the room it is in
  4. check if there is anyone in the target speaker’s room or wait until someone is there
  5. makes the announcement if the condition for the announcement is still true
  6. makes the announcement using which sequence depending on whether to use alexa or googletts.

part 3 and 6 are the same for all the different announcement scripts.
i would like to break up the script like this:

  1. take variables from the automation ie target speaker and service (either alexa or googletts)
  2. contains the announcement message for that particular automation
  3. get a file which contains the template for the speakers and the room it is in
  4. check if there is anyone in the target speaker’s room or wait until someone is there
  5. makes the announcement if the condition for the announcement is still true
  6. get a file that makes the announcement using which sequence depending on whether to use alexa or googletts.

i looked at the docs https://www.home-assistant.io/docs/configuration/splitting_configuration/ but I couldn’t get make it work. where do i save the yaml files, and how should i define it inside the script?
are all the data variables from the automation that called the first script passed to another script that is called from inside the script? are the variables from the first script passed to the second script?

here is my script.

alias: Announce Gate Left Open
sequence:
  - variables:
      announcement_msg: >-
        '{{ ( "Did you forget to close the gate?","Someone left the gate
        open","Did someone forget to close the gate?","I think someone forgot to
        close the gate","The gate has been open for a while... did someone
        forget to close it?","Somebody forgot to close the gate...","The gate is
        still open" ) | random }}'
      old_volume: '{{ state_attr(speaker_device,''volume_level'') | float }}'
      mediaplayer_State: '{{ states(speaker_device) }}'
  - wait_template: |-
      {{ (speaker_device == 'media_player.echo_show' and
        is_state('input_boolean.room_occupancy_study', 'on')) or
        (speaker_device == 'media_player.sonos_beam' and
        is_state('input_boolean.room_occupancy_living_dining', 'on')) or
        (speaker_device == 'media_player.kitchen_2' and
        is_state('input_boolean.room_occupancy_kitchen', 'on')) or
        (speaker_device == 'media_player.landing_2' and
        is_state('input_boolean.room_occupancy_1st_floor', 'on')) }}
    timeout: '00:30:00'
    continue_on_timeout: false
  - condition: state
    entity_id: binary_sensor.zone_gate_open
    state: 'on'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ speaker_service == ''notify.alexa_media'' }}'
        sequence:
          - service: media_player.media_pause
            target:
              entity_id: '{{ speaker_device }}'
          - service: media_player.volume_set
            data:
              volume_level: '{{ announce_volume }}'
            target:
              entity_id: '{{ speaker_device }}'
          - service: '{{ speaker_service }}'
            data:
              data:
                type: tts
              message: '{{ announcement_msg }}'
              target: '{{ speaker_device }}'
          - service: media_player.volume_set
            data:
              volume_level: '{{ old_volume }}'
            target:
              entity_id: '{{ speaker_device }}'
          - condition: template
            value_template: '{{ mediaplayer_State == ''playing'' }}'
          - delay:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
          - service: media_player.media_play
            target:
              entity_id: '{{ speaker_device }}'
      - conditions:
          - condition: template
            value_template: '{{ speaker_service == ''tts.google_translate_say'' }}'
        sequence:
          - service: media_player.media_pause
            target:
              entity_id: '{{ speaker_device }}'
          - service: media_player.volume_set
            data:
              volume_level: '{{ announce_volume }}'
            target:
              entity_id: '{{ speaker_device }}'
          - service: '{{ speaker_service }}'
            data:
              entity_id: '{{ speaker_device }}'
              message: '{{ announcement_msg }}'
          - delay:
              hours: 0
              minutes: 0
              seconds: 1
              milliseconds: 0
          - delay:
              hours: 0
              minutes: 0
              seconds: '{{ state_attr(speaker_device,''media_duration'') | float }}'
              milliseconds: 0
          - service: media_player.volume_set
            data:
              volume_level: '{{ old_volume }}'
            target:
              entity_id: '{{ speaker_device }}'
          - condition: template
            value_template: '{{ mediaplayer_State == ''playing'' }}'
          - delay:
              hours: 0
              minutes: 0
              seconds: 3
              milliseconds: 0
          - service: media_player.media_play
            target:
              entity_id: '{{ speaker_device }}'
    default:
      - stop: ''
mode: parallel
max: 5

i will appreciate any advice!
p/s i’m just starting out, so I may be doing this the wrong way, but please teach me patiently

That only works with splitting the configuration.yaml file. It was very useful when the configuration.yaml file was over a hundred lines. For example, my configuration.yaml had a component named sensor: and a dozen or so were defined in the sensor: section. It just got in the way if I was looking to edit or add a switch:. So, I copied everything in the sensor: section to a file named sensors.yaml, and in configuration.yaml, I added sensor: !include sensors.yaml.

I don’t think that any kind of include, substitution, package or macro exists in Home Assistant. (Yet).

there’s a blog post by thejeffreystone https://slacker-labs.com/2022/01/26/building-a-daily-briefing/ where he talks about macros and including other yaml files into automations and scripts but when i tried to replace my wait_template data with an include i got an error and wasn’t even able to save the script.

so guys, is it true that include does not work in scripts and automations?

That is my understanding. I read something on the forum a few months ago that said something like include doesn’t work on runtime objects, like scripts and automations. I am sure someone will correct me.