Average Sensor

Use the average platform.

This is my template:

- platform: average
    name: "Medeltemperatur natt"
    entities:
      - sensor.vaderstation_temp_ute_temperature
    end: "{{ now().replace(hour=6).replace(minute=0).replace(second=0) }}"
    duration:
      hours: 8
  - platform: average
    name: "Medeltemperatur dag"
    entities:
      - sensor.vaderstation_temp_ute_temperature
    start: "{{ now().replace(hour=6).replace(minute=0).replace(second=0) }}"
    end: "{{ now().replace(hour=22).replace(minute=0).replace(second=0) }}"

ā€œMedeltemperatur nattā€ is night temperature
ā€œMedeltemperatur dagā€ is daytime avg temperature.

Based on these sensors I send a notification around 07:01 for night tempā€¦

service: notify.all_devices_notification
data:
  message: >-
    NATT rapport fƶr Temperaturer<br> Medel
    {{states('sensor.medeltemperatur_natt')}}<br>MAX
    {{state_attr('sensor.medeltemperatur_natt','max_value')}}<br>MIN
  title: TEMPERATUR INFO (22:00 till 07:00)

The average is installed via HACS.
AVERAGE

1 Like

That is an option, but would be limited to the current day, right?
So a ā€œbinary_sensor.person_awake if binary_sensor.night=onā€ or ā€œsensor.person=awake if binary_sensor.night=onā€ for the last 7 days would not work :stuck_out_tongue:
I was kind of trying to disguise the nerdyness of the question, haha

Yes, I do have a sensor for myself with states like sleeping/home/work/away
But I should expand it and have attributes for awake/sleeping etc

There is an issue on github but raising here as wellā€¦ this integration is broken in 2022.5

2 Likes

@bdraco this integration gets broken by

Any idea on how to fix it?

1 Like

That integration is a copy of history_stats with some modifications. It needs to be updated to use the newer apis and, the design which wonā€™t overload the database with queries like the version that this integration is based on

It would be better to add the new functionality to the core integration so it wonā€™t break in the future. If you donā€™t want to do that copy history_stats from 2022.5 and make the same changes again. The caveat is that this path means it will break again sometime in the future when something else in core changes.

Also its calling history.state_changes_during_period

which defaults to include_start_time_state=True

So you probably want to remove

As the start time state is already in the list.

those are lines 490-496 on mine for some reasonā€¦
I tried removing those lines. Still errors.

Any chance @bdraco you could post what you think would be a working sensor.py?

Sorry, it looks like that integration hasnā€™t been maintained for years. Itā€™s a project to update it, and thatā€™s not something Iā€™m particularly intrested in taking on.

Not that its a particularly good idea, but it will get it back to working again: You can copy the old code that was removed in Remove get_state and get_states history api calls by bdraco Ā· Pull Request #70830 Ā· home-assistant/core Ā· GitHub and inline it in the integration until it can be fixed properly

Does anyone know about a core alternative that will allow to calculate time-base average ?

I was relying on this custom component to automate lighting and covers around the house - based on 15 minute averages from my Ambient Weather lx sensor. I switched over to the core statistics sensor using Mean - working again, no noticable difference for my use-case.

Updated Statistics sensor followed by previous Average sensor:


sensor:
  - platform: statistics
    name: 'Avg Light 15'
    entity_id: sensor.ambient_weather_solar_rad_lx
    state_characteristic: mean
    max_age:
      minutes: 15
    unique_id: sensor.statistics.avg_light_15

#  - platform: average
#    name: 'Avg Light 15'
#    duration:
#      minutes: 15
#    entities:
#      - sensor.ambient_weather_solar_rad_lx

I was just starting to look at this

For me, I would want to get the average of a sensor for the past 1,3 and 7 days

Max_Age is fine assuming that I can put
days: 1|3|7

But on the Average sensor I could put

end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'

Guessing thereā€™s no equivalent for this statistics based on the documentation?

The kicker for me is they removed min and max attributes from statistics as well. They now need to be seperate sensors. This screws up my flex table card

GitHub - jeroenterheerdt/HADailySensor: Sensor for Home Assistant that gets reset at midnight might work for you. I think it will for me as I always used midnight anyway

Maybe notā€¦ it doesnā€™t have attributes. Going to request this on the repo.

Take care that the calculation is different : with the core statistics sensor, if your paramater has the value 10 for 14 minutes, and then 20 for the last minute, the mean value will be 15.

With this custom component it would be something like 10.67 (temporal average).

1 Like

I used sensors like this:

- platform: average
  name: average_sensor_0
  entities:
    - sensor.temp_sensor
  precision: 1
  start: '{{ now().replace(hour=0).replace(minute=30).replace(second=0) }}'
  end: '{{ now() }}'
  scan_interval: 1800

- platform: average
  name: average_sensor_1
  entities:
    - sensor.temp_sensor
  precision: 1
  start: '{{ now().replace(hour=0).replace(minute=30).replace(second=0) - timedelta(days=1) }}'
  end: '{{ now().replace(hour=23).replace(minute=59).replace(second=0) - timedelta(days=1) }}'
  scan_interval: 1800

- platform: average
  name: average_sensor_2
  entities:
    - sensor.temp_sensor
  precision: 1
  start: '{{ now().replace(hour=0).replace(minute=30).replace(second=0) - timedelta(days=2) }}'
  end: '{{ now().replace(hour=23).replace(minute=59).replace(second=0) - timedelta(days=2) }}'
  scan_interval: 1800

 ...

I havenā€™t found another way of doing this kind of moving average with sliding window moving in the past other sensors. Does anybody know if thereā€™s an alternative?

Sure. Thatā€™s also what Iā€™m using.
What you are missing here is that this custom component does not work anymore with the last Home Assistant core version. :confused:
Maybe Andrey will be able to update his component. But still, it would be nice that this feature would be available in the core.

EDIT : oh no, you have figured out! I have read too fast

Hi Folks. In case you havenā€™t seen it a fix with an updated sensors.py file is available, seems to work, see here. Big thanks to Imagyar! :clap: :clap: :clap:

2 Likes

Maybe also useful for your use case.

Iā€™ve changed the average sensor values of last days with an input number and an automation, like this:

input_number:
  # Fresno
  average_sensor_1:
    name: "Average sensor day -1"
    initial: 0
    min: 0
    max: 200
    step: 0.1
  average_sensor_2:
    name: "Average sensor day -2"
    initial: 0
    min: 0
    max: 200
    step: 0.1

automation:
  - id: update_average_previous_days
    trigger:
      # run at almost midnight
      - platform: time
        at: '23:59:50'
    condition: []
    action:
      - service: input_number.set_value
        target:
          entity_id: input_number.average_sensor_2
        data:
          value: >-
            {{ states('input_number.average_sensor_1') }} 
      - service: input_number.set_value
        target:
          entity_id: input_number.average_sensor_1
        data:
          value: >-
            {{ states('sensor.ha_daily_sensor') }}
    mode: single

where sensor.ha_daily_sensor could be the sensor @DavidFW1960 talked about

I was using it too, and since my heating is depending on that, I decided to switch to HA integrated features. So I am using statistics in combination with min/Max now. Quadruples the number of YAML lines, but at least should be more future reliable.
With statistics, you need to choose state characteristic ā€žaverage_linearā€œ, which reflects time constants, too.

## Aufbereitung Temperaturdaten fĆ¼r Heizungssteuerung ##
#  zuerst Statistics per Weathersensor                 #
  - platform: statistics
    name: "Durchschnitt AT Temp"
    entity_id: sensor.openweathermap_temperature
    state_characteristic: average_linear
    sampling_size: 60
    max_age:
      hours: 1
    precision: 1
  - platform: statistics
    name: "Durchschnitt AT TempFeel"
    entity_id: sensor.openweathermap_feels_like_temperature
    state_characteristic: average_linear
    sampling_size: 60
    max_age:
      hours: 1
    precision: 1    
  - platform: statistics
    name: "Durchschnitt AT TempFC"
    entity_id: sensor.openweathermap_forecast_temperature
    state_characteristic: average_linear
    sampling_size: 60
    max_age:
      hours: 1
    precision: 1
#  ZusammenfĆ¼hrung der drei Statistikwerte              #
  - platform: min_max
    entity_ids:
      - sensor.Durchschnitt_AT_Temp
      - sensor.Durchschnitt_AT_TempFeel 
      - sensor.Durchschnitt_AT_TempFC
    type: mean
    name: "Durchschnitt AT"
    round_digits: 1
#  Stundenwert per Statistik auf Witterung Ć¼berfĆ¼hren   #
  - platform: statistics
    name: "Witterung AT"
    entity_id: sensor.Durchschnitt_AT
    state_characteristic: average_linear
    sampling_size: 100
    max_age:
      hours: 22
    precision: 1
1 Like

There is a patch for the average integration by a third party link in the github issue. Iā€™m going to switch to that tomorrow but may also try the daiky sensor above with some template sensors for min/max

Thanks ! It works ! :slight_smile: