Hey I got Strava working, with a couple of python scripts:
-
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
-
make sure you have python_script: in your configuration.yaml and folder named python_scripts
-
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"])
- 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') }}