Counting on/off cycles for water pump

Seems it works more mystique than before:)
History - water softener rinse from 3 till 4 am - 9 times, and 3 time from 8 till 9 am

Stats

Is this the correct sensor in your history stats?

It’s named Hacoc in your graph.

Correct, it’s just edited in dashboard

Just in case - this is my history stats sensor

sensor:
  - platform: history_stats
    name: Pump ON today
    entity_id: binary_sensor.nasos
    unique_id: pumpon.nasos
    state: "on"
    type: count
    end: "{{ now().replace(minute=0,second=0,microsecond=0) }}"
    duration:
      hours: 1

Looking at the open issues for the History Stats integration there are a couple that almost fit what you are seeing: Issues · home-assistant/core · GitHub

You should open an new issue about the count being incorrect. You can drag and drop the images you used here into your report.

@tom_l @mekaneck Guys, i’m stuck:) i left both solutions - history stats and template method.
But statistics graph is wrong again.

Here’s history from binary_sensor.nasos


From 2am till 3am - 1 time “on”
From 3am till 4am - 11 times “on” (water softener regeneration).

Now stats graph


From 2am till 3am - 3 times “on” both
From 3am till 4am - 11 times “on” both
From 4am till 5am - 11 times “on” both

How it happens ?

I apologize, the state_class of the template sensor should be total_increasing instead of measurement.

Show us the history plot (not the statistics plot) of both sensors (history_stats and template). I suspect it looks like what you wanted/expected.

The statistics are calculated from the max value for the period e.g. between 1pm-2pm, and the template and history_stats sensors both reset their values at e.g. 0.01 seconds after 1pm, which unfortunately is late enough to be considered in the new hour.

I think the state_class might fix the problem by itself because HA just considers going back to zero as a “meter reset” and so it shouldn’t actually affect any of the statistics (I think). You could also eliminate the hourly resets entirely (in addition to fixing the state_class) and the statistics plot should still show you the hourly change.

Wow, thanks a lot! Now it looks exactly how it should be.
History

Graph

I think i should open issue on Github as @tom_l recommended.

You can do as you’d like, but in my opinion this would be a feature request and not a bug report. The history_stats sensor can be used with many different window configurations, and with a continuously sliding window (e.g. the previous hour) then a measurement state class makes sense. It’s only when you have a window config such as yours (non-overlapping intermittently updating) that a total_increasing state class would be desired. So the feature request would be to add the ability to specify the state class in YAML when configuring the sensor.

That being said, it may already be possible to change it using customize.yaml, but I have not attempted it.

I’m in agreement w/ @mekaneck here, history stats sensor should change to

sensor:
  - platform: history_stats
    name: Pump ON today
    entity_id: binary_sensor.nasos
    unique_id: pumpon.nasos
    state: "on"
    type: count
    start: "{{ now().replace(minute=0,second=0,microsecond=0) }}"
    end: "{{ now() }}"

This would capture the current hour, which should make total_increasing make more sense.

Then in configuration.yaml

homeassistant:
  customize:
    sensor.pump_on_today:
      state_class: total_increasing