UK Transport component

Thought I’d post an update to my lovelace table, which uses some formatting and also some minor error detection.

The lovelace code:

  - type: conditional
    conditions: 
      - entity: sensor.next_train_to_wat
        state: "No departures"
    card:
      type: entities
      title: Trains to Waterloo
      show_header_toggle: false
      entities:
        - sensor.next_train_to_wat

  - type: conditional
    conditions: 
      - entity: sensor.next_train_to_wat
        state_not: "No departures"
    card:
      type: custom:flex-table-card
      title: Trains to Waterloo
      icon: mdi:train 
      entities:
        include: sensor.next_train_to_wat
      columns:
        - name: Type
          attr_as_list: next_trains
          modify: "x.origin_name == 'Weymouth' ? 'Fast' : ''"
        - name: Destination
          attr_as_list: next_trains
          modify: "x.destination_name == 'London Waterloo' ? 'Waterloo' : x.destination_name"
        - name: Time
          attr_as_list: next_trains
          modify: x.scheduled
        - name: Est
          attr_as_list: next_trains
          modify: x.estimated            
        - name: Status
          attr_as_list: next_trains
          prefix: '<style> cc { color: red; } </style>'
          modify: 'if (x.status == "LATE") { "<cc>" + x.status + "</cc>" } else { x.status }'
        - name: Delay
          attr_as_list: next_trains
          modify: 'if (x.estimated ) { (parseInt(x.estimated) * 60 + parseInt((x.estimated).slice(3,5))) - (parseInt(x.scheduled) * 60 + parseInt((x.scheduled).slice(3,5))) } else { " " }'
          suffix: 'm'
          align: right

The delay column does not handle trains Time & Est straddling midnight but otherwise seems to work ok. I check the attribute exists in a couple of places as a cancelled train can cause this fields to be null and then the table would otherwise not render at all. Likewise the use of the conditional card in the event that there are no trains running.

The real piece missing is the lack of a estimated arrival time at the destination. This does not appear to be supported by the underlying data source. I did come across http://www.realtimetrains.co.uk/api but thats a new component mini project…!

5 Likes

@robmarkcole

Hi Rob, is it possible to pull 15-20 buses from a timetable for each sensor please? or is that limited by the API?

Cheers
Mark

You can check what is returned for your stop using the developer tool https://developer.transportapi.com/docs?raml=https://transportapi.com/v3/raml/transportapi.raml##uk_bus_stop_atcocode_live_json

good point I didn’t think of that thanks… so this is limited by the API. At least I know I cannot drag more routes out :slight_smile:

1 Like

Hi @jchasey

Did you manage to look into the realtimetrains?

I did request a developer account but not had anything back.

1 Like

Hi @jchasey

So I tried your lovelace which is here:

  - type: conditional
    conditions: 
      - entity: sensor.next_train_to_eus
        state: "No departures"
    card:
      type: entities
      title: Trains to Waterloo
      show_header_toggle: false
      entities:
        - sensor.next_train_to_eus

  - type: conditional
    conditions: 
      - entity: sensor.next_train_to_eus
        state_not: "No departures"
    card:
      type: custom:flex-table-card
      title: Trains to Waterloo
      icon: mdi:train 
      entities:
        include: sensor.next_train_to_eus
      columns:
        - name: Type
          attr_as_list: next_trains
          modify: "x.origin_name == 'Tring' ? 'Fast' : ''"
        - name: Destination
          attr_as_list: next_trains
          modify: "x.destination_name == 'London Euston' ? 'Euston' : x.destination_name"
        - name: Time
          attr_as_list: next_trains
          modify: x.scheduled
        - name: Est
          attr_as_list: next_trains
          modify: x.estimated            
        - name: Status
          attr_as_list: next_trains
          prefix: '<style> cc { color: red; } </style>'
          modify: 'if (x.status == "LATE") { "<cc>" + x.status + "</cc>" } else { x.status }'
        - name: Delay
          attr_as_list: next_trains
          modify: 'if (x.estimated ) { (parseInt(x.estimated) * 60 + parseInt((x.estimated).slice(3,5))) - (parseInt(x.scheduled) * 60 + parseInt((x.scheduled).slice(3,5))) } else { " " }'
          suffix: 'm'
          align: right

but I get :

No card type configured.
[
  {
    "type": "conditional",
    "conditions": [
      {
        "entity": "sensor.next_train_to_eus",
        "state": "No departures"
      }
    ],
    "card": {
      "type": "entities",
      "title": "Trains to Waterloo",
      "show_header_toggle": false,
      "entities": [
        "sensor.next_train_to_eus"
      ]
    }
  },
  {
    "type": "conditional",
    "conditions": [
      {
        "entity": "sensor.next_train_to_eus",
        "state_not": "No departures"
      }
    ],
    "card": {
      "type": "custom:flex-table-card",
      "title": "Trains to Waterloo",
      "icon": "mdi:train",
      "entities": {
        "include": "sensor.next_train_to_eus"
      },
      "columns": [
        {
          "name": "Type",
          "attr_as_list": "next_trains",
          "modify": "x.origin_name == 'Tring' ? 'Fast' : ''"
        },
        {
          "name": "Destination",
          "attr_as_list": "next_trains",
          "modify": "x.destination_name == 'London Euston' ? 'Euston' : x.destination_name"
        },
        {
          "name": "Time",
          "attr_as_list": "next_trains",
          "modify": "x.scheduled"
        },
        {
          "name": "Est",
          "attr_as_list": "next_trains",
          "modify": "x.estimated"
        },
        {
          "name": "Status",
          "attr_as_list": "next_trains",
          "prefix": "<style> cc { color: red; } </style>",
          "modify": "if (x.status == \"LATE\") { \"<cc>\" + x.status + \"</cc>\" } else { x.status }"
        },
        {
          "name": "Delay",
          "attr_as_list": "next_trains",
          "modify": "if (x.estimated ) { (parseInt(x.estimated) * 60 + parseInt((x.estimated).slice(3,5))) - (parseInt(x.scheduled) * 60 + parseInt((x.scheduled).slice(3,5))) } else { \" \" }",
          "suffix": "m",
          "align": "right"
        }
      ]
    }
  }
]

What am I doing wrong?

I see the issue has closed on github; where do we go next?

The issue auto closed as nobody dealt with it, you can reopen and issue a fix

Hi,
This is working great but I can get three buses home, all from the same stop.
Can I merge the direction so that I get only one sensor? At the moment I have three but would prefer one that collates all three.
image

Thanks

You could use a template sensor to get the next departing service

I re-opened it on github, the issue is fixed in 0.99

Great devs!

Question: is the London tube API component available for Hassio? I’ve tried using the UK Transport sensor, but it seems that London tube stations / lines are not supported. Trying to use TfL Unified API, but don’t seem to find any references to set this up in Hassio.

Any guidance would be much appreciated!

Thanks!

You need https://www.home-assistant.io/integrations/london_underground/

Thanks! Got it working already. Also looking for an API that will tell me the arrival times from a certain tube station (similar to what is available with UK Transport, but for the tube)

Im not aware of an API for that

Hi,

I’m new to HA and stumbled across this…

I’m wondering is if there is some way to see is some kind of live departures board for a certain station showing the trains to depart regardless of destination along with any delay?

I have the same issue - the card is not populating - no errors in logs or anything else, strange.

The transport sensor is definitely working, as I have the next train, origin and destination stations etc. etc. (in a different tab) - I’d just like a list of train times for the next couple hours or so - any advice on troubleshooting?

Ok, scrap the post from 20 minutes ago, the card was sitting inside a vertical-stacked card and wasn’t rendering (although a new tab appeared, which obviously was blank… weird).

I purposely embedded it within the last vertical-stacked card and the flex-table-card is working fine. A superb custom piece of code; I especially like the regex matching :wink:

I wonder if the card can match template sensors that I’ve created for battery monitors? like: {{states.binary_sensor.motion_sensor_158d0001e15d57.attributes.battery_level}}
to:
{{states.binary_sensor.*.attributes.battery_level}}

Hi @jchasey

Wonder if you could assist with this, I’ve seen this a couple of times today and want to change “undefinedundefinedundefined” to “Cancelled”. Obviously a cancelled train won’t have an ETA but it’s obviously looking for an integer(?) and not a string.

image

Tried this, and doesn’t work
modify: ‘if (x.estimated == “undefined”) { “Cancelled” } else { x.estimated }’
(the whole column shows as “Cancelled” even when the trains have an ETA).

Thanks
B