Get sum for today, yesterday, last x hours from a sensor

So I’m having a little trouble setting this up. I have a sensor that tracks the sum of sunshine hours since it has been reset. It looks like this:

Now I want to display in the dashboard:

  • Sunshine today (so far)
  • Sunshine yesterday
  • Sunshine in the last x hours

(this is just an example, I have more useful values I want to display as well ;-))

A bit more general the question would be: How can I do calculations with the historical data that Home Assistant has stored?

For the today part I’m experimenting with the Utility Meter - Home Assistant integration, but that seems a bit off since I’m measuring sunshine and rain and not some utilities.

What’s the “Home Assistant” way of doing this?

I’ve seen this integration but I think it does not apply to my problem. As I read a state to track is needed (like “on”) - I do not have that. Also neither of the described 3 modes (time, ratio, count) seems to fit my scenario.

But maybe I’m reading it wrong. How can I use this integration to achieve what I want here?

Isn’t the title of this topic:

Get sum for today, yesterday, last x hours from a sensor

I use it to track the number of hours per day that my HVAC system is either heating or cooling. Then I supply that daily information to the Utility Meter integration to get weekly/monthly/yearly values.

You can make the time range whatever you want provided you don’t exceed what’s available, for an entity’s history, in the database (typically several days worth of data).

I understand that this works in your case, because probably the HVAC has a status “cooling” or “heating” that the History integration can track over a free to define period of time.

But in my case all I have is a sensor with a steadily rising “total value” (see graph above). I can not see how I would use the History integration with this.

My mistake; you’re right. History Stats tracks a given value but you want to calculate the sum of values over a period of time.

I think the closest thing might be the Statistics integration. I think you would have to use max_age to restrict the time range (but it still might not meet all of your requirements).

Maybe this would work?

No, the Riemann sum integral is kind of the other way round - it converts for example power (W) to energy spent (Wh) by integrating over time. I already have the integrated values, so to speak.

Hm I’ll take a look at the Statistics integration again. It seems it can give the min and max values, so for a sensor like mine (always just adding up) the calculation would be an easy “max - min” for the given time span. Maybe pushing the sampling size to something near infinity (instead of just the last 20 measurements) and setting max_age appropriately I could possibly get the “last x hours” and “today” values. No idea how to go about “yesterday” with this approach though.

As a side note, I just noticed that Home Assistant can do the “today” part in it’s own “Energy” tab:

.

Ah, sorry. I guess I was confused by the thread title as well.

It’s really strange how e.g. the new “Statistic Card”, that has been introduced with 2022.11, can return a value for today, yesterday etc., but the Statistic Integration only provides this “max_age”, which doesn’t help for yesterday or last month…

1 Like

Did you find a solution for this?
I have a similar problem where my heating unit only supplies number of starts since it was installed and the total amount of time on in seconds since installed. It would be nice to combine these to produce how many starts and how much time spent on per hour and per day.

same here - it’s surprising that this, probably widely needed and useful feature is not natively supported by HA…

I’m looking for a solution to this as well. Hopeful if someone can help.

By any chance, did somebody found a solution ?
I am trying to achieve the same, asked here and on reddit but no answer so far…

It feels like I am missing something obvious as the card is showing what I need but I can’t find a way to use it :slight_smile:

https://www.reddit.com/r/homeassistant/comments/1aqi4ke/statistic_card_to_template_sensor/