Retrieve bus arrival timings in Singapore with LTA Datamall

Hello! I would like to share my first python project :smile:

One of the most common ways to travel around in Singapore is by public transport, of which buses being the most common. The double decker buses in Singapore are similar to those in London. The Land Transport Authority has a public bus API that can be used to track and determine the bus timings for specified bus stops.

I made a custom component to retrieve specified bus timings for specified bus stops. It can also be installed via HACS:

The API however requires an API key, which can be requested on Datamall.

There may be a few bugs here and there, but generally the component should work fine. Any feedback is appreciated.

5 Likes

thanks. trying it out.

How is the api key entered in HA?
Any format if it is in configuration.yaml?

sensor:

  • platform: lta
    bus_stop_code: ‘XXXXX’
    api_key: $API_KEY

Will add it in the README.md

To real-time, minute-by-minute JSON about bus arrival times from every LTA Data Mall API is to register and account and get an API key.

It works!!! Thanks for your taking your time to write this out, hormat!!

1 Like

Works really well! Looking forward to seeing the multi-stop! Thanks again

1 Like

The ha-lta custom component has been updated to allow configuration of multiple bus stops :grinning_face_with_smiling_eyes:

Updated. Working well for me.
Thank you Codestian.

1 Like

Thanks for coming up with this Integration, will be useful and will integrate this in my Dashboard.

Works well! Thank you :slightly_smiling_face:

It works well!! thank you so much

I have managed to set in up in the dashboard.

I also would like to know on how to do this part from the github guide.
" Tell the bus timing before leaving the house through a smart speaker"

I also have an apple watch, which would be awesome if I can integrate this onto apple homekit.

You can create an automation, with the following devices as a simple example:

  • Door sensor
  • Google/Alexa speaker
  • ha-lta component

So when the door sensor is activated, Home Assistant will retrieve the selected bus service timing and output it onto the speaker via TTS.

hi, is this still maintained? couldnt find the ha-lta repository on HACS

I will take a look at it. You can also add custom repositories if it does not show up.

managed to get it to work! thanks! :slight_smile:

How does everyone’s cards look? care to do a show and tell?

Hiya. Would love to include this in my dashboard. Do you have any examples on how to use this in a dashboard please? Thanks!

Hi, I’m new to HA and I’m looking to build from scratch with HA green since that’s what everyone is suggesting for beginner. Am I able to use this code with HA green? I read a few posts and looks like it will be possible as an integration, correct me if I’m wrong. Thanks in advanced!

Hello. I tried to search for ha-lta in HACS in my home assistant 2024.8 but there isn’t such item. Can you help if this is no longer there? Or its some where else?

Actually, you can use Home Assistant’s native RESTful sensor integration to retrieve the data. Here is an example from my setup:

- platform: rest
  resource: http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2?BusStopCode=80091&ServiceNo=67
  name: Before Lorong 23 Geylang Bus 67
  unique_id: sensor.before_lorong_23_geylang_bus_67
  headers:
    accountKey: !secret lta_datamall_apikey
  scan_interval: 172800
  value_template: "{{ (as_datetime(value_json['Services'][0]['NextBus']['EstimatedArrival']) - now()) // timedelta(minutes = 1) }}"
  unit_of_measurement: min
  json_attributes_path: $.Services.[0]
  json_attributes:
    - 'ServiceNo'
    - 'NextBus'
    - 'NextBus2'
    - 'NextBus3'

This retrieves the arrival information for one specific bus service ServiceNo at one bus stop BusStopCode. You also need to sign up on LTA Datamall to obtain your API key that you can use in accountKey.

I set the scan_interval very high and use a separate automation to home_assistant.update_entity the sensor only when the bus arrival time data is actually available, otherwise when the bus is not running or the API is down for maintenance, the sensor will be unavailable and your logs will be spammed with errors. If this doesn’t bother you, you can set scan_interval to something usable like 60.