UK Transport component

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:

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.

Addressing your request to vary the request rate - this could be done by creating a ‘service’ to do that. If you feel up to the challenge fork the code and have a go :smiley:

Thanks for the help, I was guessing it was probably all my testing. No point in really adding the rate service unless you were using the API for more than just Home Assistant which I am not.

If I continue to hit limits then will look at forking but shouldn’t be necessary.

Thanks again :smiley:

1 Like


This is a great component! I love being able to ask Alexa “how long until the next bus!”. I may have found a bug though. Over night every night my logs get filled with the error message below. I have not had the time to debug this but I assume that as it only happens between 11 pm and 5am it is being caused as there are no buses during this period.

Aug 8 03:02:12 pi2 hass[16500]: ERROR:homeassistant.helpers.entity:Update for sensor.next_bus_to_redacted fails
Aug 8 03:02:12 pi2 hass[16500]: yield from self.hass.async_add_job(self.update)
Aug 8 03:02:12 pi2 hass[16500]: yield self # This tells Task to wait for completion.
Aug 8 03:02:13 pi2 hass[16500]: future.result()
Aug 8 03:02:13 pi2 hass[16500]: raise self._exception
Aug 8 03:02:13 pi2 hass[16500]: result = self.fn(*self.args, **self.kwargs)
Aug 8 03:02:13 pi2 hass[16500]: result = method(*args, **kwargs)
Aug 8 03:02:13 pi2 hass[16500]: _delta_mins, [bus[‘scheduled’] for bus in self._next_buses]
Aug 8 03:02:13 pi2 hass[16500]: ValueError: min() arg is an empty sequence

HI Tim, glad the component is useful :slight_smile:
You are correct about the cause. I have created an issue here
Please let me know of any further issues.

@bachoo786 I am moving our discussion from London tube status component to this thread. Your query was how to query an individual departure, in particular the 8:04 HRW to EUS.

My advice was to create a template sensor based on the scheduled departure time as described in the docs. One issue you have with that approach is that the base sensor returns data for more than 10 upcoming trains (in attributes.next_trains), so you will need to use a value template (or python script) to loop over each train to check against the scheduled departure of 8:04 OR have individual template sensors for each of the upcoming trains. I suggest the first approach, in particular using a python script which is triggered each time the base sensor is updated within a time window and fires a notify service when the departure of 8:04 is found. TBC

Seems difficult but I will have a go at it.

How can I add “operator” which is an attribute as a filter?

I posted the python script on Set lamp colour depending on train status

Since you also want the operator:

entity_id = 'sensor.next_train_to_eus'
attributes = hass.states.get(entity_id).attributes # attributes is a dict
next_trains = attributes['next_trains'] # next_trains is a list of upcoming trains, must access dict in this way
logger.warning("Number of upcoming trains is {}".format(len(next_trains)))

for train in next_trains:
    estimated = train['estimated']
    status = train['status']
    operator_name = train['operator_name']
    if estimated == '07:56':
        logger.warning("Estimated departure at {} identified".format(estimated))
        logger.warning("Status is {}".format(status))
        if status == 'ON TIME':
  'light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'green' })
  'light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'red' })
        if operator_name == 'SOUTHERN RAIL':
            logger.warning("Operator is {}, your'e buggered ".format(operator_name))
            logger.warning("Operator is {}, good luck ".format(operator_name))

Thanks buddy

Is this part of the original code or a separate code?

This is a python script to be used with the python script component

1 Like
1 Like

Can anyone help with templates for retrieving the “next fastest train” instead of the “next train” given in examples?

sorry, I don’t know much about repositories, I don’t understand what to do that!

Sorry that message was meant for another thread.!
Next_trains is an ordered list, so adapt the example in the docs for whichever train you want