TransPerth API Integration

Hi All,

I just came across an unofficial TransPerth API which gives access to Train, Bus and Ferry times as well as the ability to check your SmartRider balance.

I’m no developer but perhaps someone out there has the ability to integrate this to HA…

https://transperth.maxrumsey.xyz/

You should be able to use rest sensor for what you want.

Yeah, I found another site talking about REST sensors but the links it had to an official API didn’t work. I’ll have to investigate it further.

Hi Dave

Did you have any success with this?

No, unfortunately not.

I’ve been using the following rest command for a while to get train schedules for a given train line and station.

rest_command:
  get_next_train_time:
    url: "https://www.transperth.wa.gov.au/API/TrainLiveTimes/LiveStatus/{{ line_name }}/{{ station_name }}"
    method: GET
    headers:
      ModuleId: 5111
      TabId: 248

I then have a script with a couple fields with the expected station/line names which make it easier to use in automations.

get_next_train_times:
  alias: Get Next Train Times
  sequence:
  - service: rest_command.get_next_train_time
    metadata: {}
    data:
      line_name: '{{ line }}'
      station_name: '{{ station }}'
    response_variable: api_result
  - variables:
      trips: "{% set script_ns = namespace(to_perth=[], from_perth=[]) %}  \n{% if
        api_result.content.result == 'success' %}\n  {% for trip in api_result.content.data.StatusDetailList
        %}\n    {% set departure_hours, departure_mins = trip.Departure.split(':')
        %}\n    {% set departure_time = now().replace(hour=departure_hours|int, minute=departure_mins|int)
        %}\n    {% set diff_seconds = as_timestamp(departure_time) - as_timestamp(now())
        %}\n    {% set diff_minutes = max((diff_seconds / 60) | round(0, 'ceil'),
        0) %}\n    {% set trip = dict(trip, **{'mins_away': diff_minutes}) %}\n    {%
        if trip.Destination == 'Perth' %}\n      {% set script_ns.to_perth = script_ns.to_perth
        + [trip] %}\n    {% elif trip.Destination != 'Perth' %}\n      {% set script_ns.from_perth
        = script_ns.from_perth + [trip] %}\n    {% endif %}\n  {% endfor %}\n{% endif
        %}\n{ \"to_perth\": {{ script_ns.to_perth }}, \"from_perth\": {{ script_ns.from_perth
        }} }\n"
  - stop: ''
    response_variable: trips
  mode: single
  icon: mdi:train
  fields:
    line:
      selector:
        select:
          options:
          - All
          - Airport Line
          - Armadale Line
          - Fremantle Line
          - Yanchep Line
          - Mandurah Line
          - Midland Line
          - Thornlie Line
      name: Line
      description: Which train line?
      required: true
    station:
      selector:
        select:
          options:
          - Airport Central Stn
          - Armadale Stn
          - Ashfield Stn
          - Aubin Grove Stn
          - Bassendean Stn
          - Bayswater Stn
          - Beckenham Stn
          - Bull Creek Stn
          - Burswood Stn
          - Butler Stn
          - Canning Bridge Stn
          - Cannington Stn
          - Carlisle Stn
          - Challis Stn
          - City West Stn
          - Claisebrook Stn
          - Claremont Stn
          - Clarkson Stn
          - Cockburn Central Stn
          - Cottesloe Stn
          - Currambine Stn
          - Daglish Stn
          - East Guildford Stn
          - East Perth Stn
          - Edgewater Stn
          - Elizabeth Quay Stn
          - Fremantle Stn
          - Glendalough Stn
          - Gosnells Stn
          - Grant Street Stn
          - Greenwood Stn
          - Guildford Stn
          - High Wycombe Stn
          - Joondalup Stn
          - Karrakatta Stn
          - Kelmscott Stn
          - Kenwick Stn
          - Kwinana Stn
          - Lakelands Stn
          - Leederville Stn
          - Loch Street Stn
          - Maddington Stn
          - Mandurah Stn
          - Maylands Stn
          - McIver Stn
          - Meltham Stn
          - Midland Stn
          - Mosman Park Stn
          - Mt Lawley Stn
          - Murdoch Stn
          - North Fremantle Stn
          - Oats Street Stn
          - Perth Stadium Stn
          - Perth Stn
          - Perth Underground Stn
          - Queens Park Stn
          - Redcliffe Stn
          - Rockingham Stn
          - Seaforth Stn
          - Shenton Park Stn
          - Sherwood Stn
          - Showgrounds Stn
          - Stirling Stn
          - Subiaco Stn
          - Success Hill Stn
          - Swanbourne Stn
          - Thornlie Stn
          - Victoria Park Stn
          - Victoria Street Stn
          - Warnbro Stn
          - Warwick Stn
          - Wellard Stn
          - Welshpool Stn
          - West Leederville Stn
          - Whitfords Stn
          - Woodbridge Stn
      name: Station
      description: Which station?
      required: true

Hope that helps someone do something cool.