UK Transport component

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?

Appears there are no busses in that route…

That’s just a clipped part of the json returned. That’s showing that the next but from the origin 13004328B has an aimed departure time of 14:01. What these stops don’t have is “expected_departure_times”, I’m guessing because they don’t have live feeds of where the bus is like you often see in urban areas.

Just randomly typed the name of a town where a bus goes to and it works. I was trying to use the “direction” that the API returned from the GET request: https://transportapi.com/v3/uk/bus/stop/13004328B/live.json?

Am I missing something on the API page, should it return the list of destinations for each bus not just a “direction” which didn’t work, like I posted earlier.

I agree the docs are not clear, but the ‘destination’ in the HASS config must be in the ‘direction’ field returned by the GET request. You will see there is no ‘destination’ field returned by the request. Will submit a PR to clarify the docs. Cheers

Thanks @robmarkcole .

It did work fine last night once I figured out what it was expecting. Although today I have hit the API limit is there an easy way to change the refresh rate from the component YAML or do I need to find the python script and change it there as it mentions on your github page?

Sorry for all the questions.

The component is designed to use up your quota in normal use. You went over prob due to the extra requests during your debugging. Therefore leave another 24 hours and check that quota is not exceeded under normal use.
Cheers