UK Transport component

HI all, TFL have an API, so I wonder if anyone has thought of crating a HA component for it? Is there interest?

I have quite a bit of python experience (numpy, pandas mostly), but have not developed a HA component yet. Perhaps this would be a good one for me to start with, in which case can anyone offer advice on how to begin?

https://api.tfl.gov.uk/

Iā€™d be keen to see this. Donā€™t have a huge amount of python experience myself but happy to test!

1 Like

OK this is working!

1 Like

OK this is how to get any TFL data plus national rail for whole UK :smile:

http://www.transportapi.com/

https://developer.transportapi.com/docs?raml=https://transportapi.com/v3/raml/transportapi.raml##request_uk_train_station_station_code_live_json

The transportapi site looks interesting, shame they donā€™t seem to obviously support trams (joys of living south of the river - not much in the way of tubeā€¦). Iā€™d love to get live timing for trams into HA at some point. In the mean time, itā€™s easy to modify your rest sensor to give the tram status - thanks!

1 Like

A ā€˜uk transportā€™ component covering every mode of transport would be pretty awesome.! Is there an API covering tram? Those things arrive every 5 mins at least :grin:

The TFL API does cover trams, including next arrival ā€˜predictionsā€™ - https://api.tfl.gov.uk/swagger/ui/index.html?url=/swagger/docs/v1#!/Mode/Mode_Arrivals

Awesome! I think the TFL api actually uses transportapi, so perhaps integrating transportapi as a component is the way to go

Sadly it just looks like TfL used transport API for a London Overground project: http://transportapi.com/showcase/transport-for-london/

I may just be being silly, but I donā€™t even think thereā€™s a way of getting live tube data from transportapi let alone more obscure methods like trams.

But the TfL API now has decent tram support :slight_smile:

Anyway, enough of my love of trams for now - thanks for looking at this, always good to see other UK folks in the HA game!

transportAPI appears to support train (and tram via TFL?), bus, boat, tube, car and cycling directions.
From the docs:

ā€˜tflā€™ means to use Transport For Londonā€™s journey planner (Plan a journey - Transport for London) which will only work in and around London, and some interchange destinations
around the country.

ā€˜southeastā€™ means to use Traveline Southeastā€™s journey planner (http://www.travelinesoutheast.org.uk/) which covers the whole UK, but the queries run more slowly, and the name matching can be more ambiguous.

Additionally use data from the [National Rail Enquiries Darwin Data Feeds](
http://nrodwiki.rockshore.net/index.php/Category:National_Rail_Enquiries_Data_Feeds

When ā€˜serviceā€™ is not specified, we automatically make an appropriate
choice. We select ā€˜tflā€™ if both origin and destination are within
Greater London, or ā€˜southeastā€™ if one or the other lie outside London.

Live bus data is not available for all stops. This operation will make use
of different live data sources including NextBuses (http://nextbuses.mobi/) and TfL Countdown (Stations, stops & piers - Transport for London), to
bring you a consistently formatted response with the best live data
available, or fallback to timetable. The results can be ordered by time or
grouped by bus route

The NextBuses datasource will be used for bus stops outside of London, and
outside of a few other areas where we have access to more direct live bus
data. NextBuses is a more expensive source and the charge is adjusted to 10
hits per request to reflect this. If you wish to avoid this hit charge and
use only the other data sources, add a nextbuses=no parameter.

OK so my quick n dirty technique to add travel data as sensor is:

  1. Get the URL I want using the template maker at https://developer.transportapi.com/docs?raml=https://transportapi.com/v3/raml/transportapi.raml

  2. Determine the correct value_template

  3. Add the sensor using:

  1. Customise the sensor appearence.

I have set sensors up for my next train and its status.
Cheers

2 Likes

OK after a hiatus making some progress on the UK transport component - posting progress here to get feedback on what people want. The component will show the route/line status and where appropriate the next departure time on train/bus/tram routes. Component currently makes use of the TransportAPI API (http://www.transportapi.com/) and the TFL API (https://api.tfl.gov.uk/).

I have created tube status component using the TFL API: London tube status component

Tim has made a start on the TransportAPI with bus status: https://github.com/timcnicholls/home-assistant/blob/transport-api/homeassistant/components/sensor/uk_transport.py

Decision to make:
Have a single Unified transport component - would require TrasportAPI login credentials

OR

Separate TFL (tram, tube, no credentials required) and Bus/train (TrasportAPI login credentials required) components?

To do - in order of priority:

  1. Add to Tims work with train status. Python class https://github.com/robmarkcole/TrasportAPI-HASS
  2. Add tram status (TFL)
  3. Other requests? Car, bike

This is a great idea, Iā€™d definitely make use of a unified transport component. I also like your script to change the colour of your lights depending on the data you get back from these sensors.

Thank you for the effort so far.

Got the beta on https://github.com/robmarkcole/TrasportAPI-HASS
Need to fix the request rate, but itā€™s good for a single sensor at the moment, or you can manually set the SCAN_INTERVAL

Project complete

4 Likes

Struggling to get this to work for my local buses. The example works.
The difference appears to be setting the destination, I presume this is the ā€œdirectionā€ in the API.

The example shows setting this to Wantage, checking this in the API for the example stop it shows the direction as Market Place (Wantage). So from this should this be set to the bit in brackets. This worked for the example bus stop when I change it to Didcot, which in the API was: Parkway Station (Didcot).

Unfortunately for my local stop ā€œ13004328Bā€ the destination doesnā€™t show anything in brackets just ā€œStanley nr Crook Turning Circleā€ or something similar. Trying to enter this just breaks the component.

How should I select the bus stop destination in my case?

Hi Steve
Please post your config entry.
Cheers

Hereā€™s my config:

sensor:
  - platform: uk_transport
    app_id: <my app id>
    app_key: <my key>
    queries:
      - mode: bus
        origin: 13004328B
        destination: Watergate Estate Ullswater Crescent

The error I receive is:

2017-07-31 12:52:41 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved Traceback (most recent call last): File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step result = coro.throw(exc) File "/srv/hass/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py", line 381, in async_process_entity new_entity, self, update_before_add=update_before_add File "/srv/hass/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py", line 212, in async_add_entity yield from self.hass.async_add_job(entity.update) File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__ yield self # This tells Task to wait for completion. File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup future.result() File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result raise self._exception File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run result = self.fn(*self.args, **self.kwargs) File "/srv/hass/lib/python3.5/site-packages/homeassistant/util/__init__.py", line 303, in wrapper result = method(*args, **kwargs) File "/srv/hass/lib/python3.5/site-packages/homeassistant/components/sensor/uk_transport.py", line 184, in _update _delta_mins, [bus['scheduled'] for bus in self._next_buses] ValueError: min() arg is an empty sequence

If I change the origin and destination to your example it works. I have tried putting my destination in quotes in case the spaces cause problems but still the same.

Thanks. Did you perform the troubleshooting described in the docs of manually checking the get request?
The component is actually searching the ā€˜directionā€™ data, although this is labelled ā€˜destinationā€™ in the config

Thanks, yes I checked on their API reference page and that is where I got the direction value ā€œWatergate Estate Ullswater Crescentā€. As I mentioned this looks different to the values in the example. The API reference page GET request shows ā€œMarket Place (Wantage)ā€ and you say to put ā€œWantageā€ as the direction.

Hereā€™s the relevant part from the GET request on the API page:

{
ā€œmodeā€: ā€œbusā€,
ā€œlineā€: ā€œ84ā€,
ā€œline_nameā€: ā€œ84ā€,
ā€œdirectionā€: ā€œWatergate Estate Ullswater Crescentā€,
ā€œoperatorā€: ā€œPB2406ā€,
ā€œdateā€: ā€œ2017-07-31ā€,
ā€œexpected_departure_dateā€: null,
ā€œaimed_departure_timeā€: ā€œ14:01ā€,
ā€œexpected_departure_timeā€: null,
ā€œbest_departure_estimateā€: ā€œ14:01ā€,
ā€œsourceā€: ā€œNextBusesā€,
ā€œdirā€: ā€œoutboundā€,
ā€œidā€: ā€œhttps://transportapi.com/v3/uk/bus/route/PB2406/84/outbound/13004328B/2017-07-31/14:01/timetable.json?app_id=&app_key=ā€,
ā€œoperator_nameā€: null
}

Hope this makes sense?