it seems that all of this is harder than the original suggestion I made above…
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.
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:
And my input_boolean:
HA only updates the templated sensor once every minute but when stopped it’s to the second.
The result:
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.
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