Calculating average value from sensor

Hi all experts,
I´m stuck in the some calculation.
How can i calculate average value from sensor. Four values from same sensor got in different hour (e.g. 7hour, 14hour,…).
How can i calculate sum from sensor from day 1, month 2 to day 31, month 7 yearly cycle.

template:
  - trigger:
      - platform: time
        at: '07:00:00'
        sensor:
          - name: t_7
            entity_id: t_7h
            state: "{{ (states('sensor.T1') | float(0) | round(2)) }}"
  - trigger:
      - platform: time
        at: '14:00:00'
        sensor:
          - name: t_14
            entity_id: t_14h
            state: "{{ (states('sensor.T1') | float(0) | round(2)) }}"
  - trigger:
      - platform: time
        at: '21:00:00'
        sensor:
          - name: t_21
            entity_id: t_21h
            state: "{{ (states('sensor.T1') | float(0) | round(2)) }}"
  - trigger:
      - platform: time
        at: '23:55:00'
        sensor:
          - name: average_t
            state: "{{ (states('sensor.t_7') | float(0) + states('sensor.t_14') | float(0) + states('sensor.t_21') | float(0) + states('sensor.t_21') | float(0) ) / 4 | round(2) }}"

Thank you for your hint.

I’m not sure what you mean by this. “day 1 month 2” sounds like the 1st February and “day 31 month 7” would be 31st July. Are you wanting to average the sensor values between these two dates each year?

Hi Michael,
yes, thats exactly.

I thought I must have been wrong - it seems like a weird thing to do. Why? Anyway…

Home Assistant isn’t really set up to make this easy.

I would probably do it by ensuring that long term statistics are being stored (with state_class: measurement), since sensor data gets deleted after “purge_after_days”. Then you could use a SQL sensor to calculate the average between two dates. However, that’s going to recalcuate the average many, many more times than is really required.

Alternatively an automation (or perhaps more easily, Node Red) could read the long term statistics occasionally and update a (for example) MQTT sensor with the average (and with the retain flag set). But then you might as well just keep a running total between those dates, and divide by the number of days to get an average. This would not require long term statistics.

For example, every day at midnight, if it’s in the date period, send a new MQTT message to the total sensor, with a sum of the current total plus the new values. Then on 1st August, an automation that divides the sum with the number of days and sends it to the average sensor, and resets the total to zero. Or if you want a running average, run every day between Feb and Jul and divide by the number of days from Feb to now.

But you’re going to have to wait for a long time to prove to yourself that it’s all working as expected. Or am I misunderstanding what you’re after?

Thank you for your answer.
Maybe i´m not wrote correctly my intention. I need function called degreedays in english, but in Czech republic calculated from four values (average from four values) and between specific days called like heating season. Similar like this post Track your degree days using HA and benchmark your gas consumption - #16 by derSchreifritz?
I want to compare my degreedays with my previous heating season and publicated degreedays for the location in Czech republic where i´m living.
I don´t need measure heat consumption, because i will get this from the supplier at the end of the heating season.
Sorry for my bad english, but i’m 50 years old and i only studied russian in school :frowning: Thank you very much for the time you spent with my question.
I wish you merry xmas and happy new year.
Dalibor

No worries, what you’re saying makes sense. I’m in Australia, and although I use the heater a lot in winter, the coldest average month of July is still 7deg C, so we probably don’t use as much energy to heat. At least it never occurred to me to monitor it to reduce costs.

I think you ought to do both - put state_class: measurement on your entities so you get long term statistics and have another sensor that sums the averages during the winter months and calculates the average and the end of the period. Having the long term statistics is useful for doing graphs over many months.

And I’m 62 and only speak one language, so I’ve got no excuses :rofl: Merry Christmas to you too!