Intergation Nederlandse Spoorwegen API

Hi All,

Will it possible that this intergration will get a update so you can add a time space?

so you get trains after given time for example 30minutes

Maybe also with more results in a list.

This might be better in Feature Requests. But does sound useful.

Because you’re here, if you wanted, you can make your own template sensor that shows the “next” scheduled if time is less than 30 minutes of the current scheduled. But you’ll lose all of the useful attributes as they only apply to the current scheduled…


# Example configuration.yaml entry
sensor:
  - platform: template
    sensors:
      # Template sensor to show the next train > 30 minutes from now from X to Y.
      future_departure_x_y:
        # Follows the nederlandse_spoorwegen sensor for X to Y
        entity_id: sensor.X_Y
        friendly_name: "Train from X to Y"
        # For each update, if the next train is < 30 minutes (60*30 seconds) away, show the 'next' departure instead.
        value_template: >-
        {% if (as_timestamp(now()) - as_timestamp(states('sensor.X_Y')) < 60*30 %}
          {{ state_attr('sensor.X_Y', 'next') }}
        {% else %}
          {{ states('sensor.X_Y') }}

Now you can use both sensors to make decisions. Oftentimes you really want the current train info…especially if you’re on your way to the train or are close by. But you want to make automations based on the next train you can actually catch. If you were to just search for all trains > 30 minutes, you’d lose all of the useful information for the train you are trying to catch and have to open a different app to find out the updated info. So with this approach, you can use both!

1 Like

Thank you for the code. I will try this when i’m at home.
I will do a feature request on Github

Hi,

I get a error in the template (developer tool). I cant find it. blind for the error I guess… haha

Error rendering template: TemplateSyntaxError: unexpected ‘}’, expected ‘)’

{% if (as_timestamp(now()) - as_timestamp(states('sensor.asdz_utr')) < 60*30 %}
  {{ state_attr('sensor.asdz_utr', 'next') }}
{% else %}
  {{ states('sensor.asdz_utr') }}

Ah, I see. The sensor is returning just HH:MM time, not a timestamp. Well, that and syntax error with wrong number of parens…and no endif…

Will have to do some dang conversions. Your favorite ones will do…I’m just going to use a ptime struct and math.

        # Convert current time (ct) and next departure (nt) into
        # a ptime object and do some math to determine difference
        value_template: >-
          {% set cur = strptime((as_timestamp(now()) | timestamp_custom('%H:%M')), "%H:%M")  %}
          {% set nt = strptime(states('sensor.asdz_utr'), "%H:%M") %}
          {% if (nt.hour - cur.hour)*60 + (nt.minute - cur.minute) < 30 %}
            {{ state_attr('sensor.asdz_utr', 'next') }}
          {% else %}
            {{ states('sensor.asdz_utr') }}
          {% endif %}

I still get a error. But it seems it don’t get all the trains from the API.
To bad im not a good developer. I see in developer tool to follow information for the sensor:

going: true 
departure_time_planned: 10:26 
departure_time_actual: 10:26 
departure_delay: false 
departure_platform_planned: 2 
departure_platform_actual: 2 
arrival_time_planned: 10:48 
arrival_time_actual: 10:48 
arrival_delay: false 
arrival_platform_planned: 18 
arrival_platform_actual: 18 
next: 10:40 
status: normal 
transfers: 0 
route: Amsterdam Zuid,Utrecht Centraal 
remarks: null 
attribution: Data provided by NS 
friendly_name: asdz-utr 
icon: mdi:train`

I have access to the API Portal of NS (im working for NS) but you can register also external. And then you have access to the “Reisinformatie” Lots of possible api examples to show things.

Would be so nice if someone can rebuild this integration with lots of more possible options

I made a pull request with exactly this.

1 Like

And 1,5 year after the pull request, nothing happened because changes are not allowed until the whole integration has been rebuilt :slight_smile:

This offset would be very nice, because it’s kind of useless right now. At 10:06 now I see “next train is at 10:05” until it updates. Would be nice if you can create automations based on the train in an hour or anything, without messing with the config files everytime :slight_smile:

2 Likes

Any update on this?

The official docu still doesn’t list an offset and whenever i tried {% conditional code it always did not work

And in its current form the Integration is pretty useless as it mostly shows trains that are just departured and not those who are going to and i would like to add a couple of minutes of walking distance to it, otherwise i dont see how this is useful.

This feature is highly needed. In it’s current form the integration is quite useless.

What does it take to get the rebuild going?

I’ve tried replacing the time after time: with a template sensor that has the time offset built in, but I just get an error.

1 Like

Hi there everybody,

I’m quite new to Home Assistant but would love to have my train time table in the morning. I tried everything to add the sensor in the yaml.config but it keeps not accepting it. I tried to copy the example of the intergration, but also that it’s not working.

I do have the api key, and in the yaml.config there are already a few sensors. I’m very helpless. Hope anyone can help out.

Thanks in advance!