Average Sensor

Problem with ststistics is I cant make it give me the stats for TODAY. I dont want last 24 hrs I wan TODAY regardless of how many hrs that is

1 Like

Can you give an explanation of how to do this? Thanks

That’s what I’m trying to do at the moment. My statistics entity that replaces the old average entity is like this:

  - platform: statistics
    name: 'Average indoor temp over the last 24h unfiltered'
    unique_id: "mean_int_temp"
    entity_id: sensor.instant_indoor_temperature
    state_characteristic: average_linear
    precision: 1
    max_age:
      hours: 24

but as it bumps out of charts upon restart, I have a template sensor like this:

    - sensor:
        - name: 'Average indoor temp over the last 24h filtered'
          unit_of_measurement: "°C"
          state_class: measurement
          device_class: temperature
          state: >
            {% if (states('sensor.average_indoor_temp_over_the_last_24h') | float(0) > 35) or
                  (states('sensor.average_indoor_temp_over_the_last_24h') | float(0) < 5) %}
              {{ states('sensor.instant_indoor_temperature') }}
            {% else %}
              {{ states('sensor.average_indoor_temp_over_the_last_24h') }}
            {% endif %}
          attributes:
            last_reset: '1970-01-01T00:00:00+00:00'
          availability: >
            {{ (states('sensor.instant_indoor_temperature') is defined) and
               ((states('sensor.average_indoor_temp_over_the_last_24h') | float(0) <= 35) or
                (states('sensor.average_indoor_temp_over_the_last_24h') | float(0) >= 5) ) }}

It is extremely poor, at this moment, my average is around 19 then it goes off then comes back at 5 or 35 then slowly returns to 19.

I’m thinking about committing the change of code locally indeed and revert back to the Average integration, as the issue is reported already for a while in the statistics integration and no reaction so far…

From the PR you can see what changes have been made and then can edit the file yourself or download the patched file. Overwrite the one in your custom components folder and restart.

I think the question was how to make an average per day from midnight to midnight, not about applying a PR locally :slight_smile:

If you look at the exact quote of mine he tagged in his question my answer was 100% correct. If he has a different question I’m sure he can ask

1 Like

If that helps others here is a quick fix of the Average sensor component before any new release is posted:

1/ go to GitHub - Limych/ha-average: Average Sensor for Home Assistant
2/ download the full code in a zip, unzip in a local directory the files in /ha-average/custom_components/average
3/ copy all of them on the HA system in folder config/custom_components/average with samba or psftp or alike (alternatively, copy them directly from Github to HA if you are familiar with this)
4/ reboot HA

You will get a new version of the integration that includes the fix for the deprecated function. For me this works currently better that switching to the statistics integration with its current issues.

3 Likes

See: Uptime - Home Assistant

Though nothing will work as expected:

  • a trigger that this entity hold it’s state for x minutes will never fire
  • a sensor that some time passed after this time, will have transient value on startup

That is the only thing that seems to work:

input_boolean:
  startup_transient_is_over:
    name: "Startup transient is over"
    initial: off

automation:
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    condition: "{{ timedelta(minutes = 5) < now() - as_datetime(states('sensor.uptime')) }}"
    action:
      - service: input_boolean.turn_on
        target:
          entity_id: input_boolean.startup_transient_is_over

template:
  - sensor:
      - name: 'Average indoor temp over the last 24h filtered'
        unit_of_measurement: "°C"
        state_class: measurement
        device_class: temperature
        state: "{{ states('sensor.average_indoor_temp_over_the_last_24h') if is_state('input_boolean.startup_transient_is_over', 'on') else states('sensor.instant_indoor_temperature') }}"

Thanks @DavidFW1960 & @Gyosa3, live by HACS die by HACS. I learned something today!

1 Like

Yes I can see the logic there with the transient time, I would try it if I was desperate to use the statistics integration.

To be honest, with the workaround that was mentioned before to continue to use the Average integration in the future, I see at least 2 major reasons why to continue with it:

1/ I never had any issue with the integration, it is very stable, also at startup, values are very consistent and reliable to be used as conditions for automation (at least in the conditions I use the integration);

2/ I have discovered 2 extra functions that I bluntly ignored so far, thanks to the discussion around the deprecated function: the fact that the window of calculation can have a start and an end that are customisable, as well as the fact that the min and max values are available as attributes for the period concerned.

I was trying to replicate a min_temp and max_temp value per day with using statistics and template sensors, but the result was not at all elegant. With this integration, it’s very neat. As someone said before, this integration allows things that others don’t, and I think that it would be a great initiative to integrate permanently this integration within the statistics integration while fixing it.

Things like this should be possible as standard (imho):

# gives the min, max and average outdoor temp for yesterday
  - platform: average
    name: 'Average outdoor temperature yesterday'
    precision: 1
    duration:
      days: 1
    entities:
      - sensor.ext_temp
    end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'

# attribute extraction of average sensor
    - sensor:
        - name: 'Maximum outdoor temperature yesterday'
          unit_of_measurement: "°C"
          state_class: measurement
          device_class: temperature
          state: >
            {{ state_attr('sensor.average_outdoor_temperature_yesterday','max_value') }}

Edit: oh, and last thing to choose for Average integration, the statistics does not like at all if there is no value within the period of calculation, so the source sensor must be kept alive, which necessitates another template sensor between the source and the stats. This is for example for solar panels power which are at 0 for hours. The Average integration handles this gracefully :heart_eyes:

2 Likes

If you’re using HACS and the patch has been merged but not released, you can still update via HACS by switching the beta toggle ON and downloading the DEV branch…

Yes. This is why I started using average a long time ago.

First place I looked, but it wasn’t where I remembered, lol Your post made me look again. Thanks

1 Like

By default, undefined values are not included in the average calculation. Specifying this parameter allows you to calculate the average value taking into account the time intervals of the undefined sensor values.

Hi guys, this feature no longer works correctly in my case. What happens?

Hi the statistics sensor seems like a good idea, but it only builds data from the moment you create that sensor.

I want to build a graph of the average forecast today value over the last 23 hours, and put that on a graph to compare to my actual pv generated today.

technically, I want to average tomorrows forecast from yesterday, and display it against todays total generated, but I think the day will be shifted 1 to compare.

can this custom average sensor be used to calculate the Belgian “15 minutes energy consumption peak power” ?
For this I need the average power consumption (kW sensor is source) for fixed 15 minutes ranges.
So for example:
Quarter 1 = 00u00:00 until 00u14:59
Quarter 2 = 00u15:00 until 00u29:59
Quarter 3 = 00u30:00 until 00u44:59
Quarter 4 = 00u45:00 until 00u59:59
Quarter 5 = 01u00:00 until 01u14:59
Quarter 6 = 01u15:00 until 01u29:59
Quarter 7 = 01u30:00 until 01u44:59
Quarter 8 = 01u45:00 until 01u59:59
…

So for example if current time is 2u05, for every quarter (1 to 8, you would 1 1 average value for corresponding average value of that timeframe.
For current quarter 9, you would see a “changing” average (all values average of that 5 minutes which has passed from that 15 min frame.

Is it possible with this sensor and how do you put the settings ?
Now I use :
duration:
minutes: 15

But this only gives the average of thet last current 15 minutes (looks not fixed times, but a moving average of the last 15 minutes.

I think I found the solution :

  - platform: average
    name: 'Kwartier gemiddeld elektriciteit verbruik'
    start: >-
      {{ as_timestamp(
           now().replace(
               hour=int(now().replace(minute=(now().minute // 15) * 15, second=0).strftime('%H'))
           ).replace(
               minute=int(now().replace(minute=(now().minute // 15) * 15, second=0).strftime('%M'))
           ).replace(
               second=0
           )
       ) }}
    end: '{{ now() }}'
    unique_id: "be4903d0-53f4-11ee-8c99-0242ac120002"
    entities:
      - sensor.electricity_meter_power_consumption

this takes every running quarter xx:00 / xx:15 / xx:30 / xx:45 unitl now timestamp.
So when the next quarter is reached it starts over again in it’s average calculation.

Hey,

Im not the best with configurations in yaml so I will explain what I need and hopefully someone can help.

Here is my current config

sensor:

  • platform: average
    name: ‘Average Temperature Day’
    duration:
    days: 7
    entities:

    • sensor.openweathermap_temperature
  • platform: average
    name: ‘Average Temperature Night’
    duration:
    days: 7
    entities:

    • sensor.openweathermap_temperature

What I would like is a 7 day average of both day and night temperatures. I was thinking day time to be 6am untll 6pm and night time to be 6pm until 6am. Would it be possible to do this and if so what would the config be?

Thanks in advance

I have been using the Average Sensor for a while now without issue.
Recently I found a new usage for it, but am having trouble.

The issue is, that I would like to use an average over three Days. However, the base sensor should be read every day at 23:59 so that it has the complete daily consumption.

Is there a way of configuring this?

I recently started using this component, but it does not behave like I assume it should. I have successfully setup a sensor and restarted HA. Everything works as expected now, but when I do a “Quick Reload” in “Settings → System” the sensor becomes und stays unavailable. Once I do a “Restart Home Assistant” (also in Settings → System) the sensor comes back and works as expected.
I enabled debug logging and during the time the sensor is unavailable, I see successful updates on regular intervals in the logs. Noting point to an error there.

I am running Home Assistant: 2023.11.0
This is my config:

sensor:
  - platform: average
    name: 'Living Zone Temperature'
    unique_id: temperature_avg_living_zone_cd13009b
    entities:
      - sensor.atc_f3c7_temperature
      - sensor.atc_6ae8_temperature
      - sensor.atc_a166_temperature
      - sensor.atc_7347_temperature

and this is the log during the time the sensor is unavailable:

2023-11-02 15:54:05.505 DEBUG (MainThread) [custom_components.average.sensor] Updating sensor "Living Zone Temperature"
2023-11-02 15:54:05.506 DEBUG (MainThread) [custom_components.average.sensor] Calculation period: start=None, end=None
2023-11-02 15:54:05.506 DEBUG (MainThread) [custom_components.average.sensor] Processing entity "sensor.atc_f3c7_temperature"
2023-11-02 15:54:05.506 DEBUG (MainThread) [custom_components.average.sensor] Current state: 20.7
2023-11-02 15:54:05.506 DEBUG (MainThread) [custom_components.average.sensor] Processing entity "sensor.atc_6ae8_temperature"
2023-11-02 15:54:05.507 DEBUG (MainThread) [custom_components.average.sensor] Current state: 20.9
2023-11-02 15:54:05.507 DEBUG (MainThread) [custom_components.average.sensor] Processing entity "sensor.atc_a166_temperature"
2023-11-02 15:54:05.507 DEBUG (MainThread) [custom_components.average.sensor] Current state: 20.32
2023-11-02 15:54:05.507 DEBUG (MainThread) [custom_components.average.sensor] Processing entity "sensor.atc_7347_temperature"
2023-11-02 15:54:05.508 DEBUG (MainThread) [custom_components.average.sensor] Current state: 20.75

Does anyone have an idea, what I’m doing wrong?
Thanks in advance