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…!