Fetch departures
Use this action to fetch a list of upcoming departures for one of your Swiss public transport stationboards. A stationboard is an entry you set up with Departures from a station. The action returns the next departures from its stations, and only those that match its Stops at stations if you set any.
For example, you can show more than the next three departures on a dashboard, or send the next departures in a notification.
This action returns data that you can use in later steps of the same automation or script.
Using this action from the user interface
If you prefer building automations and scripts visually, Home Assistant walks you through this action step by step. You pick what to target, tweak a few options, and save. No YAML knowledge required.
To fetch departures from an automation or a script:
- Go to Settings > Automations & scenes.
- Open an existing automation or script, or select Create automation > Create new automation.
- If you’re setting up a new automation, add a trigger in the When section. Scripts don’t need a trigger. They run when something else calls them.
- In the Then do section, select Add action.
- From the search box, search for and select Swiss public transport: Fetch departures.
- Select the stationboard as the Instance to fetch departures for. Optionally, set a Limit for the number of departures to return. In the Response variable field, enter a name to store the departures in, such as
departures. - Select Save.
This action does not support targets. In the UI, you are not prompted to choose an area, device, entity, or label.
Options in the UI
Using this action in YAML
If you work directly in YAML, or you want to know exactly what Home Assistant does under the hood, this section has the technical reference. It lists the field names you use in YAML, their types, and which ones are required. For how an automation or a script is built in YAML, refer to Automations in YAML and Scripts.
In YAML, refer to this action as swiss_public_transport.fetch_departures. A basic example looks like this:
action: swiss_public_transport.fetch_departures
data:
config_entry_id: YOUR_CONFIG_ENTRY_ID
limit: 3
response_variable: departures
This fetches the next three departures for the selected stationboard. Replace YOUR_CONFIG_ENTRY_ID with the ID of your stationboard. If you paste the example into the visual editor, you can select the stationboard under Instance instead.
Options in YAML
Response data
This action always returns data, so the response variable is required. If you leave it out, the action fails when it runs. Use the response variable to store the data for later steps of the same automation or script. For details, refer to Action response data.
The action returns a departures list. Each departure includes the following information:
-
departure: The departure time. -
delay: The departure delay, in minutes. -
platform: The platform or stop the departure leaves from. -
line: The line name of the departure. -
destination: The name of the final stop.
Good to know
- When you do not set a limit, the action returns three departures.
- This action only works with stationboards. For a connection between two stations, use Fetch connections instead.
- If the stationboard has Stops at stations and the stop is very busy, the action can return fewer departures than the limit.
Try it yourself
Ready to test this? Open Settings > Tools > Actions, search for this action, fill in the fields, and select Perform action. You see what happens on your actual entitiesAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more] without writing a line of YAML.
More examples
Real scenarios where this action shows up in automations and scripts. Copy any example and adapt it to your setup. In the visual editor, select the UI options listed in the example or paste the YAML code.
You don’t need to edit YAML to use these examples. Copy a YAML snippet from this page, open the visual automation editor in Home Assistant, and press Ctrl+V (or Cmd+V on Mac). Home Assistant automatically converts the pasted YAML into the visual editor format, whether it’s a full automation, a single trigger, a condition, or an action.
Automation: sending the next departures before you leave
Get the next five departures from your stop as a notification on your phone every morning, so you know when to leave the house.
- Trigger: Time: at 07:00
-
Action: Fetch departures
- Instance: Your stationboard
- Limit: 5
-
Response variable:
stationboard
-
Action: Send a notification message
-
Target: My Device (
notify.my_device) - Message: A template that lists the time, line, and destination of each departure. Copy it from the YAML example.
-
Target: My Device (
YAML example for sending the next departures in the morning
Replace YOUR_CONFIG_ENTRY_ID by selecting your stationboard under Instance after you paste the example.
alias: "Send the next departures in the morning"
triggers:
- trigger: time
at: "07:00:00"
actions:
- action: swiss_public_transport.fetch_departures
data:
config_entry_id: YOUR_CONFIG_ENTRY_ID
limit: 5
response_variable: stationboard
- action: notify.send_message
target:
entity_id: notify.my_device
data:
message: >
{% for departure in stationboard.departures %}
{{ as_timestamp(departure.departure) | timestamp_custom('%H:%M') }}
{{ departure.line }} to {{ departure.destination }}.
{% endfor %}
Automation: sending the next departures when you leave work
Get the next departures from the stop near your workplace as soon as you leave. This example uses a stationboard for that stop and a zone named Work.
-
Trigger: Person leaves a zone
-
Target: Your person (
person.me) -
Zone: Work (
zone.work)
-
Target: Your person (
-
Action: Fetch departures
- Instance: Your stationboard
- Limit: 3
-
Response variable:
stationboard
-
Action: Send a notification message
-
Target: My Device (
notify.my_device) - Message: A template that lists the time, line, and destination of each departure. Copy it from the YAML example.
-
Target: My Device (
YAML example for sending the next departures when you leave work
Replace YOUR_CONFIG_ENTRY_ID by selecting your stationboard under Instance after you paste the example.
alias: "Send the next departures when leaving work"
triggers:
- trigger: zone
entity_id: person.me
zone: zone.work
event: leave
actions:
- action: swiss_public_transport.fetch_departures
data:
config_entry_id: YOUR_CONFIG_ENTRY_ID
limit: 3
response_variable: stationboard
- action: notify.send_message
target:
entity_id: notify.my_device
data:
message: >
{% for departure in stationboard.departures %}
{{ as_timestamp(departure.departure) | timestamp_custom('%H:%M') }}
{{ departure.line }} to {{ departure.destination }}.
{% endfor %}
Still stuck?
To test the action on its own, run it in the automation editor with Run action, and check the message that it shows. To see what the action did when the automation ran, open the trace of that run. For details, refer to Testing automations. For common problems and their solutions, refer to Troubleshooting automations.
If that doesn’t help, the Home Assistant community is quick to help: join Discord for real-time chat, post on the community forum with the action you’re calling and what you expected to happen, or share on our subreddit /r/homeassistant.
AI assistants like ChatGPT or Claude can also explain actions or suggest the right one when you describe what you want in plain language.