Code kopieren & zu File Editor
Echtzeit-Zugdaten für Pendler mit Abfahrtszeiten, Verspätungen, Verbindungssuche und Störungsmeldungen direkt von der Deutschen Bahn. - Echtzeit-Abfahrten: Aktuelle Zugdaten von konfigurierbaren B...
Echtzeit-Zugdaten für Pendler mit Abfahrtszeiten, Verspätungen, Verbindungssuche und Störungsmeldungen direkt von der Deutschen Bahn.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# Deutsche Bahn API Integration
# Echtzeit-Zugdaten für lokale Bahnhöfe mit Verspätungen und Störungen
# Basis-Konfiguration - API Key über https://developers.deutschebahn.com/
# Kostenloser Zugang für private Nutzung verfügbar
# REST-Sensoren für Timetable API
sensor:
# Nächste Abfahrten vom Heimatbahnhof
- platform: rest
name: "DB Abfahrten Heimat"
resource_template: >
https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/station/{{ states('input_text.db_home_station_id') }}/{{ now().strftime('%y%m%d/%H%M') }}
headers:
Authorization: !secret db_api_key
Accept: application/json
scan_interval: 300 # Alle 5 Minuten
value_template: "{{ value_json.length if value_json else 0 }}"
unit_of_measurement: "Züge"
json_attributes_path: "$[0:5]" # Erste 5 Abfahrten
json_attributes:
- dp
- ar
- category
- number
- line
- to
- platform
- delay
# Nächste Abfahrten vom Arbeitsplatz
- platform: rest
name: "DB Abfahrten Arbeit"
resource_template: >
https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/station/{{ states('input_text.db_work_station_id') }}/{{ now().strftime('%y%m%d/%H%M') }}
headers:
Authorization: !secret db_api_key
Accept: application/json
scan_interval: 300
value_template: "{{ value_json.length if value_json else 0 }}"
unit_of_measurement: "Züge"
json_attributes_path: "$[0:5]"
json_attributes:
- dp
- ar
- category
- number
- line
- to
- platform
- delay
# Verbindungssuche Hin (Morgens zur Arbeit)
- platform: rest
name: "DB Verbindung Hinfahrt"
resource_template: >
https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/journey/{{ states('input_text.db_home_station_id') }}/{{ states('input_text.db_work_station_id') }}/{{ (now() + timedelta(minutes=30)).strftime('%y%m%d/%H%M') }}
headers:
Authorization: !secret db_api_key
Accept: application/json
scan_interval: 600 # Alle 10 Minuten
value_template: >
{% if value_json and value_json|length > 0 %}
{{ value_json[0].duration if value_json[0].duration else "N/A" }}
{% else %}
"Keine Verbindung"
{% endif %}
json_attributes_path: "$[0]"
json_attributes:
- departure
- arrival
- duration
- changes
- delay
- products
# Verbindungssuche Rück (Abends nach Hause)
- platform: rest
name: "DB Verbindung Rückfahrt"
resource_template: >
https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/journey/{{ states('input_text.db_work_station_id') }}/{{ states('input_text.db_home_station_id') }}/{{ (now() + timedelta(hours=8)).strftime('%y%m%d/%H%M') }}
headers:
Authorization: !secret db_api_key
Accept: application/json
scan_interval: 900 # Alle 15 Minuten
value_template: >
{% if value_json and value_json|length > 0 %}
{{ value_json[0].duration if value_json[0].duration else "N/A" }}
{% else %}
"Keine Verbindung"
{% endif %}
json_attributes_path: "$[0]"
json_attributes:
- departure
- arrival
- duration
- changes
- delay
- products
# StaDa (Station Data) API für Bahnhofsinformationen
- platform: rest
name: "DB Station Info Heimat"
resource_template: >
https://apis.deutschebahn.com/db-api-marketplace/apis/station-data/v2/stations/{{ states('input_text.db_home_station_id') }}
headers:
Authorization: !secret db_api_key
Accept: application/json
scan_interval: 86400 # Einmal täglich
value_template: "{{ value_json.name if value_json else 'Unbekannt' }}"
json_attributes:
- name
- eva
- category
- federalState
- hasWiFi
- hasLockerSystem
- hasLostAndFound
- hasCarRental
- hasTravelNecessities
# FaSta API für Facility Status (Aufzüge, Rolltreppen)
- platform: rest
name: "DB Facilities Status"
resource_template: >
https://apis.deutschebahn.com/db-api-marketplace/apis/fasta/v2/facilities/{{ states('input_text.db_home_station_id') }}
headers:
Authorization: !secret db_api_key
Accept: application/json
scan_interval: 600
value_template: >
{% if value_json and value_json.facilities %}
{{ value_json.facilities | selectattr('state', 'equalto', 'ACTIVE') | list | length }}
{% else %}
0
{% endif %}
unit_of_measurement: "Aktiv"
json_attributes_path: "$.facilities[*]"
json_attributes:
- type
- state
- description
# Template-Sensoren für benutzerfreundliche Darstellung
- platform: template
sensors:
db_next_departure_time:
friendly_name: "Nächste Abfahrt"
value_template: >
{% set departures = state_attr('sensor.db_abfahrten_heimat', 'dp') %}
{% if departures and departures|length > 0 %}
{% set next_dp = departures[0] %}
{% if next_dp.delay %}
{{ next_dp.time }} (+{{ next_dp.delay }})
{% else %}
{{ next_dp.time }}
{% endif %}
{% else %}
"Keine Daten"
{% endif %}
icon_template: "mdi:train"
db_next_departure_platform:
friendly_name: "Nächster Bahnsteig"
value_template: >
{% set departures = state_attr('sensor.db_abfahrten_heimat', 'platform') %}
{% if departures and departures|length > 0 %}
{{ departures[0] }}
{% else %}
"Unbekannt"
{% endif %}
icon_template: "mdi:sign-direction"
db_next_departure_destination:
friendly_name: "Nächstes Ziel"
value_template: >
{% set departures = state_attr('sensor.db_abfahrten_heimat', 'to') %}
{% if departures and departures|length > 0 %}
{{ departures[0] }}
{% else %}
"Unbekannt"
{% endif %}
icon_template: "mdi:map-marker"
db_commute_duration:
friendly_name: "Arbeitsweg Dauer"
value_template: >
{% set duration = state_attr('sensor.db_verbindung_hinfahrt', 'duration') %}
{% if duration %}
{{ duration }} min
{% else %}
"Unbekannt"
{% endif %}
icon_template: "mdi:clock-outline"
db_commute_changes:
friendly_name: "Arbeitsweg Umstiege"
value_template: >
{% set changes = state_attr('sensor.db_verbindung_hinfahrt', 'changes') %}
{% if changes is not none %}
{{ changes }}
{% else %}
"Unbekannt"
{% endif %}
icon_template: "mdi:swap-horizontal"
db_total_delay:
friendly_name: "Gesamtverspätung heute"
unit_of_measurement: "min"
value_template: >
{% set home_delay = state_attr('sensor.db_abfahrten_heimat', 'delay') %}
{% set work_delay = state_attr('sensor.db_abfahrten_arbeit', 'delay') %}
{% set total = 0 %}
{% if home_delay %}
{% for delay in home_delay %}
{% if delay %}
{% set total = total + delay %}
{% endif %}
{% endfor %}
{% endif %}
{% if work_delay %}
{% for delay in work_delay %}
{% if delay %}
{% set total = total + delay %}
{% endif %}
{% endfor %}
{% endif %}
{{ total }}
# Input Helper für Bahnhofs-IDs
input_text:
db_home_station_id:
name: "Heimat-Bahnhof ID"
initial: "8000105" # Beispiel: Frankfurt Hbf
max: 10
pattern: '[0-9]*'
mode: text
db_work_station_id:
name: "Arbeitsplatz-Bahnhof ID"
initial: "8000261" # Beispiel: München Hbf
max: 10
pattern: '[0-9]*'
mode: text
# Boolean Helper für Benachrichtigungen
input_boolean:
db_notifications_enabled:
name: "Bahn-Benachrichtigungen aktiviert"
initial: true
icon: "mdi:bell-ring"
db_delay_alerts:
name: "Verspätungs-Alarme"
initial: true
icon: "mdi:alert-circle"
db_commute_mode:
name: "Pendler-Modus aktiv"
initial: false
icon: "mdi:briefcase"
# Number Helper für Verspätungs-Schwelle
input_number:
db_delay_threshold:
name: "Verspätungs-Alarm ab"
min: 5
max: 60
step: 5
initial: 15
unit_of_measurement: "min"
icon: "mdi:clock-alert"
# Binäre Sensoren für Alarme
binary_sensor:
- platform: template
sensors:
db_significant_delay:
friendly_name: "Erhebliche Verspätung"
value_template: >
{{ states('sensor.db_total_delay') | float >
states('input_number.db_delay_threshold') | float }}
device_class: problem
delay_on: "00:02:00"
db_elevator_outage:
friendly_name: "Aufzug-Ausfall"
value_template: >
{% set facilities = state_attr('sensor.db_facilities_status', 'type') %}
{% set states_list = state_attr('sensor.db_facilities_status', 'state') %}
{% if facilities and states_list %}
{% for i in range(facilities|length) %}
{% if facilities[i] == 'ELEVATOR' and states_list[i] != 'ACTIVE' %}
true
{% endif %}
{% endfor %}
false
{% else %}
false
{% endif %}
device_class: problem
db_connection_available:
friendly_name: "Verbindung verfügbar"
value_template: >
{{ states('sensor.db_verbindung_hinfahrt') != 'Keine Verbindung' }}
device_class: connectivity
db_commute_time:
friendly_name: "Pendelzeit aktiv"
value_template: >
{% set now_time = now().hour * 100 + now().minute %}
{% set is_workday = now().weekday() < 5 %}
{% set morning_commute = 600 <= now_time <= 1000 %} # 06:00-10:00
{% set evening_commute = 1600 <= now_time <= 2000 %} # 16:00-20:00
{{ is_workday and (morning_commute or evening_commute) and
is_state('input_boolean.db_commute_mode', 'on') }}
# Automatisierungen für Bahn-Benachrichtigungen
automation:
- alias: "DB Verspätungs-Alarm"
trigger:
- platform: state
entity_id: binary_sensor.db_significant_delay
to: "on"
condition:
- condition: state
entity_id: input_boolean.db_delay_alerts
state: "on"
- condition: state
entity_id: binary_sensor.db_commute_time
state: "on"
action:
- service: notify.mobile_app
data:
title: "🚆 Bahn-Verspätung"
message: >
Gesamtverspätung: {{ states('sensor.db_total_delay') }} Minuten
Nächste Abfahrt: {{ states('sensor.db_next_departure_time') }}
data:
tag: "train_delay"
group: "transport"
actions:
- action: "check_alternatives"
title: "Alternativen prüfen"
- alias: "DB Aufzug-Ausfall"
trigger:
- platform: state
entity_id: binary_sensor.db_elevator_outage
to: "on"
condition:
- condition: state
entity_id: input_boolean.db_notifications_enabled
state: "on"
action:
- service: notify.mobile_app
data:
title: "♿ Aufzug außer Betrieb"
message: "Aufzug am {{ state_attr('sensor.db_station_info_heimat', 'name') }} defekt"
data:
tag: "elevator_outage"
- alias: "DB Morgendliche Pendler-Info"
trigger:
- platform: time
at: "07:00:00"
condition:
- condition: state
entity_id: input_boolean.db_commute_mode
state: "on"
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
action:
- service: notify.mobile_app
data:
title: "🌅 Pendler-Info"
message: >
Arbeitsweg: {{ states('sensor.db_commute_duration') }}
Umstiege: {{ states('sensor.db_commute_changes') }}
Nächste Abfahrt: {{ states('sensor.db_next_departure_time') }}
Bahnsteig: {{ states('sensor.db_next_departure_platform') }}
- alias: "DB Abendliche Rückfahrt-Erinnerung"
trigger:
- platform: time
at: "17:30:00"
condition:
- condition: state
entity_id: input_boolean.db_commute_mode
state: "on"
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
action:
- service: notify.mobile_app
data:
title: "🏠 Rückfahrt-Info"
message: >
Rückfahrt: {{ states('sensor.db_verbindung_rückfahrt') }}
{% if states('sensor.db_total_delay') | float > 10 %}
⚠️ Aktuelle Verspätung: {{ states('sensor.db_total_delay') }} min
{% endif %}
# Script für manuelle Verbindungssuche
script:
db_search_connection:
alias: "DB Verbindung suchen"
fields:
from_station:
description: "Start-Bahnhof ID"
example: "8000105"
to_station:
description: "Ziel-Bahnhof ID"
example: "8000261"
departure_time:
description: "Abfahrtszeit (optional)"
example: "08:00"
sequence:
- service: notify.persistent_notification
data:
title: "DB Verbindungssuche"
message: "Suche Verbindung von {{ from_station }} nach {{ to_station }}..."
- service: homeassistant.update_entity
target:
entity_id: sensor.db_verbindung_hinfahrt
- delay: 5
- service: notify.mobile_app
data:
title: "🔍 Verbindung gefunden"
message: >
Dauer: {{ state_attr('sensor.db_verbindung_hinfahrt', 'duration') }} min
Umstiege: {{ state_attr('sensor.db_verbindung_hinfahrt', 'changes') }}
Abfahrt: {{ state_attr('sensor.db_verbindung_hinfahrt', 'departure') }}
# Utility Meter für Statistiken
utility_meter:
db_delays_monthly:
source: sensor.db_total_delay
cycle: monthly
name: "Monatliche Verspätungen"
# Counter für Störungen
counter:
db_disruptions_today:
name: "Störungen heute"
initial: 0
step: 1
icon: "mdi:train-car-passenger-door"