Script with template that uses a entity as variable?

script:
  notify_train_status:
    sequence:
    - service: notify.notify
      data:
        title: "Tåg till jobb"
        message: >
          Tåg till jobb är {{ '' }}
          {%- if is_state('sensor.to_work', 'delayed') -%}
            försenat med
            {{ states.sensor.to_work.attributes.number_of_minutes_delayed }}
            minuter
          {%- elif is_state('sensor.to_work', 'canceled') -%}
            inställt
          {%- else -%}
            i tid
          {%- endif -%}
          {%- if states.sensor.to_work.attributes.other_information -%}
            , övrig information: {{ states.sensor.to_work.attributes.other_information }}
          {%- endif -%}
          {%- if states.sensor.to_work.attributes.deviations -%}
            , avvikelser: {{ states.sensor.to_work.attributes.deviations }}
          {%- endif -%}
          .
        data:
          icon: "https://materialdesignicons.com/api/download/B6E1232A-3A43-4200-BE34-1BC436B34BF1/FFFFFF/1/FFFFFF/0/128"

I need to replace all references of sensor.to_work with a variable how would the syntax for that look?

I managed to get it working ill leave it here in case someone else needs to do something similar.

notify_train_status:
  alias: Notify train status
  sequence:
    - service: notify.notify
      data_template:
        title: "{{ states.sensor[train_entity_id].attributes.friendly_name }}"
        message: >
          {{ states.sensor[train_entity_id].attributes.friendly_name }} är {{ '' }}
          {%- if is_state('sensor.' + train_entity_id, 'delayed') -%}
            försenat med
            {{ states.sensor[train_entity_id].attributes.number_of_minutes_delayed }}
            minuter
          {%- elif is_state('sensor.' + train_entity_id, 'canceled') -%}
            inställt
          {%- else -%}
            i tid
          {%- endif -%}
          {%- if states.sensor[train_entity_id].attributes.other_information -%}
            , övrig information: {{ states.sensor[train_entity_id].attributes.other_information }}
          {%- endif -%}
          {%- if states.sensor[train_entity_id].attributes.deviations -%}
            , avvikelser: {{ states.sensor[train_entity_id].attributes.deviations }}
          {%- endif -%}
          .
        data:
          icon: "https://materialdesignicons.com/api/download/B6E1232A-3A43-4200-BE34-1BC436B34BF1/FFFFFF/1/FFFFFF/0/128"