Support for Dutch Railway API (NS)

I don’t have the skills to do so, but can someone have a look and advise if it is possible to include the dutch railway “Nederlandse Spoorwegen” (NS) into Home Assistant?

Some links:


Heya,

I could see what I can do here. Is there anything specific you wish to use? I’m guessing just the delays on known routes?

Based on the info I found so far:

  • display next three departure times
  • In case of a delay, the delay is shown.
  • display the track

What would be great indeed is that I would be able to send a notification in case of a delay. The actual android app isn’t working on that subject for me. See for example: https://github.com/aquatix/ns-notifications

An existing example is:

Could you give this code a shot? --> nederlandse_spoorwegen.py

Dump that in your custom_components/sensor/ directory and add the following config to your configuration.yaml:

sensor:
  - platform: nederlandse_spoorwegen
    email: <YOUR EMAIL ADDRESS>
    password: <API PASSWORD>
    departures:
      - name: Some Name
        from: <STATION>
        to: <STATION>

The station codes can be found here. API password can be requested here.

Cool, I’ve got this working, thanks!
(At first I added the configs under “transport”, but of course it should be set-up as a sensor)
I’m aiming for Telegram notifications in case of delays & maybe try customizations such as red times in case of delays.

Very nice! I tried it myself but somehow I can’t seem to get it working. Placed the nederlandse_spoorwegen.py in the custom_components dir, and added the config you provided, but it throws an error saying that it cannot find the custom component nederlandse_spoorwegen:

2017-09-29 18:07:42 ERROR (MainThread) [homeassistant.loader] Unable to find component sensor.nederlandse_spoorwegen 2017-09-29 18:07:43 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved Traceback (most recent call last): <[like 100+ error lines]> TypeError: 'bool' object is not iterable

Do you know what could be wrong?

Much thanks!

Cheers

I’m guessing the same (but notification through pushover). Other possibilities are to set certain lights to red in the morning in case of delays :wink:

Did you put it in the custom_components/sensor/ directory (note: sensor) ?

If you and maedium are happy with it, I’ll send it to be included in the default list of sensors.

After installing utilkit (pip install utilkit) it works like a charm, thnx.

Do you know how to install utilkit on hassio as well? Now it doesn’t work.

No sorry, maybe someone else knows. I run HA on ubuntu in a vm…

It seams that utilkit is needed to run the custom script. I use hassio, which does not include utilkit.
At the hassio forum they advise me the below post. Will you include the dependency when you will create an official component?

Creating an official component looks like a lot of work, so if anyone wants to do it (and take full credit), be my guest. The ns-api package already lists this as a dependency so I figured it’d be dragged in. I see no issues in listing it in the REQUIREMENTS bit though. Might even work around it all together (it’s not doing rocket sciency things).

Alright, so I gave it a go, cleaned some stuff up and created a PR.
Here’s the pull request if you’re interested: https://github.com/home-assistant/home-assistant/pull/10136

I cannot load it correctlty…? Does someone has any advise?

I’m running:

  • hass.io host version 1.1
  • hass.io supervisor version 0.74
  • home assistant version 0.57.1

The following config:

Config is:

sensor:
  - platform: nederlandse_spoorwegen
    email: [email protected]
    password: blablablabla
    routes:
      - name: Alphen-Utrecht
        from: Apn
        to: Ut
      - name: Utrecht-Alphen
        from: Ut
        to: Apn

The error is as follows:

2017-11-05 16:43:27 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up platform nederlandse_spoorwegen
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/entity_component.py", line 170, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=self.hass.loop)
  File "/usr/lib/python3.6/asyncio/tasks.py", line 352, in wait_for
    return fut.result()
  File "/usr/lib/python3.6/asyncio/futures.py", line 244, in result
    raise self._exception
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/sensor/nederlandse_spoorwegen.py", line 56, in setup_platform
    for departure in config.get(CONF_DEPARTURES):
TypeError: 'NoneType' object is not iterable

Did you download a copy and dump it in the custom_components directory? If so, remove it, the NS component is now part of home assistant (and the configuration changed slightly, giving you this error).

I did figure it that an hour ago. You are completely right. Thanks for the help, I does work correct now! :grinning:

Would it be possible to get the departure platform (VertrekSpoor) as attribute in HASS?

In the ns_api.py I see some logic that that attribute is actually send by the NS API, but it seems the object doesn’t include it as an attribute in HASS.

Ok, that was easy. The information was already available in the retrieved object, so making it available in te state attributes required just 4 lines of code:

1 Like