National Rail Integration

Thanks, this is really helpful, got it working nicely for me in my dashboard, as a relative newbie to HA!

Very nice. I will add this in the docs when I have a minute. If you want to send a PR this would be even better

Iā€™m new to HA, and finding this integration really useful. Iā€™ve got it setup sensors setup, and have both the HTML (with some edits) and markdown (stock at present) cards working. I am however, struggling to work out one thing, Iā€™m only getting 2 entries (occasionally 3, if a train departure time is imminent) in either card. Iā€™d like to get more entries, and ideally only that number (e.g. 5), how do I go about that? (Iā€™m sure this is a trivial question, so I do apologise, but Iā€™ve scoured the code without success)

Need to dive into this further but it looks like the API I used in the code only returns the trains departing in the next 2 hours or so

My usual trains departs every 30 minutes and I get 4 trains displayed. For your frequency, 2 responses seems OK

Ah, that explains it, thanks. In general 2 hours will be more than sufficient.

Hello, Iā€™m new to HA. Iā€™m having difficulty displaying the train times in the dashboard. Iā€™ve double-checked my token, and the station names (SWK & VIC) are correct. Iā€™ve restarted (and rebooted) the HA server a few times. Iā€™m pretty sure I followed the documentation to the letter.

Any suggestions of where to look to troubleshoot?

Hi @si1

Have you looked in Settings ā†’ System ā†’ Logs to see if there is anything related? When you set up the integrations, did you use the 3-letter abbreviations?

Hello, thank you for getting back to me. Yes, I used the three-letter station code and double-checked they were correct. SWK & VIC are correct.

The logs only say this:
2024-02-26 18:50:35.620 WARNING (SyncWorker_3) [homeassistant.loader] We found a custom integration nationalrailuk which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant

Hi

2 questions

  1. Do you know how to use the ā€œDeveloper Toolsā€ ? In the ā€œstatesā€ tab seach for ā€œtrain_scheduleā€. Can you see some entities with the train schedule? Send a copy-paste

  2. I cannot see a lot of trains between those 2 destinations on the national rail website either for this evening or for tomorrow. This might be explained because there is a change in Hove, isnā€™t it? I use the departure board API so it would only display SWK to Hove

Cool integration, thanks! Just added as a custom integration in HACS.

When setting up the integration, I initially didnā€™t enter a value for the destination station and got ā€˜An unknown error occurredā€™. Not sure if this could be made more descriptive i.e. ā€˜Please ensure you have entered both a departing and destination stationā€™.

Thank you for your quick responses!!!
So, there is usually a change at Brighton (BTN) or Hove (HOV) to get to Victoria (VIC). Are you saying the integration can only show direct trains?.. That would explain why Iā€™m not seeing trains in the schedule.

Is there a way to show trains even if thereā€™s a change (or two) to get to the destination?

The integration relies on the ā€œDeparture Boardā€ API (it shows the same info you get on the platform). It will only display direct trains.

Would have to change to the Online Journey Planner Data Feeds | National Rail API

There is another intergration on the forum that might be using the API required for your use case. It displays less trains though so I am not planning to change this

Hello jfparis,

Thank you so much for this integration, it works beautifully.

Is it possible to tweak the code to provide an Arrivals board? Iā€™ve taken a look at the GitHub but itā€™s well beyond my abilities (which are lacking!).

Iā€™m looking to keep abreast of the train times for the route my wife takes home from work?

Many thanks again!

There is an issue with token validation, commented out line 46 in config_flow.py and I was able to successfully add the integration and monitor the train proving that the token is valid.

Thank you, Iā€™ve also added this and it works great! I was wondering if it was possible to obtain the timetable for a custom time range? I normally plan my journeys the day before the actual trip. If not, is this something that could be added?

Is this still in HACS?

Hiya - this is exactly the card Iā€™m looking for! Really lovely design, would you mind posting the YAML please?

Iā€™m trying to make links to the national rail live train details page, which uses a url param ā€œsidā€. For example, here is a train from Gatwick to Brighton https://www.nationalrail.co.uk/live-trains/details/?sid=202410117150533&type=departures&targetCrs=BTN&filterCrs=GTW

Do you have any idea how to get that ā€œsidā€?

I was also curious about this, so Iā€™ve done some trial and error and think Iā€™ve worked it out.

The first part of the ā€˜sidā€™ is year month date (with no spaces), in your example ā€œ20241011ā€.

The next part of the ā€˜sidā€™ uses the service uid, but with a twist. This uid is in the form of a letter (A-Z), followed by 5 digits. Itā€™s a key, value pair for each train service within the next_trains attribute of each sensor created by the integration. So for the first service in the sensorā€™s list, you would access it in a template via:

state_attr('sensor.your_sensor_object_id', 'next_trains')[0]['service_uid']

The twist is that for the sid, the letter appears to be replaced by a 2 digit number, where A=65, through to Z=80. So for your example, Iā€™m deducing that the service uid was G50533.

By converting the letter to the two-digit number, you should be able to deduce the ā€˜sidā€™ and then build the entire url.

Iā€™ve written a custom macro that generates a list of urlā€™s for a given Real Time Trains sensor, and the results it produces do seem to work so far. I guess my next project is working out how to turn this into something that could be clicked through to from a dashboard!

Oh, and welcome to the community by the way.

Thanks for the tips. However, I couldnā€™t find next_trains and service_uid in my sensor state attributes. Are we talking about the same integration?