Average temperature every day from 6 to 12

Hello, I need some advice.
I have temperature sensors, I can make averages for days, weeks or even in a certain period of time. But I need A little something different.
I need to create a sensor that would only have data from 8-16 hours. so that I can average the days only in this time period. I use this by default
start: “{{ now().replace(hour=8).replace(minute=0).replace(second=0) }}”
end: “{{ now().replace(hour=16).replace(minute=0).replace(second=0) }}”
but this is for the current day. but I need it for every day.
So that I can average the temperature per month in the room during the heating time which is from 8-16 (for example). thank you for the advice

Can you show the cnofig for the 8-16 sensor?

I currently have this, but that’s only the current day.

sensor:
  - platform: average
    name: "Kitchen"
    entities:
      - sensor.01_temperature
    start: "{{ now().replace(hour=8).replace(minute=0).replace(second=0) }}"
    end: "{{ now().replace(hour=16).replace(minute=0).replace(second=0) }}"
    unique_id: sensor.avg_01_temperature

I thought of using a labda filter. but I don’t know how to use the data only from 8:00 am to 4:00 pm every day.

In short, I need data only from 8-16 hours, I need to discard the others to make an average every day only in this time period and then monthly in the same time period from 8-16. At this time it is heating and I need to make an average for the month for how much it heats. I am sorry for my English. use a translator

Feed that sensor to another average sensor but start at the beginning of the month and end at the end of the month.

This assumes you keep 30 days history in your recorder.

I understand what you mean by that, but it doesn’t solve my problem.
Because on the first Average sensor I created, the value changes over time (the value is always averaged to a new value). But if I give average again, it makes the average of all the averages of that day and that is nonsense. For me, it would be to create a sensor in which all measured values from 0:00-8:00 and then from 16:00 - 24:00 are deleted. and use it already on the stored data in the original sensor.

If you create a template sensor that has something like:

{% if (8 <= now().hour >= 16) %}
{{ states('sensor.temperature') }}
{% endif %}

Then I believe this sensor will only update between 8 and 16.
If you then create an average sensor that uses the data of this sensor then it should be what you want.

So it would look like this

template:
  - sensor:
      - name: "Kitchen"
        unit_of_measurement: "°C"
        state: >
            {% if (8 <= now().hour >= 16) %}
            {{ states('sensor.01_temperature') }}
            {% endif %}
        unique_id: sensor.time_01_temperature

Where sensor.01_temperatu is the original sensor with all measurements
and sensor.time_01_tempretature with 8 am - 4 pm selected.
Is that right?

I would think so, yes.

Just to be clear, we are now using a “weakness” of HA to our advantage.
The average sensor only updates when the target sensor is updated.

So if the temperature is stable at 20 degrees for 10 hours then drops to 10 degrees in 5 seconds then the average will be 15.

if that’s the case I’ll be happy. i will try and see tonight. and it is possible to do it differently than this way. Because if I were to say at the end of the month that I don’t want until the 16th but the 17th, the data is not there. Is it possible to use a filter to display already stored data in the original sensor? retroactively. I know I’m a demanding person

  1. That’s not a valid template. There is no else case, i.e. it is undefined outside 8-16.
  2. Doesn’t your average sensor only update between 8 and 16 anyway? In which case feeding it to a monthly average will work.

So how do I get the daily/weekly/monthly average temperature (from 8:00 a.m. to 4:00 p.m.) or only night temperatures (from 4:00 p.m. to 8:00 a.m.). I will change the times, for example, after running. Therefore, I need to apply it to the already original sensor with all the data. When I create a new sensor, it only has data from when it was created, but it does not have the history of the original sensor. and here I need it

You already have your daily 8-16 sensor, you posted it here.

Feed that daily 8-16 average to a monthly average sensor.

sensor:
  - platform: average
    name: "Kitchen Monthly Average"
    entities:
      - sensor.avg_01_temperature #### <---- Daily 8-16 average ####
    start: "{{ now().replace(day=1, hour=0, minute=0, second=0, microsecond=0 ) }}"
    end: "{{ now() }}"
    unique_id: sensor.monthly_avg_01_temperature

This doesn’t make sense. it’s a mathematical paradox.
I will explain, I have the first sensor.avg_01_temperature
the first day of the month
at 9:00 - 21°C (from 8:00 to 9:00)
at 10:00 - 21°C (from 8:00 to 10:00)
and so on
at the end of the day it is
at 16:00 24°C (from 8:00 to 16:00)
and on the same day (1.)
by sensor.montly_avg_01_temperature would average the data from the values 21.21…24 so for example 22.74°C. But the news average value for the month is still 24°C because it is the first day of the month.

So you only want to sample the average from 8:00 to 16:00 at 16:00 each day?

I’m sorry it wasn’t obvious from the beginning.
It would be enough for me if I could make a copy of the original sensor without the given time period that I don’t want. retroactively. because I have the data for about a year already. but until now I always need it during the day and not at night, which I may change as needed. I don’t need data from the night. With the fact that today I will set the night to last from 4:00 p.m. to 8:00 a.m. but I find out after half a year that it should be from 5:00 p.m. to 7:00 a.m. That’s why I need it so that it can also be displayed backwards from the original sensor.

Not sure if this is still an open discussion, but I more or less want to achieve the same.
I would like to know the average temperature between i.e. 08:00-16:00. Not per hour, just what the average was in that whole timespan. Based on that I’d like to cool my greenhouse 6C below that average, with a minimum of 21C. This because during the summer the nights are too warm to the liking of my peppers. (I have a small greenhouse with peppers, which I sell on our local market).
So my question goes a little bit further than just an average temperature, I’d like to use the outcome for setting the cooling temperature of my airco.
Does anyone know how to do this?