Adding daily run distances

Hi,
I want to calculate how many kms I have left to reach my weekly running goal.

I have created a couple of input.number sensors. One for the weekly goal and another for what distance I ran today. In my config I have a two sensors to do the calculations.

image

   - platform: template
     sensors:
       kms_run_this_week:
         friendly_name: Kilometers So Far
         value_template: "{{ states('input_number.kms_so_far') | float + states('input_number.today_s_kms') | float }}"
         icon_template: mdi:run
   - platform: template
     sensors:
       kms_left:
         friendly_name: Kilometers Left
         value_template: "{{ states('input_number.weekly_run_goal') | float - states('sensor.kms_run_this_week') | float }}"
         icon_template: mdi:run

The issue I have is I would like the calculation to add input_number.today_s_kms to kms_run_this_week then zero out so I can add another run later in the day or the next day.

I’m totally new to this so some help would be appreciated

I’m not sure if you could do this with just a template, I suspect you might have to use a script or automation so that you can actually add the value of today_s_kms to kms_so_far. The template you are using only does the calculation for display purposes, it doesn’t actually write to kms_so_far.

You could possibly make an automation that is triggered by a state change on the today_s_kms entity, but if it were me, I would be creating a script that is triggered by pressing a button on your dashboard. The below is tested and working for me:

alias: Add KMS
sequence:
  - service: input_number.set_value
    data:
      value: >-
        {{ states('input_number.kms_so_far') | float +
        states('input_number.today_s_kms') | float }}
    target:
      entity_id: input_number.kms_so_far
  - service: input_number.set_value
    data:
      value: 0
    target:
      entity_id: input_number.today_s_kms
mode: single

EDIT: Also, if you use a Garmin watch to track your runs, you might consider using the Garmin Connect custom integration which might simplify this process for you and provide all the entities you need by default.

Thank you! Works a treat for me.

I use the Strava integration. My next thing I would like to do in HA is to be able to add up all of my runs completed this week automatically but it seems quite complex because of the way the date is pulled in with the title.

Thanks for your help.

:+1: :running_man:

image

1 Like

So i now have Strava in HA calculating my runs for me based on an input_number of how many runs I have done in a week:

   - platform : template
     sensors:
       kms_this_week:
         friendly_name: Kms This Week
         icon_template: mdi:trophy
        #  unit_of_measurement: kms
         value_template: >-
              {% if is_state('input_number.runs_this_week', '1.0') %}
              {{ states('sensor.strava_0_3') }}
              {% elif is_state('input_number.runs_this_week', '2.0') %}
              {{ states('sensor.strava_0_3') | float + states('sensor.strava_1_3') | float }}
              {% elif is_state('input_number.runs_this_week', '3.0') %}
              {{ states('sensor.strava_0_3') | float | round(2) + states('sensor.strava_1_3') | float| round(2) + states('sensor.strava_2_3') | float | round(2) }}
              {% elif is_state('input_number.runs_this_week', '4.0') %}
              {{ states('sensor.strava_0_3') | float | round(2) + states('sensor.strava_1_3') | float| round(2) + states('sensor.strava_2_3') | float | round(2) + states('sensor.strava_3_3') | float | round(2) }}
              {% elif is_state('input_number.runs_this_week', '5.0') %}
              {{ states('sensor.strava_0_3') | float | round(2) + states('sensor.strava_1_3') | float| round(2) + states('sensor.strava_2_3') | float | round(2) + states('sensor.strava_3_3') | float | round(2) + states('sensor.strava_4_3') | float | round(2) }}
              {% elif is_state('input_number.runs_this_week', '6.0') %}
              {{ states('sensor.strava_0_3') | float | round(2) + states('sensor.strava_1_3') | float| round(2) + states('sensor.strava_2_3') | float | round(2) + states('sensor.strava_3_3') | float | round(2) + states('sensor.strava_4_3') | float | round(2) + states('sensor.strava_5_3') | float | round(2) }}
              {% elif is_state('input_number.runs_this_week', '7.0') %}
              {{ states('sensor.strava_0_3') | float | round(2) + states('sensor.strava_1_3') | float| round(2) + states('sensor.strava_2_3') | float | round(2) + states('sensor.strava_3_3') | float | round(2) + states('sensor.strava_4_3') | float | round(2) + states('sensor.strava_5_3') | float | round(2) + states('sensor.strava_6_3') | float | round(2) }}
              {% elif is_state('input_number.runs_this_week', '8.0') %}
              {{ states('sensor.strava_0_3') | float | round(2) + states('sensor.strava_1_3') | float| round(2) + states('sensor.strava_2_3') | float | round(2) + states('sensor.strava_3_3') | float | round(2) + states('sensor.strava_4_3') | float | round(2) + states('sensor.strava_5_3') | float | round(2) + states('sensor.strava_6_3') | float | round(2) + states('sensor.strava_7_3') | float | round(2) }}
              {% elif is_state('input_number.runs_this_week', '9.0') %}
              {{ states('sensor.strava_0_3') | float | round(2) + states('sensor.strava_1_3') | float| round(2) + states('sensor.strava_2_3') | float | round(2) + states('sensor.strava_3_3') | float | round(2) + states('sensor.strava_4_3') | float | round(2) + states('sensor.strava_5_3') | float | round(2) + states('sensor.strava_6_3') | float | round(2) + states('sensor.strava_7_3') | float | round(2) + states('sensor.strava_8_3') | float | round(2) }}
              {% elif is_state('input_number.runs_this_week', '10.0') %}
              {{ states('sensor.strava_0_3') | float | round(2) + states('sensor.strava_1_3') | float| round(2) + states('sensor.strava_2_3') | float | round(2) + states('sensor.strava_3_3') | float | round(2) + states('sensor.strava_4_3') | float | round(2) + states('sensor.strava_5_3') | float | round(2) + states('sensor.strava_6_3') | float | round(2) + states('sensor.strava_7_3') | float | round(2) + states('sensor.strava_8_3') | float | round(2) + states('sensor.strava_9_3') | float | round(2) }}
              {% else %}
              0
              {% endif %}

Only issue I have is if I run over 10 activities (like I did last week) it can’t be added in because the Strava custom integration only allows for 10 activities.

I need to somehow store the 10th activity distance, sensor.strava_9_3, then if the input_number.runs_this_week hits 11 it adds in this distance

Hm, sorry I’ve not used the Strava integration myself.

Perhaps you could use an automation that is triggered on the state change of input_number.runs_this_week and adds the distance of the latest run to kms_this_week?

Also, you should be able to simplify the above (I think, I haven’t tested, test this in the template editor before editing your sensor) by simply writing:

{{ (states('sensor.strava_0_3') | float(0) + states('sensor.strava_1_3') | float(0) + states('sensor.strava_2_3') | float(0) + states('sensor.strava_3_3') | float(0)+ states('sensor.strava_4_3') | float(0) + states('sensor.strava_5_3') | float(0) + states('sensor.strava_6_3') | float(0) + states('sensor.strava_7_3') | float(0) + states('sensor.strava_8_3') | float(0) + states('sensor.strava_9_3') | float(0))|round(2) }}
              

The zero in brackets after float says “if this entity is unavailable, output this value instead”. It also encapsulates the whole thing in brackets so you only need to call the round(2) function once, this will make it faster and possibly give more accurate results (because you’re adding the true value, not a rounded value). See here: Template Designer Documentation — Jinja Documentation (3.2.x) (palletsprojects.com)