API with Oauth2 and Home Assistant

This has been a real challenge for me and I just wanted to share how I made this work. Mostly so I can remember how to solve this for the next time. Ok, So what is this all about?


I wanted to get the free parking spots on my commuter parking outside Gothenburg (Called Delsjön pendelparkering) in Home Assistant


There is an api (vasttrafik) but they were using Oauth2. This made the bearer token change every 3600s and I was unable to parse the info in the home assistant sensor


With some help, Node Red and MQTT this was finally solved.


Hands on

I have used the Västtrafik API. You will have to register at https://developer.vasttrafik.se. Create an app and start a subscription. You might probably use another API and I suppose this will work the same more or less. The hard part was not about getting the api up and running but more about getting the bearer token. I will get back to this later. After setting this up you will get an:




I started to look at the api documentation at https://developer.vasttrafik.se/portal/#/api/SPP/v3/landerss and also tried the api console. Make sure your api calls work here first.

Home Assistant

After this I created a sensor in my sensors.yaml in Home Assistant. The purpose of this sensor is to generate an updated bearer. This will change every 3600 seconds. It will make a post and get the base encoded token. You will need to decode this. I did use something like this https://www.base64decode.org/ I have also used some specific parameter just for vasttrafik in the sensor header below. This might differ from what api you are using.

- platform: rest
  resource: https://api.vasttrafik.se/token
  method: POST
  name: "Parkinglotsbearer"
  value_template: '{{ value_json.access_token }}'
  payload: 'grant_type=client_credentials&scope=device_1'
    Content-Type: application/x-www-form-urlencoded

Try to add this new sensor in Home Assistant after you have verified the configuration. It should out put a new bearer token every 3600s.

The next sensor is what was casing all the problems. I tried to create a sensor like below. This sensor will work just fine in 3600 seconds. But after that there will be a new token and it will stop working. I did try changing so that the bearer was sent in the headers but using a template in headers seems to be impossible. So basically this sensor will not work. However you may want to try and add it and type the bearer token manually just to make sure it works. Note that the 156 is the name of the parking.

- platform: rest
  resource: https://api.vasttrafik.se/spp/v3/availableCapacity/156
  name: "Parkinglots"
#    Authorization: 'Bearer {{states.sensor.parkinglotsbearer}}'
#    Authorization: Bearer {{  states.sensor.parkinglotsbearer }}
#    Authorization: 'Bearer 666d9f21-d833-3444-4440-44417773a008'
     Authorization: Bearer {{ {{states('sensor.parkinglotsbearer') }}

Node Red

Ok, I was a little bit stuck here but got some help setting this up in Node Red. The flow will get the bearer token from Home Assistant, parse the token in a function, send the http get, list the free parkings and post them to mqtt. You can download the flow here.

I have used the cloudmqtt and have configured this in Home Assistant. I have a self hosted one as well. If you have that working you can add a new sensor in HA like below. This is the sensor that now will list free parkings

- platform: mqtt
state_topic: "parking/delsjon"
name: mqttparkingdelsjon

It will look like this in Home Assistant.


Ok, Everything is now up and running. However you may also now have figured out that HA will keep history of the changes. You can also add in the timestamp in Node Red to post every minute. This means you will be able to use this info in Grafana. And in theory find out at what time parking are full at specific time.

As you can see in the graph below there is a lot of free parkings in the night and they are almost full at 08:00.