Helper to keep running tally of elapsed time?

File this under useless stuff I dont really need, but still want to do anyway.

I am looking for a way to keep a running tally of elapsed time that a sensor is on/off, etc.

So for example;

  1. Sensor turns on
  2. Timer starts counting up
  3. 90 second elapse
  4. Sensor turns off
  5. Timer stops/pauses at 90 sec. Only to resume counting from 90 when

Very similar to the ‘counter’ helper, but just with elapsed time couting in sec/min/hours

I don’t kjnow of an “easy” way to do it but I would do it by having an automation that triggers every second (time_pattern) and make the condition be the entity being ‘on’. the action would be to increment a counter every time the automation runs the actions (ie. every second while the entity is on).

then you can take the value of the counter and convert that to H:M:S using a template.

I do this to keep track my boiler “on” time using the history_stats integration: History Stats - Home Assistant

Super easy to do.

2 Likes

History stats will only work for as long as you keep your recorder data (default is 10 days I think).

using a counter will keep it as long as you can continue to count up the seconds.

Perhaps you can use two entities for this.
The history stats to keep track during the day, then just before midnight you have an automation that takes the value and adds it to a helper.

If you use the yesterday example in history stats, at midnight it will update the total for the last 24 hours. Then create an automation that adds to a counter when the sensor updates.

end: "{{ now().replace(hour=0, minute=0, second=0) }}"
duration:
  hours: 24

Use the today example for the time during the current day. Finally use a template sensor to add the counter and today sensor if you want a current running total.

Maybe

@Mikefila @Hellis81

So to test I created this using the history stats. It is updating as my light remains on.
image
image

Then you guys seem to be saying at end of day, add this number, lets say its 0.8 hours, to a counter. But a counter only counts in seconds, right? I see you specify duration is 24 hours, but I don’t know how to integrate that into a counter.

So create an automation with time trigger on 23:59:59 (or perhaps slightly earlier to be safe).

As action input_number set

{{ states('input_number') + states('sensor.lamp_on_today')*60 }}

This should update the counter with the days time giving you the total in the input_number.

So the input_number will only be accurate at midnight, but at least you will get a “sensor” that can span a long time.

Then you need either a sensor or some way of formatting the seconds to hours, minutes and seconds.
This can be done with timestamp_custom().

@hellis81

Where do I put in that code? It wont let me enter it into the ‘Value’ area…

I believe you need to be in yaml mode to add templates.

I do this with bathroom fans, lights and timers.

  alias: Half Bath Timer Activation
  description: Activate the timer for the half bath
  trigger:
  - platform: device
    type: turned_on
    device_id: 80fc931335816d62657bd1e191872c9a
    entity_id: switch.half_bath_light
    domain: switch
  condition: []
  action:
  - service: timer.start
    data:
      duration: '180'
    target:
      entity_id: timer.halfbath_timer
  mode: single

This turns on fan for 90 seconds

  alias: Half Bath Fan Run
  description: Turn on Half bath fan if light was turned after after being on for
    at least 3 minutes
  trigger:
  - platform: device
    type: turned_off
    device_id: 80fc931335816d62657bd1e191872c9a
    entity_id: switch.half_bath_light
    domain: switch
  condition:
  - condition: state
    entity_id: timer.halfbath_timer
    state: idle
  action:
  - type: turn_on
    device_id: 4984c7129c2427afae9c21906c566402
    entity_id: switch.half_bath_fan
    domain: switch
  - delay:
      seconds: '{{ states(''input_number.fan_runtime'')| int }}'
  - type: turn_off
    device_id: 4984c7129c2427afae9c21906c566402
    entity_id: switch.half_bath_fan
    domain: switch
  mode: single

I set duration through Lovelace.

I don’t see how this is related to the question.

1 Like

it seems that all of this is harder than the original suggestion I made above… :man_shrugging:

But with a counter you need an automation that runs every second or minute or whatever level of resolution you want. Or have I missed something?

yes, but that’s not really a big deal. there are lots of things that run in HA in the background at sub-second intervals (ms even) with no issue.

I already have an automation that runs every second that updates a sensor for these exact situations and it’s been running for months with no issues.

then I can use the sensor changing as a trigger in other automations. That way I only need one “one second update” automation.

There is a service call counter.configure that let’s you set the counter’s current value. On sensor update you could add to the current count then set that value.

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.