The Problem
Currently, the integration fetches a fixed number of upcoming departures (e.g., maxJny: 20) for a given station and applies direction/line filters locally (post-request).
If the departure station is busy, those 20 slots are quickly filled up by other trains/buses, meaning the API response might only cover the next 4 minutes of real time. If a user's specific bus is 10 minutes away, it gets excluded from the HAFAS payload entirely, resulting in the sensor showing no departures until the vehicle is less than 6 minutes away for example.
The Solution
The ÖBB HAFAS API natively supports server-side direction filtering (pre-request) via the dirLoc paramete, offloading the filter to the API, so the amount of journeys can take us a lot further.
Proof of Concept
Based on a captured payload from the ÖBB web frontend, we can pass a destination Station object and append dirLoc to the StationBoard request data.
1. Injecting dirLoc into the HAFAS request (req_data):
# Assuming `direction_station` is a resolved Station object
if direction_station:
dir_loc: dict[str, Any] = {"extId": direction_station.id}
if direction_station.name:
dir_loc["name"] = direction_station.name
if direction_station.lid:
dir_loc["lid"] = directionStation.lid
req_data["dirLoc"] = dir_loc
This is the JSON I reverse-engineered from the departures frontend:
{
"name": "Wien Praterstern Bahnhof (U)",
"lid": "A=1@O=Wien Praterstern Bahnhof (U)@X=16392153@Y=48219082@U=81@L=1290201@p=1787822408@",
"extId": "1290201"
}
Example implementation in _async_update_data:
# Resolve the target station first to get its `lid` and `name`
direction_station = await self.client.resolve_station("1290201") # e.g., from config options
try:
return await self.client.get_departures(
self.station,
max_journeys=DEFAULT_MAX_JOURNEYS, # e.g., 20
prod_value=_prod_value(transport_modes),
direction_station=direction_station, # Pass to client
# ... other parameters
)
except HafasError as err:
raise UpdateFailed(str(err)) from err
I got this to run in my local home assistant integration for the use case Floridsdorf Bahnhof with direction Praterstern.
The Problem
Currently, the integration fetches a fixed number of upcoming departures (e.g.,
maxJny: 20) for a given station and applies direction/line filters locally (post-request).If the departure station is busy, those 20 slots are quickly filled up by other trains/buses, meaning the API response might only cover the next 4 minutes of real time. If a user's specific bus is 10 minutes away, it gets excluded from the HAFAS payload entirely, resulting in the sensor showing no departures until the vehicle is less than 6 minutes away for example.
The Solution
The ÖBB HAFAS API natively supports server-side direction filtering (pre-request) via the
dirLocparamete, offloading the filter to the API, so the amount of journeys can take us a lot further.Proof of Concept
Based on a captured payload from the ÖBB web frontend, we can pass a destination
Stationobject and appenddirLocto theStationBoardrequest data.1. Injecting
dirLocinto the HAFAS request (req_data):This is the JSON I reverse-engineered from the departures frontend:
{ "name": "Wien Praterstern Bahnhof (U)", "lid": "A=1@O=Wien Praterstern Bahnhof (U)@X=16392153@Y=48219082@U=81@L=1290201@p=1787822408@", "extId": "1290201" }Example implementation in
_async_update_data:I got this to run in my local home assistant integration for the use case Floridsdorf Bahnhof with direction Praterstern.