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/

Hey I know this is pretty old, but I stumbled in here while searching for the same thing.

I ended up going with the Utility Meter integration. I seems a little inefficient because I understood that it is counting up and resetting the counter instead of just calcurlating “(current Value) - (value at start of day)”, but it gets the job done and was very easy to configure.

I use this automation and input _number helpers to store the data I want, the automation triggers every evening at midnight and ‘cascades’ the day’s sensor value to the next inline.

i.e.

Today -4 days is stored in today -5
Today -3 days is stored in today -4
Today -2 days is stored in today -3
Today -1 days is stored in today -2
Today is stored in today -1

id: '1699398125439'
  alias: "Outside temp history daily update"
  description: Update temp  entities
  trigger:
  - platform: time
    at: '23:59:59'
  condition: []
  action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_5
      value: '{{ states("input_number.outside_temp_min_24_hr_4")| round(2) }}'
  
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_4
      value: '{{ states("input_number.outside_temp_min_24_hr_3")| round(2)}}'
  
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_3
      value: '{{ states("input_number.outside_temp_min_24_hr_2")| round(2)}}'
  
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_2
      value: '{{ states("input_number.outside_temp_min_24_hr_1")| round(2)}}'
  
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_1
      value: '{{ states("sensor.outside_temp_min_24_hours") | round(2) }}'

Have the exact same requirement. Allow an automation only to run if a temperature in the last past 12h was not below x and in the last 4 hours not above y. The recorder has the data for that temp sensor to answer that exactly at the point in time when the condition matters.

Instead the only way I found, to achieve that is to create additional statistics sensors unique to that single conditional question, continiously hammering the recorder with their own redundant measurement data just to eventually provide a very simple answer once in a while?

Can’t be true. Unfortunately I couldn’t find a way to access existing historical sensor data in a jinja template. I mean that’s such a basic thing in useful automations, beond what a stupid electrical lightswitch can do. I mean whats the recorder even there for in the first place if automations can’t use the data?