Notification on delay from Deutsche Bahn

Hi, I just don’t get it. Messing around since a while: the idea is to notify if the S-Bahn has more than 5 minutes delay.

Sensor: https://home-assistant.io/components/sensor.deutsche_bahn/

price: null
ontime: true
departure: 12:20
arrival: 13:01
next: 12:44
next_on: 13:09
friendly_name: A to B
products: S
delay: 0
transfers: 0
icon: mdi:train
time: 0:41

Automation:

- alias: "Notify on S-Bahn delay. Home -> Work"
  trigger:
    - platform: template
      value_template: "{{ states.sensor.a_to_b.attributes.ontime }}"
  condition:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
 # how to add the condition of more than 5 minutes delay here?
  action:
    - service: notify.notify
      data:
        title: "S-Bahn is late"
        message: "S-Bahn currently has x minutes delay" # how to add the value of delay here?

I just don’t get it to work. Thanks for insights :slight_smile:

Try that:

- alias: "Notify on S-Bahn delay. Home -> Work"
  trigger:
    - platform: template
      value_template: "{{ states.sensor.a_to_b.attributes.delay | int > 5 }}"
  condition:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: notify.notify
      data:
        title: "S-Bahn is late"
        message: "S-Bahn currently has {{ states.sensor.a_to_b.attributes.delay }} minutes delay"

Thank you so much, now I get it. I also added some other conditions now to be only notified during certain time frames and when I am at home or at work:

- alias: "Notify on S-Bahn delay. Home -> Work"
  trigger:
    - platform: template
      value_template: "{{ states.sensor.a_to_b.attributes.delay | int > 5 }}"
  condition:
    - condition: time
      after: '06:30:00'
      before: '08:30:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: state
      entity_id: device_tracker.my_iphone
      state: home
  action:
    - service: notify.xxx
      data:
        title: "Zug um {{ states.sensor.a_to_b.attributes.departure }} nach XXX +{{ states.sensor.a_to_b.attributes.delay }} min."
        message: "Nächste S-Bahn: {{ states.sensor.a_to_b.attributes.next }} Uhr"

- alias: "Notify on S-Bahn delay. Work -> Home"
  trigger:
    - platform: template
      value_template: "{{ states.sensor.b_to_a.attributes.delay | int > 5 }}"
  condition:
    - condition: time
      after: '15:30:00'
      before: '18:30:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: state
      entity_id: device_tracker.my_iphone
      state: work
  action:
    - service: notify.xxx
      data:
        title: "Zug um {{ states.sensor.b_to_a.attributes.departure }} Uhr nach Hause +{{ states.sensor.b_to_a.attributes.delay }} min."
        message: "Nächste S-Bahn: {{ states.sensor.b_to_a.attributes.next }} Uhr"

Manually triggering resulted in notifications alread, let’s see what the time brings :wink:

1 Like

Hey,
thanks for the example. Is there a way to check for a specific type of train in the search. I found the attribute ‘product’ but this is only linked to the next train. If I’m only taking the RB then this will be blocked by an IC that is departing 2 minutes earlier and my delay notification is to late.
Any idea?

@nodomain could you please post your sensors.yaml?
i am a total newbie and don’t get this sensor to work with this use case :wink:

Hey,
maybe this helps:

sensor:
  - platform: deutsche_bahn
    from: DarmstadtHbf
    to: Frankfurt(Main)Hbf

That’s all I’m using. Together with:

zug_delay:
        value_template: '{{ states.sensor.DarmstadtHbf_to_FrankfurtMainHbf.attributes.delay | float }}'
        friendly_name: 'delay'
        unit_of_measurement: 'min'

Hope this helps you :upside_down_face:

Can anyone post the entire config for this plugin? I can’t get it working, even with the posts in this thread. The documentation on the docs page is not so useful…
Just want to display the delay for a 9:00h clock train from Münster(Westf)Hbf to Salzbergen