History_stats: total runtime of entity unplausible

Hi @ all :slight_smile:

I have a drainage pit with a pump running to get rid of excess water. There is a hydrostatic sensor measuring the water content in the pit and a Shelly that is controlled by Home Assistant to start the pump if the water level is above 50%.

I monitor the runtime using a binary sensor that reacts to the output power of the Shelly (consider running if power output is > 20W:

template:
  - binary_sensor:
      - name: "heESP24: Drainage Pump Status"
        unique_id: 883fe33e-7b55-4a35-a2e8-8759ec277e88
        device_class: running

        state: >
          {{ states('sensor.heesp24_power_realtime')|float > 20 }}
        icon: >
          {% if states('sensor.heesp24_power_realtime')|float > 20 %}
            mdi:pump
          {% else %}
            mdi:pump-off
          {% endif %}

Now with the history_stats component I’d like to monitor the “runtime” of the pump: daily, weekly, monthly and TOTAL.

sensor:
  - platform: history_stats
    name: "heESP24: Drainage Pump Runtime This Day Decimal"
    entity_id: binary_sensor.heesp24_drainage_pump_status
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}"
    end: "{{ now() }}"
  - platform: history_stats
    name: "heESP24: Drainage Pump Runtime This Week Decimal"
    entity_id: binary_sensor.heesp24_drainage_pump_status
    state: "on"
    type: time
    #Ref: https://www.home-assistant.io/integrations/history_stats/
    start: "{{ as_timestamp( now().replace(hour=0, minute=0, second=0, microsecond=0) ) - now().weekday() * 86400 }}"
    end: "{{ now() }}"
  - platform: history_stats
    name: "heESP24: Drainage Pump Runtime This Month Decimal"
    entity_id: binary_sensor.heesp24_drainage_pump_status
    state: "on"
    type: time
    #Ref: https://www.home-assistant.io/integrations/history_stats/
    start: "{{ now().replace(day=1, hour=0, minute=0, second=0, microsecond=0) }}"
    end: "{{ now() }}"
  - platform: history_stats
    name: "heESP24: Drainage Pump Runtime Total Decimal"
    entity_id: binary_sensor.heesp24_drainage_pump_status
    state: "on"
    type: time
    #Ref: https://www.home-assistant.io/integrations/history_stats/
    start: "{{ 0 }}"
    end: "{{ now() }}"
  - platform: history_stats
    name: "heESP24: Drainage Pump Start Events"
    entity_id: binary_sensor.heesp24_drainage_pump_status
    state: "on"
    type: count
    start: "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}"
    end: "{{ now() }}"

For the most part the values appear to be valid (I’m also trying to convert them from decimal to “human readable” (weeks,days,hours,minutes) - but that is a different story :slight_smile:

The system is running for approximately 1.5 years now. Pump start events are between 0/day (summer) up to 30/day (autumn, winter), with each run pumping approximately 120 l water.

Now the Drainage Pump Runtime Total Decimal seems to be off:

473.230 h / 24 ~ 19.718 days RUNtime in 1.5 years!

The start and stop values for the sensor are from the integrations support page, so I guess they are fine?

Furthermore, in the second screen shot the “current value” is 473232:44:24 which does not look like a valid number to me.

Any ideas what I might be missing / overseeing?

Thanks in advance for any insight / input!

Cheers,

The History Stats integration relies on the History integration which relies on the Recorder integration.

In other words, History Stats uses the data stored by Recorder which, by default, is limited to 10 days (unless you increased/decreased it). Anything older than 10 days is automatically purged. Increasing the limit will, of course, increase the database’s size because it records the history of every entity in your system (unless configured otherwise).

So your “total” History Stats sensor bases its calculation on the available data in the database which is limited to the last X days (where X is the duration of the purge cycle).

What I suggest you do is create a single History Stats sensor that calculates daily runtime. Then create daily/weekly/monthly/yearly sensors with the Utility Meter integration. Sensors based on that integration store their data in a table within the database that is excluded from the periodic purge cycles.

I provided an example in the following topic:

Interesting…

I’m using an external MariaDB for storage of the recorded data. I was under the assumption that with external DB the storage would not be limited.

That’s what you would typically call “naive” as I did not verify it until now:

SELECT MIN(last_updated) FROM homeassistantdb.states WHERE entity_id LIKE "%ESP24%";

Resullt:

2023-12-27 03:12:03.190

A pitty that the cumulative runtime of the pump seems to be lost. And I still do not understand how this “fantasy runtime” was “calculated” :slight_smile:

But I’ll definitively will give your suggestion a try - sounds like a good plan.

Thanks!