ha_realtime_trains_api icon indicating copy to clipboard operation
ha_realtime_trains_api copied to clipboard

Add Aggregate delay / duration data

Open phillprice opened this issue 2 years ago • 0 comments

Adds a 'delay' value to the departure, stop and arrival based on the local (estimatedTs - scheduledTs) for each.

Also aggregates this data across all tracked trains to give a count, min, average and max delay, along with similar stats for durations using existings data.

Adds this as an attribute alongside next_trains called aggregate_data

aggregate_data:
  delays:
    arrival:
      count: 1
      min: 42
      max: 42
      average: 42
    stop:
      count: 4
      min: 1
      max: 43
      average: 17
    departure:
      count: 4
      min: 1
      max: 43
      average: 17
  durations:
    count: 10
    min: 24
    max: 52
    average: 34

I use this in a mushroom card like this... 12E3DD5E-74AD-40A8-BD02-E6105B833D61

type: custom:mushroom-template-card
primary: Start to End
secondary: >-
  Leaving: 
  {{states.sensor.next_train_from_abc_to_def.attributes.aggregate_data.delays.departure.count}}
  trains
  ({{states.sensor.next_train_from_abc_to_def.attributes.aggregate_data.delays.departure.average}}m)

  Arrive:
  {{states.sensor.next_train_from_abc_to_def.attributes.aggregate_data.delays.arrival.count}}
  trains
  ({{states.sensor.next_train_from_abc_to_def.attributes.aggregate_data.delays.arrival.average}}m)

  Shortest:
  {{states.sensor.next_train_from_abc_to_def.attributes.aggregate_data.durations.min}}
  mins
icon: ''
entity: sensor.next_train_from_abc_to_def
multiline_secondary: true
layout: vertical
fill_container: false

Although template sensors can be used to extract the data.

phillprice avatar Aug 17 '22 21:08 phillprice