MeasureIt: Measure all you need based on time and templates!

Is it possible to start the counting when the condition is ‘> 3’ and not a fixed value?

{{ is_state('sensor.psa_charge_rate', '> 3') }}

Sure: {{ states('sensor.psa_charge_rate') | float > 3 }}
Note that if this is the template for a counter sensor, it will only count when the value becomes higher than 3 and not after that when the value changes but is still higher than 3.
The template should first become False before it will be counted again.

1 Like

Nice work, @danieldotnl! Very useful integration.

Is there a built-in way to measure cycle times with this integration? Some duration examples:

  • refrigerator compressor
  • clothes dryer
  • irrigation zone
  • septic/sump pump

I think you could do it with a regular duration sensor that resets each time the monitored sensor switches from off to on, but I’m wondering if there’s a simpler way.

In the latest release you can choose for a reset period ‘after each session’. So it would reset each time the conditions become false. Would that be what you are looking for?

v0.7.0

For source meter configurations: source entities that reset can now still be tracked if they have state_class total_increasing.
As an example: we can now measure the precipitation per hour/day/week/month etc. based on a source sensor that resets every day.

Changes

  • Support source entities that reset with state class total_increasing @danieldotnl (#104)
  • Make sure we are compatible with HA core change regarding event filters in 2024.4 @danieldotnl (#109)
  • Add translatable icons @danieldotnl (#110)
  • Update dependency pytest-homeassistant-custom-component to v0.13.109 @renovate (#108)
  • Update dependency ruff to v0.3.4 @renovate (#107)

I’m glad having found MeasureIt ! :slight_smile:

till now I was using history_stats which has some drawbacks especially in regards to the recorder.
Question for you @danieldotnl: how could I initialize/set a measureIt counter with the value I’m currently having?
e.g. I counted the number of coffees my coffee machine has brewed, and each 400 cups I need to perform a maintenance. If my new coffee counter (based on measureIt) would start at 0 I would miss the right time for the maintenance.

I’m glad having found MeasureIt ! :slight_smile:

till now I was using history_stats which has some drawbacks especially in regards to the recorder.

Happy to hear that, and spread the word! :slight_smile:

Question for you @danieldotnl: how could I initialize/set a measureIt counter with the value I’m currently having?

That’s not yet possible. Could you please create a feature request at github? Thanks!

What you could do for now is to add the current number of cups in the value_template of the sensor, like: (value | int) + 102.
Once you clean it and reset the sensor, you should then also update the value_template.

@danieldotnl Thanks a lot for the fast reply.

Unfortunately I can’t figure out where to edit the existing template. Please have a look at the screenshots below.

grafik
Edit the main configuration shows the template as empty:

Edit a sensor configuration adding the value template leads to an “unknown error”
grafik

Seems like a bug. I’ll need to dive into the unknown error…

@danieldotnl I think it is related to the missing unit of measurement, I saw an entry in the logs which could be related.
But which unit of measurement should I enter for a counter?

Thanks for checking :slight_smile:

It was a bug anyway, so I fixed it in v0.7.1.
The unit of measurement can be anything you want for a counter. So for you ‘cups’ would make most sense I guess.

1 Like

@danieldotnl thanks for following up and providing a new release

I wanted to ‘edit a sensor configuration’ and have set the value template (value | int) + 1002.
and added cups (or Tassen) as unit of measurement.

And I got an error message telling me to check the logs:

Logger: homeassistant.config_entries
Quelle: config_entries.py:551
Erstmals aufgetreten: 08:40:59 (1 Vorkommnisse)
Zuletzt protokolliert: 08:40:59

Error setting up entry Kaffees insgesamt for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 551, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 139, in async_setup_entry
    return await component.async_setup_entry(entry)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 187, in async_setup_entry
    raise ValueError("Config entry has already been setup!")
ValueError: Config entry has already been setup!

If I don’t provide a “unit of measurement”, so only the value template, the error message is different:

Logger: homeassistant.config_entries
Quelle: config_entries.py:551
Erstmals aufgetreten: 08:40:10 (3 Vorkommnisse)
Zuletzt protokolliert: 08:50:57

Error setting up entry Kaffees insgesamt for measureit
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 668, in state
    numerical_value = float(value)  # type:ignore[arg-type]
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: '(value | int ) + 1002'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 551, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/measureit/__init__.py", line 114, in async_setup_entry
    run_start(None)
  File "/config/custom_components/measureit/__init__.py", line 109, in run_start
    coordinator.start()
  File "/config/custom_components/measureit/coordinator.py", line 135, in start
    sensor.on_time_window_change(time_window_active)
  File "/config/custom_components/measureit/sensor.py", line 399, in on_time_window_change
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1119, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1056, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1004, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 670, in state
    raise ValueError(
ValueError: Sensor sensor.kaffees_total has device class 'None', state class 'total_increasing' unit 'None' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '(value | int ) + 1002' (<class 'str'>)

okay, my bad.
the value template should have been provided with parenthesis
{{ (value | int) + 1002 }}

it works, and yes, if I provide ‘unit of measurement’ it’s working as well.

Unfortunately the value template is not applied to the historic values.
Which is most likely pretty difficult, if not even impossible.

@danieldotnl
I’ve set up my first time meter with MeasureIt ‘sensor.zeit_seit_bruhgruppe_reinigen’ :slight_smile:

I’ve configured it as manual reset and with min as unit of measurement. Unfortunately it still measures in seconds, I use a template to show the time since cleaning the brewing unit of my coffee machine:
states('sensor.zeit_seit_bruhgruppe_reinigen')
the template shows a time in seconds.

The entity dialog instead shows hh:mm:ss and min on the legend of the y-axis, but the values are seconds.


I took the screenshot at ~22:15

My aim is to see the time elapsed in minutes. Hence I set the unit of measurement to minutes. But I still get seconds.
Is that the intended behavior?
Do I need to divide the value by 60 in the value template?

Any help is appreciated.

Nick, did you find this in the FAQ? Let me know if it helps!

OH :flushed:
@danieldotnl Thank you so much. I would have bet I’ve read the docs, but I missed that part. Thanks for pointing me there.

I’ve now added a value template {{ (value / 60) | round(0) }} that converts the measured seconds to minutes, which now matches the unit of measurement.

Small remark: the brackets around (value / 60) are important, because value / 60 | round(0) would apply the round only to the 60, and we would still see fractions of minutes.
Perhaps it’s worth to adjust the documentation.

Thanks again! :pray:

You are welcome and thanks for the remark. I updated the README.

PS: calibrate service coming soon!

1 Like

v0.8.0

This release introduces a calibrate service which can be used to adjust the value of the sensor. E.g. you start measuring the number of coffees your coffee machine produces, but it already brewed 50 cups before you configured MeasureIt. You can now set it to 50!

Until now, all sensors other than hourly ones, were resetting at midnight. It is now also possible to reset your sensors at custom intervals by means of a cron expression. E.g.: when you want to measure the time you spent in bed, it’s not very convenient if the sensor resets at midnight.

Changes

  • Update dependency ruff to v0.4.1 @renovate (#124)
  • Refactor reset service implementation @danieldotnl (#122)
  • Implement a calibrate service @danieldotnl (#121) Closes #115
  • Add support for custom cron expressions @danieldotnl (#120) Closes #102
  • Update README.md @danieldotnl (#123)
  • Update dependency ruff to v0.3.7 @renovate (#118)
  • Update dependency pytest-homeassistant-custom-component to v0.13.114 @renovate (#119)
  • Update szenius/set-timezone action to v2 @renovate (#117)
  • Update dependency pytest-homeassistant-custom-component to v0.13.113 @renovate (#116)
2 Likes

Work like a charm!!
@danieldotnl Many thanks for the calibrate service in particular and for MeasureIt in general :pray:

1 Like

Hi all

How should I create some MeasureIt sensors (daily, weekly, monthly and yearly) from this power sensor ?

Thanks