UK Transport component

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

Hi,

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.
Cheers!

@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':
            hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'green' })
        else:
            hass.services.call('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))
        else:
            logger.warning("Operator is {}, good luck ".format(operator_name))
2 Likes

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

After taking another look, it appears that the component doesn’t report the expected arrival time (although the transportapi does include it), therefore it is impossible to calculate the “next fastest train”, unless it is added in

Working on a table to show all train data, check it out

My latest project on Hackster using this component, getting notifications and changing the colour of a light depending on the status of my morning train:

Hey @robmarkcole. Thanks for your effort with this module, I’ve used it to replace the useless national rail alerting service, and I will likely get it to interact with my lamp too :slight_smile:

I’ve recently opened a feature request to add the ability to vary the refresh interval via automation. So, for example set it to every 10 minutes in the evenings and during the middle of the day when I don’t use the trains, and allow me to add a second sensor for my return journey without sacrificing the refresh interval during rush hours.

Is this possible or would it make things complicated?

Just need to add a service to update the scan interval, perhaps you could submit a PR?

Excellent job with this component. I have a few issues with it though which I’m unsure how to solve:

  1. I can get multiple buses to my specific destination but the final destination for each bus is different, how can I do that without defining two configs?

  2. On the contrary to 1 I can also get only one bus to my destination at another stop but there are multiple buses with the same final destination (which don’t get me to MY destination on the way).

Ultimately, I don’t want to specify a destination, rather I want to specify the bus number. This makes much more sense and I think it would suit way more peoples use cases if this were possible to do in the config.

Thanks!

HI @mderrick it appears from the API (https://developer.transportapi.com/docs?raml=https://transportapi.com/v3/raml/transportapi.raml) that queries are by bus/train stops only, and I don’t see any queries for specific bus/train id. There might be another service online that provide the info you need and I suggest a Google :slight_smile:
Cheers