How to get a daily step count from a total step count over several days

Hello everyone. In the mobile app integration I have access to my step count. However, this step count is the total number of steps since the phone was last turned on. I am trying to get the number of steps in the last 24 hours. I have an idea of how to do it, but I don’t know how to implement it.

My idea:
set a number helper which will reset at midnight every day to the current value of the total step count since the phone was last powered on.
Then subtract the total step count from the helper to get the daily steps.

I have tried to do this with automations but cant figure it out. I am sure it can be done with templates but I really don’t know how to use templates.

If anyone could help that would be great. I know I am kind of asking a lot, so if its too much then no worries.

This is using template sensors:

What this does:

Trigger - At midnight
Action - Calculate daily steps

Trigger - Two (2) seconds after midnight
Action - Set the new total steps

template:
#--------------------------------------------------------------------------------------------------
#  Create a trigger template to count steps
#--------------------------------------------------------------------------------------------------

  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    - sensor:
      - name: "Daily Steps"
        unique_id: "Daily Total Steps"
        state: {{ (states('sensor.total_steps) - states('sensor.total_steps_yesterday')) | int }}

#--------------------------------------------------------------------------------------------------
#  Create a trigger template to set yesterdays total steps
#--------------------------------------------------------------------------------------------------

  - trigger:
      - platform: time
        at: '00:00:02'
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    - sensor:
      - name: "Total Steps Yesterday"
        unique_id: "Yesterday Total Steps"
        state: {{ states('sensor.total_steps') | int }}

This is configured to go into configuration.yaml. I am not sure this will survive a reboot.

You may have to use a number helper and an automation to survive a reboot.

Amazing. Thank you so much. There were a couple of typos which I eventually figured out and I changed the daily step counter so that it updates every 5 minutes. This is what I eventually ended up with.

Also I was getting an error with the daily steps and it turned out the state of the steps was being a string and not an intiger. After a considerable amount of googling, it seems to work now.

It should be find that a restart of my phone will break it as I hardly ever turn off my phone anyway.

template:
  - trigger:
      - platform: time_pattern
        minutes: "/5"
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: "Daily Steps"
        unique_id: "Daily Total Steps"
        state: "{{ (states('sensor.sm_g781b_steps_sensor')| int - states('sensor.total_steps_yesterday')| int) | int }}"

#--------------------------------------------------------------------------------------------------
#  Create a trigger template to set yesterdays total steps
#--------------------------------------------------------------------------------------------------

  - trigger:
      - platform: time
        at: '00:00:02'
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: "Total Steps Yesterday"
        unique_id: "Yesterday Total Steps"
        state: "{{ states('sensor.sm_g781b_steps_sensor') | int }}"
1 Like

Hi thanks this is great as I have got the same issues for me and my wife, how can I achieve this for me and my wife phone? Where can I place the template

You add the code to your config.yaml file. Just make sure to change the entity names.

You can also copy the templates (only the parts between the double quotes in state: ) into developer tools → tenplates - in home assistant to make sure it gives you the correct value.

Hope this helps.

Hi Yup Many thanks, it worked now. :slight_smile: Thanks