How to make sensor to record time spend over limit?

I want to track how much current has been over specified limit. Result in minutes.
Right now i have set binary sensor to turn on when over limit, then i have history_stats to show ON time, but it does not work like i was hoping.
I also set this history sensor to total_increasing but value is changing lower and higher.
As i would like sensor to only add time, not remove.
How should this be done?

If you have a base sensor that is ‘1’ when on and ‘0’ when off, then an integral helper of that sensor will be the lifetime amount of time that that sensor has been on.

Sounds like maybe that is what you want?

History stats can also do this but you need to set it up like the following:

  • Need to make sure it’s a resetting interval, not a sliding window. So e.g. you can make a sensor of the amount of time you’re over the limit daily which resets to 0 each midnight.
  • You need to force override the state_class with customize to total_increasing.
  • The lifetime ‘over limit’ count will only be visible as a long term statistic.
2 Likes

Sounds very similar to tracking how much time a force hot air furnace filter has been used.

Essentially I use the reimann sum integration and a utility meter. This example calculates in hours.

homeassistant:
  customize:
    sensor.um_furnace_filter_air_usage:
      unit_of_measurement: hours
input_number:
  um_furnace_filter_filter_life:
    min: 0
    max: 50000
    step: 1
    unit_of_measurement: hours
    mode: box
input_button:
  um_furnace_filter_filter_reset:
    name: um_furnace_filter_filter_reset
template:
  - trigger:
      - platform: state
        entity_id: input_button.um_furnace_filter_filter_reset
    sensor:
      - name: "um_furnace_filter_filter_replaced"
        state: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }} "
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    sensor:
      - name: um_furnace_filter_per_hour
        unit_of_measurement: hours
        state: >-
          {{ iif(states("sensor.ac_power")|int(0)>0 or states("sensor.furnace_power")|int(0),1,0) }}
        attributes:
          triggered_at: "{{ now().hour }}"
  - sensor:
      - name: um_furnace_filter_filter_life
        unit_of_measurement: "%"
        state: "{{ (((1.0 - ( states('sensor.um_furnace_filter_air_running')|float(0) / states('input_number.um_furnace_filter_filter_life')|float(0) )))*100.0)|round(1) }}"
sensor:
  - platform: integration
    source: sensor.um_furnace_filter_per_hour
    name: um_furnace_filter_air_usage
    unit_time: h
    method: left
    round: 2
utility_meter:
  um_furnace_filter_air_running:
    source: sensor.um_furnace_filter_air_usage
recorder:
  include:
    entities:
      - sensor.um_furnace_filter_air_running
      - sensor.um_furnace_filter_air_usage
      - sensor.um_furnace_filter_per_hour
      - input_number.um_furnace_filter_filter_life
      - input_button.um_furnace_filter_filter_reset
      - sensor.um_furnace_filter_filter_life
      - sensor.um_furnace_filter_filter_replaced
      - binary_sensor.al_um_furnace_filter_filter_replace_alert
      - alert.al_um_furnace_filter_filter_replace
      - input_boolean.al_um_furnace_filter_filter_replace_alert_enable
input_boolean:
  al_um_furnace_filter_filter_replace_alert_enable:
    name: um_furnace_filter_filter_replace alert enable
binary_sensor:
  - platform: template
    sensors:
      al_um_furnace_filter_filter_replace_alert:
        value_template: '{{ (states("sensor.um_furnace_filter_filter_life")|float(1) <= 0.0) and is_state("input_boolean.al_um_furnace_filter_filter_replace_alert_enable", "on") }}'
alert:
  al_um_furnace_filter_filter_replace:
    name: um_furnace_filter_filter_replace
    message: 'ALERT {{state_attr("zone.home","friendly_name")}} um_furnace_filter_filter_replace  '
    done_message: 'Cleared {{state_attr("zone.home","friendly_name")}} um_furnace_filter_filter_replace  '
    entity_id: binary_sensor.al_um_furnace_filter_filter_replace_alert
    state: "on"
    repeat:
      - 60
      - 1440
    can_acknowledge: true
    skip_first: false

And then the automation to reset the utility meter when a button is pressed.

- id: reset um_furnace_filter filter
  alias: reset um_furnace_filter filter
  description: ""
  mode: single
  trigger:
    - platform: state
      entity_id: input_button.um_furnace_filter_filter_reset
  action:
    - service: utility_meter.calibrate
      data:
        value: "0"
      target:
        entity_id: sensor.um_furnace_filter_air_running
        

I have my history_stats sensor customized to total_increasing, now sensor looks like this

  - platform: history_stats
    name: "L2 yli 25A"
    state: "on"
    entity_id: binary_sensor.l2_yli_25a
    unique_id: unique_l2_yli_25a
    start: "{{ today_at() }}"
    end: "{{ now() }}"

I had start {{ now() - timedelta(days=1) }} now changed to {{ today_at() }} lets see if that helps.
For that integral helper, it cant use binary_sensor, only sensor, so to use that i should create sensor instead of binary_sensor, that has states 1 and 0 ?

Yeah that should be better.

Yes, correct.