Anyone using västtrafik component?

Would like to know if you got it running lately or if it might be broken. Read somewhere about api changes from västtrafik.

I cant get it running but would be nice to know it works for others.

I use it, it works.

1 Like

I just found out that you had to add resplanerare to the application you created and then it works i was a little to quick at first :wink:

So to get it working be sure to set up your application correctly at vasttrafik webpage first.

1 Like

I also must admit that I can see the following error quite often in my log:

17-01-18 22:54:46 homeassistant.core: Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 233, in _step
    result = coro.throw(exc)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 387, in _update_entity_states
    yield from update_coro
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/entity.py", line 216, in async_update_ha_state
    yield from self.hass.loop.run_in_executor(None, self.update)
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/util/__init__.py", line 296, in wrapper
    result = method(*args, **kwargs)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/sensor/vasttrafik.py", line 119, in update
    date=datetime.now()+self._delay)
  File "/home/homeassistant/.homeassistant/deps/vasttrafik/journy_planner.py", line 116, in departureboard
    **request_parameters)
  File "/home/homeassistant/.homeassistant/deps/vasttrafik/journy_planner.py", line 149, in _request
    str(res.content))
Exception: Error: 401b'{"fault":{"code":900901,"message":"Invalid Credentials","description":"Access failure for API: /bin/rest.exe/v2, version: v2. Make sure your have given the correct access token"}}'

Not sure if it is Västtrafik or HASS bug.

Hi! Do time zone setting mess with the västtrafik component?

My shows 2h off, 13:35 intsted of 15:35.

Hi, I don’t use the västtrafik compnent but I use västtrafik’s API for my API. The HA component uses the same API.

It works as it should, no problem.

Your problem is that Home-assistant reports time in UTC, you need somehow compensate for that.

Maybe you could do some templating using the time-sensor?

- platform: time_date
  display_options:
    - 'time'
  scan_interval: 30

This reports the correct time in Sweden. You can use this sensor and the time in HA to track the difference between local time and UTC (due to winter/summer time), then compensate for this.

Or the simple way is to make a sensor that adds 2 hours to UTC, but in wintertime you need to manually adjust it to +1h.

Have you set your location in HA configuration.yaml to Stockholm/Sweden? Maybe that’s what’s missing for the component to work. If you have done that, try what I suggested above.

Hi! Thanks for the responce, i´m a newbe on this so it has been alot of try and error :slight_smile:

The timezone is set to: time_zone: Europe/Stockholm

The issue seams to just mess with västtrafik. The google travel API displays the time correctly.
The Timeline is correct time in västtrafik it´s the depature time that is off. Or am i not understanding the image correctly?

This is due to the UTC-timing in HA. It’s probably due to the component reporting time using the time HA reports. And HA reports the time in UTC.

So you need to make a sensor template if you want to fix it, let me know if you need any help with the templating.

ooh…that looks a bit over my knowlege in coding :slight_smile:
Is this suppose to only be used with vässtrafik?

I was just about making a template sensor for you, I started to add the VT component when I saw that it works as it should.

I used the example in the component example:

 - platform: vasttrafik
   key: xxxxxxxxxxxxxxxxx
   secret: *********************
   departures:
     - name: Mot järntorget
       from: Musikvägen
       heading: Järntorget
       delay: 10

Can you add the time sensor in the post above “-platform : time_date” and report what time it shows you? It should report local Swedish time.

This is my setup:

- platform: vasttrafik
    key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    departures:
      - name: Mot Stationen
        from: Stormgatan
        heading: Kungsbacka Station
      - name: Till Göteborg
        from: Kungsbacka station
        heading: Göteborg C

time_date shows correct.

Preformatted text

Put the example in also…

I tried your code and the component reports time as it should. Do you have a recent version of home-assistant?

0.45.1 this is relly bugging me! might i have done something wrong on the västtrafik API site?

No, Västtrafik API and the component works as it should. Try this, set your location to New york and report what sensor.time and sensor.vasttrafik reports.

Thanks, it’s pretty clear that in your case the component ignores the time_zone and just reports in UTC.

sensor.time looks at the time_zone hence it displays correct. I have 0.45.0, I’ll look if 0.45.1 has a version of västtrafik component that causes it to display UTC time.

Can you downgrade HA?

sudo pip3 install homeassistant==0.45.0

Follow the usual update procedure but use line above instead of

sudo pip3 install homeassistant

Does this help?
The västtrafik component was updated 24 days ago, should be the same one as in 0.45.0.

Hi again!

I will test that tomorrow, Thanks for the support!

Can you test this in terminal where HA is running:

python -c 'import time; print time.localtime()'

Look at the tm_hour, is it local time?

If no, then your problem is that your system does not have correct local time. Change the time of the system

If yes, then I created a python-script for you that is good as a temporary fix until you sort it out. The compensation of time couldn’t be done in HA, hence python.

  • Create a folder: ~/.homeassistant/python
  • Create a file called “vasttrafik_localtime.py” in python-folder above

Paste this code in vasttrafik_localtime.py

import requests
import json
import time

############################################
# User-defined settings
############################################
url_homeassistant = '192.168.1.225'
port_homeassistant = '8123'
entity_id = 'sensor.till_goteborg'
###########################################


url = 'http://' + url_homeassistant  + ':' + port_homeassistant + '/api/states/' + entity_id
r = requests.get(url)
myjSon = json.loads(r.text)
mytime = myjSon['state']


thistime = time.strptime(mytime,'%H:%M')

myhour = thistime.tm_hour
myminutes = thistime.tm_min

if time.localtime().tm_hour < time.gmtime().tm_hour :
  hourdifference = time.gmtime().tm_hour - time.localtime().tm_hour - 20
else:
  hourdifference = time.localtime().tm_hour - time.gmtime().tm_hour


myhour = myhour + hourdifference
if myhour > 23:
  myhour = myhour - 24

myhour = str(myhour)

if len(myhour) < 2:
  myhour = str(0) + str(myhour)


mytime = myhour + ':'+str(myminutes)

print(mytime)

Change only the user-defined dettings.

In configuration.yaml add this

sensor
  - platform: command_line
    name: till_goteborg_localtime
    command: 'python3 /home/homeassistant/.homeassistant/python/vasttrafik_localtime.py'
    scan_interval: 30

Note that your path to HA may differ from mine.

You need to create an additional .py-file for sensor.mot_stationen and edit entity_id in user-defined settings in the .py-file and add corresponding sensor in HA. This will fix your problem if you can’t solve it in any other way.

This script automatically adjusts to wintertime, so you could use it permanently if you want.