Home Assistant Community

UK Transport component

#1

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/

#2

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

1 Like
#7

OK this is working!

#8

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

#9

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
#10

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:

#11

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

#12

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

#13

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!

#14

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 (https://tfl.gov.uk/plan-a-journey/) 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 (https://tfl.gov.uk/travel-information/stations-stops-and-piers/), 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.

#15

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

#16

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
#17

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.

#18

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

#19

Project complete

2 Likes
#20

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?

#21

Hi Steve
Please post your config entry.
Cheers

#22

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.

#23

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

#24

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?