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

MeasureIt launched! :partying_face:

After many years I decided to build a custom component that does exactly what I was missing: Measuring all kind of things I would like to know, based on what happens in Home Assistant.

Measure It

MeasureIt can measure all kind of things happening in Home Assistant based on time and templates.

Some examples of use cases:

  • Measure the daily shower duration
  • Measure the number of planes during the night flying over your home
  • Measure the time your kids are watching tv every day
  • Measure how many times the door opens when the AC is on

MeasureIt has overlap with history_stats and utility_meter but provides other features as well, is easier to set up and can measure based on conditions and time windows.

Note: you do require some Home Assistant templating knowledge for most use cases. If you need help with this, do not create a Github issue but ask your question on the community forum.

Developing and maintaining integrations like this one is a lot of work. Please consider supporting me:

Buy Me A Coffee

How does it work?

MeasureIt currently offers 3 different ā€˜meter typesā€™ which you can choose from: time, source, counter.

Time

Time is basically just a timer that runs when all the conditions that you provide in a template are met. You can also configure to only measure during specific times. E.g. only in the weekend, or only during the night. Time meters measure in seconds but the sensors update every minute. E.g. measure when the following template applies {{ is_state('media_player.tv', 'on') }}.

Source

Source meters do listen for state changes in another entity and measure the difference. E.g. listen to the gas consumption sensor, and keep track of how much it changes when the shower is on.
Currently source entities that do reset are not supported (e.g. you cannot yet use MeasureIt to measure the amount of precipation in a week based on a precipation sensor that resets every day).

Counter

A counter meter counts how many times a configured template changes to True. E.g. {{ is_state('binary_sensor.front_door', 'on') }} counts each time the front door opens.

The measurements are kept in sensors for different periods that can be configured. So a day sensor will reset each day and a year sensor each year. You can also choose for a sensor that does not reset automatically.
You can manually/from an automation reset a sensor at a given time with the measureit.reset service.

Installation (using HACS)

hacs_badge
hacs installs

Install quickly via a HACS link

MeasureIt is included the standard HACS repositories. Install it via the standard steps, or quickly with the button above:

  • Search for MeasureIt in the Integrations section of HACS,
  • Choose ā€˜MeasureItā€™ and select the Download button,
  • Restart HomeAssistant to complete the installation.

Configuration

Go to Settings ā†’ Devices & services, and hit the ā€˜+ add integrationā€™ button. Search for MeasureIt and click it to start the configuration flow.
The config flow is descriptive and hopefully as clear and simple as possible.

Do keep in mind that the config flow is guiding you through 3 important steps:

  • What do you want to measure?
  • When do you want to measure?
  • How do you want to measure?

The what is the time, source or counter described above, plus the details required for those. The when is all about the conditions that should be met for measuring. And the how is about the sensors that will be created. Here you pick the periods (e.g. per day/week/year) and for each of those a sensor will be created.
For the additional sensor properties like unit of measurement, state class and device class, defaults are picked as good as possible. Only change those when you know what you are doing.
If you want different properties per sensor, you can add additional sensor after setting up MeasureIt, by choosing ā€˜configureā€™ (the options flow) behind you MeasureIt configuration in ā€˜Devices & servicesā€™.

FAQ

How do I show time sensors in a different format?

By default, the device class duration is applied on time sensors. This, in combination with the unit of measurement s (seconds) lets Home Assistant know what this sensor is about. HA will automatically apply an applicable format in the frontend (the format changes depending on the amount). I recommend using this.

If you really think you need a different format, you can do so by providing a value template for the sensor (in the ā€˜howā€™ part of the config). E.g. if you want to show hours, you can divide the state by 3600: {{ value / 3600 | round }}. This is still numeric and supports long term statistics.
You can also change the format in a string format like ā€˜HH:MMā€™. E.g. with this value template: {{ value | float | timestamp_custom('%H:%M', false) }}.
Attention: if you really want this, do not use a device class or a state class. This will format the state in a string and statistics cannot be calculated.

How can I show more/less digits for source and time sensors?

MeasureIt is rounding source sensors states to 3 digits and time sensors to seconds. If you need more or less digits, you can do so by providing a value template. The value inserted in the value template is not rounded. E.g.: {{ value }} will give you all digits.

How can I reset a sensor when I need to?

You can reset a sensor manually/via an automation, with the measureit.reset service. This service takes the entity ids of the sensors you want to reset, and optionally a future reset datetime. By default, it will reset the sensor immediately.


Iā€™m really curious what awesome use cases you guys will come up with, and looking forward to your thoughts and reactions!

MeasureIt: GitHub - danieldotnl/ha-measureit: Measure anything in Home Assistant based on time and conditions.

17 Likes

Hi,
I just set it up to measure roborock docked time. But as I understand this integration is only measure time in seconds.
Can it do it in hours? It will be much convenient.
Edit:
Probably can be converted using value template as

{{ (states('sensor.my,sensor_day') | float / 3600) | round(2) }}

Indeed, use the value_template for that and then set the unit of meaurement to: ā€œhā€

Home Assistant accepts the following unit of measurement for device class ā€˜durationā€™:
</s> <s>seconds = s</s> <s>minutes = min</s> <s>hours = h</s> <s>days = d</s> <s>

MeasureIt relaunched. For formatting time sensors, please read the updated readme or first post of this topic.

1 Like

Hi danieldotnl, first of all great implementation!
Iā€™m trying to monitor the usage of my Dryer Machine, Iā€™m able to calculate the total amount of ā€œrunning timeā€ thanks to your component, but I have a question: can I use it to calculate how many time the machine has been used (e.g. 1 per day, 7 per week). From the instruction it looks possible, but I cannot make it work.
I have a Smarthings integration, and thanks to that I have a switch for the overall status of the machine, and a sensor showing the running status (feasible value would be ā€œrunā€).

How should I configure the integration to compute such value?
Thanks!

Thank you, and Iā€™m happy to see people using this integration!
You are right about the count feature. I was a bit too enthusiastic in my opening post. Iā€™m still working on the feature. Expect it soon!

Will there be a possibility to reset the forever meter? I want to measure the water usage since last emptying of sewage tank and would love a reset measurement option based on template or service call.

1 Like

Yes, Iā€™m working on it, but it is a bit more complex than anticipated :slightly_smiling_face:

3 Likes

Iā€™m not sure how to use this with tracking my power usage and solar generation; I have sensors that provide wattage generated by solar and wattage imported from grid, but they are changing on a regular basis. Do I just use Source Sensor as what I want to measure? Iā€™ve tried this and getting some negative results on solar generation - which really canā€™t be true.

Can you please explain, what we need to type into the value template field so, that the measurements will be in minutes? Sry Iā€™m a beginner :confused:

Please have some patience. Iā€™m currently rewriting this integration to solve some issues and add some features. It got popular faster than expected :slightly_smiling_face:

One of the issues is that the device_class is hard-coded for time sensors, which interferes with the units you want to use for measurements.,

1 Like

Iā€™m not sure that is a good use case for this integration. If you would have a total production sensor, you could use that one as a source, and measure per day/week/month. Sound like you are now trying to create that total number with measureit, but I guess wattage is all over the place with the sun coming and going and clouds in front of it.
I can only think of negative values if you start measuring while your panels are producing and after a while production decreases. MeasureIt would measure the difference between those moments. But once your reset at midnight, this shouldnā€™t occur anymore.

1 Like

In Germany there are some youtubers that have made it popular :sunglasses:

Can someone provide a sample of using it for counting times sensor is in on? i.e number of times a door is opened or lights are switched on. I am having problems getting it to count. thanks

3 Likes

I have MeasureIT set up to measure the number of seconds each day that the thermostat is calling for heat.

How can I save that data to be able to look back at earlier dateā€™s totals?

Thank you.

:partying_face: Relauching MeasureIt! :partying_face:

It took some time but here you have a brand new MeasureIt. And I heard you! It contains:

  • a counter sensor :raised_hands:. Count each time a template evaluates to True! E.g. {{ is_state('binary_sensor.front_door', 'on') }}
  • a reset service that can reset any sensor, now or at a future datetime
  • sensors that do not reset (automatically, but you can reset them with the reset service)
  • source sensors now update immediately when the source entity updates
  • the issues with the sensors sometimes resetting at UTC midnight has been fixed.
  • fixed issue with device_class and state_class that did not respect what you configured.
  • lots of automated tests

I think all of this deserves a version jump to 0.5.x!

A lot changed and have been rewritten but I tried to support your existing configurations as good as possible! If you still do encounter issues with those, please delete it a create a new config entry.
If you have sensors that were affected by the UTC reset time bug, you can set the correct datetime with the new reset service.

Documentation has been updated as well, so please re-read the README on github. (or the updated first post of this thread)

Last but not least, please share your use cases in this thread, so I can summarize them every now and then, in the first post to inspire other people!

2 Likes

v0.6.0

You can now choose a sensor that will reset after each measuring session. E.g. when you want to measure some device that is running multiple times per day.

An issue with daylight savings time is fixed which caused the monthly pattern to finish 1 hour too early. Please fix your existing configurations manually with the reset service.
Thank you @misa1515 for providing Slovakian translations!

Changes

3 Likes

Hi all

I have defined a day sensor and it looks like this

if I use it in a card without templating, I see like this

Screenshot 2024-03-17 162748

How can I show it as HH:MM ?

Thanks

First of all, Your unit_of_measurement should be s. Then, together with the device_class: duration, Home Assistant will automatically format the duration, usually in HH:MM:SS.
If thatā€™s not desired, please check the FAQ in the readme.

1 Like

Thanks!
If I want to create also a weekly and monlthly sensor, it will also convert in a relative format, showing also days ?

Yes, I believe it does! Iā€™m hoping for some more configuration options in the frontend soon, as I feel formatting durations is quite messy at the moment. The number of questions about it on the forum is significant.

The best thing in my opinion is to format in a frontend card. E.g. Iā€™m using a mushroom chip template card to format tv time:

- type: custom:mushroom-chips-card
    chips:
      - type: template
        icon: mdi:eye-circle
        content: >-
          Today - {{ '%2d:%02d' % ((states('sensor.tvtime_day')|int/3600)|int,
          (states('sensor.tvtime_day')|int%3600)/60) }} hour

image

1 Like