How to calculate daily total number of steps

I have a sensor which contains the current number of steps in a session. This sensor produces something like the following:

I.e. this sensor is an increasing value until a walk stops. At that point it is reset to zero.

I would like to create a sensor derived from walking pad steps that would produce a history_stat that is the sum of all the steps in that day: 4151+3284+902=8337.

It seems what I need is a way to calculate the local maxima values for the current day. Does anyone know how to achieve something like this?

1 Like

You could try the total or total_increasing state classes.

Using total_increasing should give you a value that increases forever corresponding to your total steps (see explanation here). Perhaps not that helpful.

Using total would give you a value that increases throughout the day (I think?), as long as you can also set last_reset to be the start of each day (see explanation here).

Not that I’ve tried either for this type of use case, but that’s where I’d start.

1 Like

This worked! Thanks for the quick answer!

I’m trying to do the exact same. Adding up the local maxima to get a “Daily Steps” counter. But neither state_class: total nor state_class: total_increasing work for me. Both reset to zero every time the step counter drops to zero.

@Mobius7 How exactly did you solve this? Could you post your yaml?

Solved it myself by using an SQL sensor.

Under Integrations add the “SQL” Integration, then use the following settings:
Column: total_steps
Query:

SELECT
      SUM(B.state) +
      (
      SELECT A.state
      FROM
              states as A
              INNER JOIN states_meta as A_meta
              ON A.metadata_id = A_meta.metadata_id
      WHERE
              A_meta.entity_id = 'sensor.walkingpad_4'
      ORDER BY A.state_id DESC
      LIMIT 1
      ) AS total_steps
FROM
      states as A
      INNER JOIN states_meta as A_meta
      ON A.metadata_id = A_meta.metadata_id,
      states as B
      INNER JOIN states_meta as B_meta
      ON B.metadata_id = B_meta.metadata_id,
      states as C
      INNER JOIN states_meta as C_meta
      ON C.metadata_id = C_meta.metadata_id
WHERE
      A_meta.entity_id = 'sensor.walkingpad_4'
      AND A.old_state_id = B.state_id
      AND B.old_state_id = C.state_id
      AND CAST(A.state AS INTEGER) < CAST(B.state AS INTEGER)
      AND CAST(C.state AS INTEGER) < CAST(B.state AS INTEGER)
      AND strftime('%Y-%m-%d', B.last_updated_ts, 'unixepoch', 'localtime') = strftime('%Y-%m-%d', 'now', 'localtime')
ORDER BY B.state_id DESC

Replace sensor.walkingpad_4 with the name of your step sensor (in both places).

Cacan you please add your code as example. I like to do that also but i dont know how where i put that total stateclass.

i know im late but i was trying to see how others did it and came across this thread so hopefully this will help someone in the future because im sure you have figured it out by now lol but my phone gives me sensor.s25_ultra_steps_sensor then i made a packages folder in my config and put people.yaml under that. then in the people.yaml i have

homeassistant:
  name: People

utility_meter:
  s25_ultra_steps_daily:
    source: sensor.s25_ultra_steps_sensor
    cycle: daily

  s25_ultra_steps_weekly:
    source: sensor.s25_ultra_steps_sensor
    cycle: weekly

  s25_ultra_steps_monthly:
    source: sensor.s25_ultra_steps_sensor
    cycle: monthly

template:
  - sensor:
      - name: s25_ultra_step_summary
        unique_id: s25_ultra_step_summary
        state: >
          {% set today = states('sensor.s25_ultra_steps_daily') | int(0) %}
          {% set yesterday = state_attr('sensor.s25_ultra_steps_daily', 'last_period') | int(0) %}
          {% if today > yesterday %}
            You've walked {{ today - yesterday }} more steps than yesterday! 🚶‍♂️💪
          {% elif today < yesterday %}
            You're {{ yesterday - today }} steps behind yesterday. Keep going! 🏃‍♂️🔥
          {% else %}
            Same number of steps as yesterday — try to beat it! 📊
          {% endif %}
        icon: mdi:walk

make sure your config has

homeassistant:
  # Load all YAML files under /config/packages as packages
  packages: !include_dir_named packages

still messing with the card but so far its

type: vertical-stack
cards:
  - type: custom:mushroom-title-card
    title: Step Tracker
    alignment: center
  - type: grid
    columns: 2
    square: false
    cards:
      - type: custom:mushroom-entity-card
        entity: sensor.s25_ultra_steps_daily
        name: Daily Steps
        icon: mdi:shoe-print
        layout: vertical
        primary_info: state
        secondary_info: name
        icon_color: green
      - type: custom:mushroom-entity-card
        entity: sensor.s25_ultra_steps_weekly
        name: Weekly Steps
        icon: mdi:calendar-week
        layout: vertical
        primary_info: state
        secondary_info: name
        icon_color: orange
      - type: custom:mushroom-entity-card
        entity: sensor.s25_ultra_steps_monthly
        name: Monthly Steps
        icon: mdi:calendar-month
        layout: vertical
        primary_info: state
        secondary_info: name
        icon_color: purple
      - type: custom:mushroom-entity-card
        entity: sensor.s25_ultra_steps_sensor
        name: Total Steps
        icon: mdi:walk
        layout: vertical
        primary_info: state
        secondary_info: name
        icon_color: blue
  - type: custom:mushroom-template-card
    primary: "{{ states('sensor.s25_ultra_step_summary') }}"
    icon: mdi:walk
    icon_color: teal
    fill_container: true
    multiline_primary: true