Strava sensors

So, I’m struggling to get this to work. I’ve taken one of the working examples above, added it under my sensor domain, and set the access token to be "Bearer " where is the value in “your access token” in the strava API.

I think what’s going on is there’s some setup steps to get this to work. They’re described under the developers page but seem to assume an application that is coded to specifically integrate with strava (with redirection etc). Can anyone whose gotten this to work speak to what I may be missing?

Thanks!

Hi, it looks like Strava has changed the key management.

It might be possible that the apps created after the update (around 15 Oct 2018) need to perform more tasks in order to access the data.
The key are also expiring only after a few hours and there is another key used to refresh the fist one.

I wanted to try this also yesterday and found out that nothing works as described in the topic before.

Is there anything new on this. I didn’t try it yet but I also want to set this up.

Regarding to your post it seems like this doesn’t work any more.

Can someone clear that up for me? Don’t want to spend time setting this up when it’s already known that it won’t work,

Unfortunately, the sensor does not work anymore due to changes how we need to authenticate against the Strava API.

Hi all,

I implemented a new platform for Strava sensors which fixes this token issue:
https://github.com/home-assistant/home-assistant/pull/22151

It is currently work in progress. Please feel free to test it. Some feedback is appreciated :slight_smile:
It is based on the PyPi package “stravalib”.

I am getting an error trying to Link Strava Account. I see the correct client_id in the URL. Don’t see reference to the client_secret.

{"message":"Bad Request","errors":[{"resource":"Application","field":"redirect_uri","code":"invalid"}]}

Hi @popboxgun,

You will need to change the “Auhtorization Callback Domain” setting for your App at the Strava Developer website to the IP address or hostname of your HA instance:

1 Like

Thanks, the problem appeared to be that the redirection is getting the ip 10.72.10.6 from somewhere as the callback domain. Not sure where that IP came from, it’s not on my local network.

This error was solved, I keep for other’s reference.


Hi

not sure if I’m doing well.

I created custom_components/strava/init.py and sensor.py files from your repo (just those two). Also, added this into configuration.yaml:

strava:
client_id: myaccesstoken
client_secret: myclientsecret

Finally I added some sensors, and configured My Applitcation API at Strava Developer website as you indicated above. I got at the site an Access Token and ClientSecret, and I set them in my config as indicated (not sure if they are OK…¿?)

I still get "
{“message”:“Bad Request”,“errors”:[{“resource”:“Application”,“field”:“redirect_uri”,“code”:“invalid”}]}
"

I guess I’m missing something in my setup.

Any help is much appreciated!
KR

Got it- Though I got the 500, sensors were present at <> in HA :slight_smile:


changed client_ID to 12345 (as retrned in Strava Developer website). Then at HA I followed the “Link Strava Account” link and got to Authorize (great!).

Unfortunatelly, then I got an “500 Internal Server Error. Server got itself in trouble” message.

Ummm…not sure what else I can try ?

Does this still work ?

This was working fine for me for 2 years plus but noticed thats todays ride was not showing up in HA. After checking my API I noticed that this all stopped on 15th Oct 2019 and a search on Strava’s site I found this

## Update: The migration period ended on October 15, 2019

The migration period ended on October 15, 2019 and forever tokens will no longer work. If you did not migrate you will need to reauthorize your users in order to get new tokens. When your users log in, we recommend sending them to authorize with Strava again. If you are having trouble implementing OAuth, please view this guide: https://developers.strava.com/docs/getting-started/#oauth

Looks as if we will need a new way to get to this data - with no known Add-on that I can see it will stop working until a new way is found to exchange tokens on a regular basis.

1 Like

You just need to create a new token and give it access to a specific scope i had the same error.

@stv0g: Sorry for coming here “begging” but it is still almost x-mas :slight_smile: Any plans to pick up the Strava sensor again (https://github.com/home-assistant/home-assistant/pull/22151)? I do of course understand you have a busy life. A completed component in HA would have been much appreciated. I don’t know where to start myself, but if I can be of any help let me know. Happy new year!

Just picked up my HA project on the side. Got HA with Appdeamon. I am familiar (albeit still class myself as a beginner/hobbyist) with Python. I suspect this has died because of the switch to OAuth2 which will require either programmatic login or a program to request refresh every 6 hours. I’d be keen to get this working. Anyone had another go at this?

As far as I understand this has been completely abandoned. I have recently linked strava to HA using IFTTT webhooks to send data to Home Assistant variables then use a few automation a to interpret and arrange that data nicely.

It looks great!
Just yesterday I started looking for information to integrate strava into HA, and it seems that the issue is stopped by Strava API changes. Can anyone still read the Strava data? Could you share how you did it with IFTTT?
Cheers!

Hello,
I think this can be managed with some steps in order to obtain a long-time token. I am not pretty sure but I have obtained the distance. I will monitor it some days to see if the token expires.

Perfect! I hope you explain your results as soon as possible :slight_smile:
Agur!

Well, looks like there are no “long-time” tokens any more. Strava has replaced them for short-time tokens. The good news are that there is a refresh token to reauthorize the app periodically. I am not sure if I can write an app for this but it looks feasible for someone with experience.
agur!

SCREENSHOT-2020-06-17 at 15-34-46

Hey I got Strava working, with a couple of python scripts:

  1. You need to get all necessary tokens, here is a really good video that explains how to get them
    (you need to install Postman, but curl should also work)

    https://www.youtube.com/watch?v=sgscChKfGyg&t=791s

    for the Python Script: https://www.youtube.com/watch?v=2FPNb1XECGs&t=611s

  2. make sure you have python_script: in your configuration.yaml and folder named python_scripts

  3. create a python script for each data, for example, distance of last ride is called strava_distance.py,
    with this content: (replace all three XXX with your data)

import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

auth_url = "https://www.strava.com/oauth/token"
activites_url = "https://www.strava.com/api/v3/athlete/activities"

payload = {
    'client_id': "XXX",
    'client_secret': 'XXX',
    'refresh_token': 'XXX',
    'grant_type': "refresh_token",
    'f': 'json'
}

# print("Requesting Token...\n")
res = requests.post(auth_url, data=payload, verify=False)
access_token = res.json()['access_token']
# print("Access Token = {}\n".format(access_token))

header = {'Authorization': 'Bearer ' + access_token}
param = {'per_page': 200, 'page': 1}
my_dataset = requests.get(activites_url, headers=header, params=param).json()

print(my_dataset[0]["distance"])
  1. add this sensor to your yaml:
  - platform: command_line
    name: Strava Distanz
    command: "python3 /config/python_scripts/strava_distance.py"
    command_timeout: 60
    scan_interval: 180

that’s it! you need to restart Home Assistant but all sensors should work, tested it now for a couple of weeks and it works without problems or API errors


here some useful template sensors:

distance in kilometers:

  - platform: template
    sensors:
     strava_distanz_km:
      friendly_name: "Strava Distanz Km"
      value_template: >
        {{ (states('sensor.strava_distanz') |int  / 1000) | round(2) }}

average speed in km/h:

  - platform: command_line
    name: Strava Average Speed
    command: "python3 /config/python_scripts/strava_average_speed.py"
    value_template: '{{ (value | multiply(3600) / 1000) | round(2) }}'
    unit_of_measurement: km/h
    command_timeout: 60
    scan_interval: 180

duration time in HH:MM:SS:

  - platform: template
    sensors:
      strava_duration_insgesamt_hh_mm_ss:
        friendly_name: Strecke Insgesamt
        icon_template: mdi:bike
        entity_id: sensor.time
        value_template: >-
          {% set t = states('sensor.strava_elapsed_time') | int %}
          {{ '{:02d}:{:02d}:{:02d}'.format((t // 3600) % 24, (t % 3600) // 60, (t % 3600) % 60) }}

arrive time in HH:MM:SS:

  - platform: template
    sensors:
      strava_ankunft_uhrzeit:
        friendly_name: Startzeit
        icon_template: mdi:timer-off-outline
        entity_id: sensor.time
        value_template: >-
          {% set minutes = states('sensor.strava_elapsed_time') | int %}
          {{ (as_timestamp(strptime(states.sensor.strava_start_time.state, "%Y-%m-%dT%XZ")) + (minutes)) | timestamp_custom('%H:%M:%S') }}
2 Likes