Need help with adjusting a script with template that reads state attributes

I have an automation that triggers a script. The script used to be based on sensor state but tthe output has changed and now i need it to enterpret attributes instead.

original code (about 5 years old) for Trafikverket Train

that it’s based on

My code:
I’ve made a sensor that works and gives this output.

sensor.train_from_a_to_b
state: 2022-05-02T09:05:00+00:00 ## used to be: on_time, delayed or canceled

These are the Attributes:

departure_state: delayed
canceled: false
number_of_minutes_delayed: 28
planned_time: '2022-05-02T07:07:00+00:00'
estimated_time: 'howh'
actual_time: null
other_information: Movingo gäller.
deviations: Prel. tid, Sen tågvänd.
device_class: timestamp
icon: mdi:train
friendly_name: Train from A to B

OR

departure_state: canceled
canceled: true
number_of_minutes_delayed: null
planned_time: '2022-05-04T14:59:00+00:00'
estimated_time: null
actual_time: null
other_information: Hänvisning till nästa Mälartåg.
deviations: Inställt
device_class: timestamp
icon: mdi:train
friendly_name: Train from A to B

I’ve made an automation

alias: 'Train status from A to B'
description: ''
trigger:
  - platform: state
    entity_id: sensor.train_from_a_to_b
condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: sensor.train_from_a_to_b
        attribute: departure_state
        state: delayed
      - condition: state
        entity_id: sensor.train_from_a_to_b
        state: canceled
        attribute: departure_state
action:
  - service: script.turn_on
    data:
      variables:
        train_entity_id: sensor.train_from_a_to_b
    target:
      entity_id:
        - script.notify_train_status
mode: single

and then the script that needs to be adjusted:

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 }} is {{ ''
        }} {%- if is_state('sensor.' + train_entity_id, 'delayed') -%}
          delayed by {{ '' }}
          {{- states.sensor[train_entity_id].attributes.number_of_minutes_delayed|int -}}
           {{ '' }} minutes.
        {%- elif is_state('sensor.' + train_entity_id, 'canceled') -%}
          canceled.
        {%- else -%}
          on time.
        {%- endif -%} {%- if
        states.sensor[train_entity_id].attributes.other_information -%}
          , Other information: {{ states.sensor[train_entity_id].attributes.other_information -}}
        {%- endif -%} {%- if
        states.sensor[train_entity_id].attributes.deviations -%}
          , Deviation: {{ states.sensor[train_entity_id].attributes.deviations -}}
        {%- endif -%} .
mode: single
icon: mdi:train

can anyone help me to get the right values that needs to be adjusted for the script to give the right output.

Thanks in advance.

Did you solve this? Id like to copy your automation

Hi, no. I haven’t found a way to change the scrips content to the newer code.