Binary sensor state time accumulator / integration statistics

I have a pump which is a switch so I can turn on and off the pump.
I’d like to get info about the pump run time, I’d like to have info on the time it run in last 24hrs, last month, and time from ever since.
I’d bumped around the statistics, the history statistics, and integration sensor among other but none seems to fit my needs.
I’d like not to use history itself and do a history query, I expect the last values to be stored there, e.g. If the pump run for 20 hours last month and I restart home assistant this counter should be restored to 20.

Is there an integration that would solve this? What are some options to approach this?

State history is limited by the value in purge_keep_days in the Recorder component, so that would need to have a very large value for this to work unfortunately. The potential problem then would be database size from the sheer number of days of history recorded. 30 days might be doable depending on the number of domains/entities you have recorded. Anything more than that might not be practical.

The history in your home assistant database won’t be deleted when you restart home assistant. If you would like to have some nice graphs, you could use something like Grafana and connect it to your database.
Check out this thread, this guy did something similar to your needs.

In case you don’t want to use the history, you can write the ‘on’ time to a file everyday at 23:59:59 and then calculate based on the entries in this file. I would suggest to do this in AppDaemon, as I’m not sure if this will be doable in home assistant. If you go the AppDaemon route I can provide you some help/guidance to achieve this.

I actually do this using the variable custom component. OP’s use case sounds more complicated than mine though, which is why I didn’t suggest it.

Why history _stat doesn’t fit your needs? I’m trying it and seem to do the work…
SmartSelect_20190917-014419_Chrome

Isn’t this one dependent on how long data is kept on the database?

Thanks for all the reply’s unfortunately I’m busy but I’m gonna go through and report back

May be I’m asking several things on the same post which makes my scenario complicated.

The lifetime counter I believe could be made with a template sensor and have an automation on the observed variable to add the time when the variable changes state. This should work. But I was looking something already setup.

I made some test with another component, it should easily work:

  1. i measure the sensor runtime (pump, water valve etc., in this test i used a lamp off time) using history_stats:
  - platform: history_stats
    name: RGBW_1 OFF TIME
    entity_id: light.RGBW_1
    state: 'off'
    type: time
    start: '{{ 0 }}'
    end: '{{ now() }}'    
  1. then i use an utility_meter to store lifetime that data
utility_meter:
  dailyRGB_1_off_time:
    source: sensor.rgbw_1_off_time
    cycle: daily

The output is sensor.dailyrgb_1_off_time
I made some test changing original data source (sensor.rgbw_1_off_time) to 0 and the output of the utility meter still remain to counted value, because this component count only in incremental way.

Please check if this solution works for your purpose and let me know.

Hi all, i would like to track time/day, time/week, time/month, time/year of a valve state on. As example above i used history_stats to count total time (in that example that was a test, i used a lamp state), then utility_meter to track daily/weekly/montly/yearly data. Problem is that utility_meter doesn’t manage time, so the result is that it count in normal number.
There is a way to use utility_meter to count time (HH:mm) instead of kWh, m3 etc.?

Solved, i disn’t see that history_stats already count in decimal hours (not HH.mm), so using history_stats then utility_meter i have time count in decimal hours everywhere. This is ok for me.

From a newbe! This seams useable also for me! Easy to understand! I like to know how many seconds per 1H a pump has been pumping and also use it to alert me if not been active for more then an hour. But where is this to be modified and pasted into the platform to work also for me? Is it a script or an automation? Excuse a new to the platform!