National Rail Integration

I pull arrival and departure times for each station as well as the current train location

1 Like

Cool, trying it out :+1:

Sometimes a journey sensor becomes unavailable. If I delete that sensor and try re-adding it I get “Unexpected error”, but I can still add journeys to/from those stations to other places. So it seems like a periodic problem on particular routes. Have you experienced this?

Hi Andrew

Which code base are you running? Whilst forking is totally welcome and encourage, perhaps we should fork this thread as well @plutomedia987

Agreed, because in this case I am referring Pluto’s fork. I’ve not encountered this in the original integration, although I have not run them simultaneously to be sure it would not occur

Agreed. Ill create a new thread for this. Ill have a look in to the unexpected and tag you in the new forum.

Anyone else having issues post updating to 24.12 or is it just me (or the update could be a coincidence)?

Opened an issue on Github but essentially get this error:

ModuleNotFoundError: No module named 'cgi'

Hi
Yes, one of the dependencies had an issue with the upgrade to python 3.13. I fixed it last night and pushed a new version. Did you try that before opening the issue?

1 Like

I should have checked that first shouldn’t I…

Fixed now, many thanks.

Probably. Although …
How do you install / update it? HACS (I don’t use that)?
Does it prompts you for updates?

I think I had it installed as custom repo from HACS, but I don’t believe I got an update prompt.

I just updated manually today, (but should have looked first, apologies again).

I’ve been using this successfully - thanks all for the work on it. One thing my fairly basic skills haven’t allowed me to do is to create an ‘arrivals board’ for a given station. I live near a fairly small station which doesn’t have a huge number of trains through it but they do come from a range of departure points. Has anyone been able to show planned arrivals to a given station rather than tracking them by individual departure points? If so I’d love to see some code. Thanks

Love this integration and many thanks for the markdown card CarlBird provided - helped me understand a lot about how to query the attributes.

I’m a bit of a noob and wonder if there’s an easy way to show the trains sorted not by departure time but arrival time - i.e. not show a slow train before a faster train?

Thanks again.

Hi,

I’ve started using the NationalRailUK integration.
It works for me, but I don’t know how to set up a nice departures board from my station.
Do you know where I can find some step-by-step instructions on how to do this?

Currently it looks like this:
image

TIA!

Here. Make sure you have the custom:html-template-card installed from HACS. Then add a card to a dashboard. select type manual and then paste my code into the window. Make sure to edit the title and journey entities for your journey.

type: custom:html-template-card
title: Horley - City Thameslink timetable
ignore_line_breaks: true
content: >
  <style> table {width: 100%;}  tr:nth-child(even) { background-color:
  #222222;}  td, th {text-align: left;} td.dest {padding-left: 1em;} </style> 
  <table>   <thead> <tr> <th>Destination</th> <th>Sch'd</th>         
  <th>Expt'd</th> <th>Platform</th> </tr></thead>  {% if
  states.sensor.train_schedule_hor_ctk and
  (states.sensor.train_schedule_hor_ctk.attributes.trains | length) > 0 -%}
  <tbody> {% for train in states.sensor.train_schedule_hor_ctk.attributes.trains
  %}<tr>
      <td>{{ train.terminus }}</td>
      <td>{{ as_timestamp(train.scheduled) | timestamp_custom('%I:%M %p') }}</td>
      <td>{% if train.expected is not string -%}
      {{ as_timestamp(train.expected) | timestamp_custom('%I:%M %p') }}
      {%- else -%}{{ train.expected -}}
      {% endif %}</td>
      <td>{{ train.platform }}</td>
  </tr> {% for dest in train.destinations -%}  <tr>
      <td class="dest">{{ dest.name }}</td>
      <td>&nbsp;</td>    
      <td>{% if dest.time_at_destination is not string -%}
      {{ as_timestamp(dest.time_at_destination) | timestamp_custom('%I:%M %p') }} 
      {%- else -%}{{ dest.time_at_destination -}}
      {% endif %}</td><td></td>
  </tr>  {% endfor -%} {%- endfor -%}    {%- else -%} <tr><td>No
  Trains</td></tr> {%- endif -%} </tbody> </table>

1 Like

Here’s another one. This is a markdown card. Again, make sure to edit the journey entity. BTW, all mine are taken for other people’s I have seen and edited so credit should go to them.

{% set numrows = 5 %}
{% set data = states.sensor.train_schedule_hor_ctk %}
{% if data and (data.attributes.trains|length) > 0 %}
<u><b>Next Trains - {{ data.attributes.name.split('_')[2] }} to {{ data.attributes.name.split('_')[3] }} {% if data.attributes.next_train_expected is not string %}{% set due = as_timestamp(data.attributes.next_train_expected) - as_timestamp(now()) %}{% endif %}{% if due is defined %}({% if due > 600 %}<font color="orange">{% else %}<font color="red">{% endif %}{% if due > 60 %}{{ due|timestamp_custom('%M')|int+1 }} mins{% else %}Due now!{% endif %}</font>){% endif %}</b></u>
{% set ns = namespace(count=1) %}
{% for trains in data.attributes.trains %}
{% if ns.count <= numrows %}
<table>
<tr><td><b>Destination: </b></td><td>{{ trains.terminus }}</td></tr>
<tr><td><b>Scheduled: </b></td><td>{% if trains.scheduled is not string %}{% if trains.scheduled != trains.expected %}<s>{{ as_timestamp(trains.scheduled)|timestamp_custom('%H:%M') }}</s> {% if trains.expected is not string %}<i><font color="aqua">Expected: {{ as_timestamp(trains.expected)|timestamp_custom('%H:%M') }}</font></i>{% else %}<b>{% if trains.expected == 'Cancelled' %}<font color="red">{% else %}<font color="orange">{% endif %}{{ trains.expected }}</font></b>{% endif %}{% else %}{{ as_timestamp(trains.scheduled)|timestamp_custom('%H:%M') }}{% endif %}{% else %}<b>{% if trains.scheduled == 'Cancelled' %}<font color="red">{% else %}<font color="orange">{% endif %}{{ trains.scheduled }}</font></b>{% endif %}</td></tr>
<tr><td><b>Platform: </b></td><td>{% if trains.platform != none %}{{ trains.platform }}{% elif trains.expected == 'Cancelled' %}<font color="gray">N/A</font>{% else %}<font color="gray">Unknown</font>{% endif %}</td></tr>
<tr><td><b>Arriving: </b></td><td>{% if trains.destinations[0].time_at_destination is not string %}{% if trains.scheduled != trains.expected %}<i><font color="aqua">{% endif %}{{ as_timestamp(trains.destinations[0].time_at_destination)|timestamp_custom('%H:%M') }}{% else %}<font color="gray">{% if trains.destinations[0].time_at_destination == 'Cancelled' %}N/A{% else %}Unknown{% endif %}</font>{% endif %}</td></tr>
</table>
{% if ns.count <= (numrows-1) and (ns.count-data.attributes.trains|length) != 0 %}<br>{% endif %}
{% set ns.count = ns.count + 1 %}
{% endif %}
{% endfor %}
{% else %}
<u><b>Next Trains - {{ data.attributes.name.split('_')[2] }} to {{ data.attributes.name.split('_')[3] }}</b></u>

<i>No more trains!</i>
{% endif %}
1 Like

The problem is that I don’t have it in HACS.

Only when I type html in the search bar I get the following:

In HACS I installed Lovelace HTML Jinja2 Template card and working now!

Thanks for your help!

Now I need to find an integration that will show me bus departures from stops near my house.

This is on my list to make. There is an goverment API for it, just havent gotten around to it. May have a look this weekend.

TFL also provide bus details 9n their API if youre in London.

1 Like