I have been having a play around to see what kind of information can be gathered from the strava api for us that cycle.
This is far from what i want to be able to do with the API, but i see this as a start.
If anyone else is interested in helping me please drop me a pm. I would love to make a component that we can easily integrate into HAS.
Get yourself to
https://www.strava.com/settings/api
The access token retrieved via settings ->
My API Application this has the default permission, which means we can only read public activities.
I am working on writing some simple scripts to delete and upload new activities, which requires an access token with write permission.
This guide details how to quickly get a Strava access token with write access.
Log into the strava website
https://www.strava.com/settings/api
Create an application “My Api Application”
Make sure that you set the callback URL a localhost.
We will need to to obtain an authorization token.
Take note of your client ID, these will be needed later
Leave strava tab open
Create a request URL for Strava authorization, where the base URL is https://www.strava.com/oauth/authorize and parameters are:
client_id: your application’s ID, obtained during registration
redirect_uri: URL to which the user will be redirected with the authorization code.
response_type: must be ‘code’
scope: ‘public’, ‘write’, ‘view_private’, ‘view_private,write’
Example
“http://www.strava.com/oauth/authorize?client_id=”CLIENT_ID_FROM_API_SETTINGS”&response_type=code&redirect_uri=http://localhost/exchange_token&approval_prompt=force&scope=write”
Copy the above URL with your “Client_id” into a browser.
Browser will 404 as
http://localhost/exchange_token
Does not exist.
Authorize the app on Strava.
Copy the authorization URL from previous step and take note of the code.
Example
“http://localhost/exchange_token?state=&code=43534534534543gfgfdgdfgdfgd43563”
Authentication
Use a rest client. Perform a POST to https://www.strava.com/oauth/token as documented in the API.
client_id: your application’s ID, obtained during registration
client_secret: your application’s secret, obtained during registration
code: authorization code from last step
Example
$ curl -X POST https://www.strava.com/oauth/token1
-F client_id=”CLIENT_ID_FROM_API_SETTINGS
-F client_secret=”CLIENT_SECRET_FROM_API_SETTINGS
-F code=“CODE_FROM_PREVIOUS_STEP”
Retrieving Data from API Endpoints
Strava offers many endpoints, i have only played around with a couple at the moment.
/v3/oauth
/v3/athlete
/v3/athletes/:id
/v3/activities/:id
/v3/activities/:id/streams
/v3/clubs/:id
/v3/segments/:id
/v3/segments/:id/leaderboard
/v3/segments/:id/all_efforts
/v3/uploads
Example using v3/activities
Using a rest client perform a GET with the following
https://www.strava.com/api/v3/activities?access_token=“YOUR_API_KEY
Information returned from the API
Any of the fields can be used and brought into the front end in HomeAssistant using the rest platform.
It is worth referring to the documentation when picking up fields in Home assistant. https://strava.github.io/api/v3/activities/
As an example elevation_gain is reported in meters, but on my mobile app it is reported in ft. It bugged me the wrong measurement being reported in Home Assistant on the front end.
total_elevation_gain: float
meters
Using a value template you can convert metres to feet and vice versa.
value_template: '{{ value_json[0].elev_high | multiply(3.280839895) | round(1) }}'
1 Meter = 3.28084 feet
Home Assistant Configuration
Sensor.yaml sample
Picking up the elev_high parameter from the api which is reported in metres, but converted to FT
– platform: rest
name: strava_elev_high
resource: https://www.strava.com/api/v3/activities
method: GET
headers:
Authorization:YOUR_API_TOKEN
value_template: ‘{{ value_json[0].elev_high | multiply(3.280839895) | round(1) }}’
unit_of_measurement: ft
scan_interval: 300
Group Created on Information from API
Automation based on sensor
– alias: Notify Last Ride Mileage
trigger:
– platform: state
entity_id: sensor.strava_last_ride_milage
condition:
condition: template
value_template: “{{ states(‘sensor.strava_last_ride_mileage) != ‘unknown’ }}”
action:
– service: notify.notifier_telegram
data_template:
message: Ride uploaded to strava
i hope to add more sensors so that when a message is sent to a notifier we can have a summary of information such as heart rate, average, speed etc. These sensors can then be used with tts script.
Very quick write up so apologies for any mistakes