Helper to keep running tally of elapsed time?

So I did a node-red solution that integrates with a input_number helper. (@Hellis81 thats the other post you have been responding too)

I do everything else in node red, so I wanted to keep this in there too. I didnt think it was possible, which is why I originally made the post. But then found there is a way.

FWIW, I use a History Stats sensor to compute daily run time (for heating, cooling, etc) combined with the Utility Meter integration to keep a running tally of daily/weekly/monthly/yearly data.

The Utility Meter integration records its data in long-term storage (statistics table) and is unaffected by the recorder’s purge cycles.

1 Like

Hi, please could you tell me how to create a status counter when an entity is in the “on” state and that is not cut by the recorder? I haven’t found a solution yet.

Are you looking for how many times it has been on or how long it has been on?

How long has it been on

Ah! That topic seems to be a complicated one for HA!
I’ve just spent the last 6 hours trying to wrap my head around a simple elapsed timer in HA for my car charger. It doesn’t exist.
I found a Stopwatch with start/stop/resume, lap and reset solution which uses 4 input_booleans + 2 template triggers + 3 automations, 1 of which runs every second. I did not like that approach either…too much work plus I do not like the idea of an automation firing once every second. There is this solution using input_datetime helpers but you need automation(s) to update the datetime helpers. I moved from HomeSeer over a year ago and it has had timers for many, MANY years where you can create timers which can count UPWARDS unlike HA timers which only count DOWN from a DURATION.
So I had a look over in Node-RED and started searching there. Also somewhat void but I did mange to find a node that I have made work: the interval-length node…

And this is the sensor it created in HA:

If you’re not into Node-RED then datetime_helpers + templates + automations seems to be the only solution.

Hope that helps!

Unfortunately you didn’t help me much, I really don’t know how to create this system. Thank you anyway

Ok. Did you look at Simple stopwatch?&/or Stopwatch with start/stop/resume, lap and reset?

You need two input_datetime helpers, one to store the start time and one for the stop time. Then an automation that fires when the device turns on which updates the start time helper and another automation that fires when the device turns off which updates the stop time. I combined the two automations into one with two triggers and a choose section. Last item is a template sensor whose state is ( end_time minus start_time ) if the timer is stopped, or ( now() - start_time ) if the timer is running. I threw the following together this evening. In the code below I’m using an input_boolean that I created to trigger & test it. Just replace the input_boolean with the entity_id of the switch you want to track.

templates:
  - sensor:

    - name: 32A Charging Timer
      unique_id: 32a_charging_timer
      state: >
        {% if is_state("input_boolean.32a_charging_on_off", "on") %}
          {{ as_timestamp(now()) - state_attr("input_datetime.32a_charging_start", "timestamp") }}
        {% else %}
          {{ state_attr("input_datetime.32a_charging_end","timestamp") - state_attr("input_datetime.32a_charging_start","timestamp") }}
        {% endif %}
      device_class: duration
      state_class: measurement
      unit_of_measurement: s

Automation:

alias: 32A Charging Timer
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.32a_charging_on_off
    id: Start
    to: "on"
  - platform: state
    entity_id:
      - input_boolean.32a_charging_on_off
    id: End
    to: "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Start
        sequence:
          - service: input_datetime.set_datetime
            data:
              timestamp: "{{ as_timestamp(now()) }}"
            target:
              entity_id: input_datetime.32a_charging_start
      - conditions:
          - condition: trigger
            id: End
        sequence:
          - service: input_datetime.set_datetime
            data:
              timestamp: "{{ as_timestamp(now()) }}"
            target:
              entity_id: input_datetime.32a_charging_end
mode: single

3 helpers:

image

And my input_boolean:

image

HA only updates the templated sensor once every minute but when stopped it’s to the second.
The result:

image

image

image

image

image

I posted a solution a year ago (see above). Use a History Stats sensor and the Utility integration.

I’ve used the combination for well over a year and have daily, weekly, monthly, and yearly stats for how long the furnace has been heating and the AC has been cooling.

Great! thanks!

I tried to create it, and it returns me this value:

How do I get an HH:MM result?

I believe your system lasts 1 year at most I would like a life timer

Trying it, unfortunately, the sensor is not working every time the entity changes state from “INATTIVA” to “IN FUNZIONE” it resets and starts again with zero value.

Sensor

    - name: "Contatore Riscaldamento Acceso"
      unique_id: "contatore_riscaldamento_acceso"
      state: >
        {% if is_state("sensor.stato_riscaldamento", "IN FUNZIONE") %}
          {{ as_timestamp(now()) - state_attr("input_datetime.contatore_riscaldamento_start", "timestamp") }}
        {% else %}
          {{ state_attr("input_datetime.contatore_riscaldamento_stop","timestamp") - state_attr("input_datetime.contatore_riscaldamento_start","timestamp") | round(0) }}
        {% endif %}
      device_class: duration
      state_class: measurement
      unit_of_measurement: s

Automation

- id: '1670081715498'
  alias: Contatore Riscaldamento Acceso
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.stato_riscaldamento
    id: Start
    to: IN FUNZIONE
  - platform: state
    entity_id:
    - sensor.stato_riscaldamento
    id: Stop
    to: INATTIVA
  condition: []
  action:
  - choose:
    - conditions:
      - condition: trigger
        id: Start
      sequence:
      - service: input_datetime.set_datetime
        data:
          timestamp: '{{ as_timestamp(now()) }}'
        target:
          entity_id: input_datetime.contatore_riscaldamento_start
    - conditions:
      - condition: trigger
        id: Stop
      sequence:
      - service: input_datetime.set_datetime
        data:
          timestamp: '{{ as_timestamp(now()) }}'
        target:
          entity_id: input_datetime.contatore_riscaldamento_stop
  mode: single

I started recording long-term statistics for a fan in October 2021. Here’s its yearly Utility Meter sensor data. You can see it has data for 2022 and 2021.

Ok but as you can see from the graph in January 2022 it restarted from 0

That’s simply due to how I chose to graph the data.

Here’s the cumulative value.

The point is that you claimed it “lasts 1 year at most” but it clearly records more than a year’s worth of data and you can choose how to display it.

Is this correct?

start: "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}"
end: "{{ now() }}"

I would like to start at 00:00 and finish at 23:59
How did you configure this daily History Stats sensor?
Thanks

Example

Thanks, you are very kind!
I think I did everything right…

One last doubt, graphically after a year, does this Utility Meter Yearly sensor start from scratch or does it show the sum with the previous year?

If you set the sensor’s cycle to yearly it will display the total of the current year’s data (even though data is recorded for all years and can be graphed as I demonstrated in my previous posts).

According to the documentation, cycle is optional so you can try creating a Utility Meter sensor without the cycle option and, in theory, it should simply display a running total indefinitely. I say “in theory” because I have never tried it (because I don’t need to display a running total that spans multiple years).

The cycle option is just a way to tell the sensor the time span it should use to calculate its result. It gets the total of every day from the History Stats sensor and uses that to compute a total for day/week/month/year based on what’s specified for cycle.

1 Like

With | timestamp_custom("%H:%M:%S", 0)

  - name: 32A Charging Timer
    unique_id: 32a_charging_timer
    state: '{{ iif(is_state("input_boolean.32a_charging_on_off","on"), as_timestamp(now())-state_attr("input_datetime.32a_charging_start","timestamp"), state_attr("input_datetime.32a_charging_end","timestamp")-state_attr("input_datetime.32a_charging_start","timestamp")) }}'
    device_class: duration
    state_class: measurement
    unit_of_measurement: s
    attributes:
      formatted_time: '{{ states("sensor.32a_charging_timer")|float(0) | timestamp_custom("%H:%M:%S", 0) | regex_replace(find="^[0:]*",replace="") }}'