Im looking to see if its possible to create a sensor that gives me the average daily power consumption but updates when a new day starts… i hope i have explained this??
So for example if I manually calculate my average daily Power consumption its 14.83, but lets say I use more today, i would then expect that number to slightly increase.
Do you have a sensor that records your daily energy consumption measured in kWh or Wh?
The average for yesterday is easy enough:
template:
- trigger:
- platform: time
at: "23:59:58" # misses 2 seconds of consumption in 24 hours (which is insignificant), but ensures the calculation is done before the daily sensor is reset.
sensor:
name: Average Daily Consumption Yesterday
unit_of_measurement: "kW" # or W depending on your daily energy sensor
device_class: power
state_class: measurement
state: "{{ (states('sensor.daily_energy_consumption')|float(0) / 24 )|round(2) }}"
availability: "{{ states('sensor.daily_energy_consumption')|is_number }}"
If you want one for the current day that is a bit trickier, as you have to work out how many hours have been in the day so far, including fractions of hours.
template:
- sensor:
name: Average Daily Consumption Today
unit_of_measurement: "kW" # or W depending on your daily energy sensor
device_class: power
state_class: measurement
state: >
{% set hrs = [ now().hour + now().minute/60, 0.01]|max %} {# this template only updates every minute so we don't have to worry about seconds, we do have to worry about it being zero though #}
{{ (states('sensor.daily_energy_consumption')|float(0) / hrs )|round(2) }}
availability: "{{ states('sensor.daily_energy_consumption')|is_number and (now().hour + now().minute/60) > 0 }}"
Thanks for this. Im not sure this is what im after, but i could be wrong.
I did a quick simple math
I have used 1943kwh over 133 days since i started recording. This = 14.60kwh daily average.
Lets say i use 50kwh today which brings my total to 1993kwh
My daily average just went up = 14.87
Regardless of what’s on the end. if I calculate my average daily power consumption based on what my energy dashboard shows me I have consumed in total so far it’s 14.60 as the average…
How do I get that but so it updates and takes into account the day just gone by?
Hey there @cloudbr34k, did you ever figure out how to get this daily mean average of usage? I’m seeking this myself and have some thoughts, though not a full plan yet.
I have the start of an idea. I already used a built-in “Helper” (Energy Meter) to show today’s usage, and it keeps track of history. So it’s just a matter of extracting those days and doing the math. The Statistics - Home Assistant integration has some average functions that would work easily enough—it’s just a matter of fetching the daily values to feed in. I have no clue on that front, so far! I’m sure I’ll get there, but if you have had luck in the past 7 months, it’d save me a lot of time!
PS: I found your request pretty easy to understand. It was described okay initially, but became very easy to understand after you provided more detail. Maybe the moderator just has a slight language barrier? Though, you also provided math, so IDK! Anyway, just wanted to give you credit there.)
Here’s how I calculate the amount of energy used to heat water over a rolling window of 15 days:
sensor:
- platform: statistics
unique_id: hot_water_energy_total_over_15d
entity_id: sensor.hot_water_energy_meter # <- value only ever increases (e.g. utility meter with no cycle)
name: "Hot water energy over the last 15 days"
state_characteristic: change
max_age:
days: 15
The same approach applies to a rainfall sensor, for example. The important thing is that the state of the source sensor (entity_id) is monotonically increasing.
To get the average only requires one more sensor to divide by the size of the rolling window, 15 days in my example.
template:
- sensor:
- unique_id: hot_water_energy_average_over_15d
device_class: energy
unit_of_measurement: 'kWh'
state: |
{% set total_kwh = states('sensor.hot_water_energy_total_over_15d') | float %}
{% set days = 15 * max(state_attr('sensor.hot_water_energy_total_over_15d', 'age_coverage_ratio'), 0.0001) %}
{{ (total_kwh / days) | round(2) }}