Trying to make a runtime sensor

I am trying to make a sensor that will count how long a light is on.
I try to do this whit a counter sensor

counter:
  soverom_lampe_on_timer:
    name: Soverom lampe timer
    initial: 0
    step: 1

I want it to reset and start when the light is on, then stop when the light is off.
I made his automation, and it is resetting, but it is not counting.

alias: counter test
description: ''
trigger:
  - from: 'off'
    platform: state
    to: 'on'
    entity_id: light.zigbee_soverom
action:
  - service: counter.reset
    data: {}
    entity_id: counter.soverom_lampe_on_timer
  - repeat:
      until:
        - condition: device
          type: is_off
          device_id: 489f29b6d96b719c5518e7d49bc870a1
          entity_id: light.zigbee_soverom
          domain: light
      sequence:
        - service: counter.increment
          data: {}
          entity_id: counter.soverom_lampe_on_timer
        - wait_template: 'minutes: 1'
mode: single

Anyone know how?

Use the history_stats integration with count

Whatfor? Do you want the light to turn off after a given time? That can easily be achieved by an automation. Otherwise Burningstone’s hint on history_stats is correct.

If you would rather use your automation I think the problem is here:

sequence:
  - service: counter.increment
    data: {}
    entity_id: counter.soverom_lampe_on_timer
  - wait_template: 'minutes: 1'

that doesn’t look valid for the wait_template.

try this instead:

sequence:
  - service: counter.increment
    data: {}
    entity_id: counter.soverom_lampe_on_timer
  - delay: 
      minutes: 1

You automation increments the counter the moment the light is turned on. That means the light hasn’t yet been on for a minute but the counter already indicates 1. In other words, the counter will always report an extra minute. Is that how you want it to work?

If the extra minute is what you want then this single Template Sensor can replace your automation and counter. When light.zigbee_soverom is on it reports the number of elapsed minutes. When it’s off it reports 0.

- platform: template
  sensors:
    soverom_time:
      value_template: >-
        {{ ((now() - states.light.zigbee_soverom.last_changed).seconds // 60) + 1 if is_state('light.zigbee_soverom', 'on') else 0 }}

Thanks :slight_smile: This one works very well. But it loks like it is a little after, not by a minute, but by 0,5 +/- :expressionless:
PS: I want the first minute to be “0” because it is not 1Min yet.

I tried this but it is 1 minute to late:

      soverom_light_time:
        friendly_name: "Tid lyd soverom er på"
        value_template: >-
          {{ ((now() - states.light.zigbee_soverom.last_changed).seconds // 60) if is_state('light.zigbee_soverom', 'on') else 0 }}

Did your original automation work like that? I don’t think it did. The instant it triggered, it immediately incremented the counter. In other words, even though the light just turned on, the counter was already reporting 1 minute.

The Template Sensor I posted works like your automation: it is always 1 minute ahead of the actual elapsed time. If you remove the + 1 from the template, the reported time will be 1 minute behind the actual elapsed time.

Your choice is one or the other (ahead or behind). The only way to make it more accurate is to display the elapsed time in minutes and seconds. However, that won’t work because the Template Sensor is not updated every second but only every minute (because the use of now() is what causes the template to be evaluated every minute).

1 Like

Well, my original automation did not work as it should.
The Template Sensor looks like what I want.
Do you know how often, and when it updates? Because that is quite relevant on how the sensor should be set up.
Like if the template updates just before increase. That is probably the reason I got variable results

It will update when light.zigbee_soverom changes state and every minute (because now() changes state every minute).

I found this thread while searching for this exact solution. I’m trying to count up the total time that a given binary sensor is on. I want the time to persist through HA restarts and count total time over the course of several months. I’m just starting to look around and this is the first thread I found that’s close.

EDIT: I think history_stats is exactly what I need. The only trouble is, I don’t know how to format the start to include history from the current year. For now, "{{ 0 }}" will work, but next year I’ll want a more specific format.

1 Like