A custom component for Home Assistant providing a client for the HAFAS API. It can be used to retrieve connection data for a number of public transport companies in Europe, most notably Deutsche Bahn.
Credit goes to @kilimnik and pyhafas.
Once configured, this integration provides a sensor to show the next connections between two stations.
The state of the sensor is the timestamp of the next non-cancelled departure including delay. This allows to use the state programmatically, e.g., to compute a timedelta from it. Also, the sensor will be shown natively in Lovelace as a relative time, such as "in 5 minutes".
The attributes will contain the following additional data:
connections: a list of all connections retrieved (see below)- plus all information from the first non-canceled connection, except its list of
legs
Note: The connections attribute is not recorded in history, because it contains a lot of data.
And we don't want to bloat your home assistant database.
Each entry in the connections list contains the following data:
origin: name of origin stationdeparture: timestamp of planned departuredelay: timedelta of departure delaydestination: name of destination stationarrival: timestamp of planned arrivaldelay_arrival: timedelta of arrival delaytransfers: number of legs minus oneduration: timedelta from departure to arrivalcanceled: Boolean,trueif any leg is canceled elsefalseontime: Boolean,trueif zero departure delay elsefalseproducts: comma-separated list of line nameslegs: list of legs with more detailed information
Each connection can consist of multiple legs (different trains with transfers in between). A leg contains the following data:
origin: name of origin stationdeparture: timestamp of planned departureplatform: departure platformdelay: timedelta of departure delaydestination: name of destination stationarrival: timestamp of planned arrivalplatform_arrival: arrival platformdelay_arrival: timedelta of arrival delaymode: transport mode such astrainname: name of transport line such asRE123canceled: Boolean, if this leg is canceleddistance: walking distance if any (only walking legs)remarks: list of stringsstopovers: list of station names
Generate an output as in the old db integration, e.g., 11:11 + 11:
{%- set departure = state_attr('sensor.koln_hbf_to_frankfurt_main_hbf', 'departure') | as_local %}
{%- set delay = state_attr('sensor.koln_hbf_to_frankfurt_main_hbf', 'delay') | as_timedelta %}
{{- departure.strftime('%H:%M') }}
{%- if delay -%}
{{- ' + ' ~ (delay.total_seconds() // 60) | int -}}
{%- endif -%}While the main entity state will show as relative time (e.g. in 12 minutes) in your dashboard automatically, you might want to show other departures as relative time, too:
{% set departure = states.sensor.kobenhavn_h_to_malmo_c.attributes.connections[0].departure
+ states.sensor.kobenhavn_h_to_malmo_c.attributes.connections[0].delay
| as_timedelta %}
{% if departure > now() %}
in {{ time_until(departure) }}
{% else %}
too late
{% endif %}Only retrieve the planned departure datetime objects of all non-canceled connections:
{{ state_attr('sensor.koln_hbf_to_frankfurt_main_hbf', 'connections')
| rejectattr('canceled')
| map(attribute='departure')
| list }}