Skip to content
Average Sensor for Home Assistant
Python Shell
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Update environment Jan 2, 2020
custom_components/average Ignore non numeric values (#7) Jan 2, 2020
.gitattributes
.gitignore
.pre-commit-config.yaml Update environment Jan 2, 2020
CONTRIBUTING.md First version Aug 25, 2019
LICENSE.md Added "precision" option Aug 25, 2019
README.md README typography fix. Sep 25, 2019
dev-setup.sh Update environment Jan 2, 2020
example.png First version Aug 25, 2019
hacs.json Added updates tracking Aug 25, 2019
hass-integration-manifest.schema.json
info.md
requirements-dev.txt
requirements.txt Update environment Jan 2, 2020
tracker.json Ignore non numeric values (#7) Jan 2, 2020
update_tracker.py Update environment Jan 2, 2020

README.md

Please ⭐️ this repo if you find it useful

Average Sensor for Home Assistant

GitHub Release GitHub Activity License Requires.io

hacs Project Maintenance

GitHub pull requests Bugs

Community Forum

This sensor allows you to calculate the average state for one or more sensors over a specified period. Or just the average current state for one or more sensors, if you do not need historical data.

Initially it was written special for calculating of average temperature, but now it can calculate average of any numerical data.

Example

What makes this sensor different from others built into HA:

Compare with the min-max sensor:
This sensor in the mean mode produces exactly the same average value from several sensors. But, unlike our sensor, it cannot receive the current temperature data from a weather, climate and water heater entities.

Compare with statistics sensor:
This sensor copes with the averaging of data over a certain period of time. However… 1) it cannot work with several sources at once (and can't receive temperature from weather, climate and water heater entities, like min-max sensor), 2) when calculating the average, it does not take into account how much time the temperature value was kept, 3) it has a limit on the number of values ​​it averages - if by chance there are more values, they will be dropped.

Note:
You can find a real example of using this component in my Home Assistant configuration.

I also suggest you visit the support topic on the community forum.

Breaking changes

  • Since version 1.3.0 the default sensor name is “Average” instead of “Average Temperature”

Installation

  1. Using the tool of choice open the directory (folder) for your HA configuration (where you find configuration.yaml).

  2. If you do not have a custom_components directory (folder) there, you need to create it.

  3. In the custom_components directory (folder) create a new folder called average.

  4. Download all the files from the custom_components/average/ directory (folder) in this repository.

  5. Place the files you downloaded in the new directory (folder) you created.

  6. Restart Home Assistant

  7. Add average sensor to your configuration.yaml file:

    To measure the average current temperature from multiple sources:

    # Example configuration.yaml entry
    sensor:
      - platform: average
        name: 'Average Temperature'
        entities:
          - weather.gismeteo
          - sensor.owm_temperature
          - sensor.dark_sky_temperature

    To measure average temperature for some period:

    # Example configuration.yaml entry
    sensor:
      - platform: average
        name: 'Average Temperature'
        duration:
          days: 1
        entities:
          - sensor.gismeteo_temperature

    or you can combine this variants for some reason.

* * *

I put a lot of work into making this repo and component available and updated to inspire and help others! I will be glad to receive thanks from you — it will give me new strength and add enthusiasm:


Patreon PayPal
or support via Bitcoin or Etherium:
Bitcoin
16yfCfz9dZ8y8yuSwBFVfiAa3CNYdMh7Ts

Configuration Variables

entities:
(list) (Required)
A list of temperature sensor entity IDs.

Note:
You can use weather provider, climate and water heater entities as a data source. For that entities sensor use values of current temperature.

name:
(string) (Optional)
Name to use in the frontend.
Default value: "Average"

start:
(template) (Optional)
When to start the measure (timestamp or datetime).

end:
(template) (Optional)
When to stop the measure (timestamp or datetime).

duration:
(time) (Optional)
Duration of the measure.

precision:
(number) (Optional)
The number of decimals to use when rounding the sensor state.
Default value: 2

Time periods

The average integration will execute a measure within a precise time period. You should provide none, only duration (when period ends at now) or exactly 2 of the following:

  • When the period starts (start variable)
  • When the period ends (end variable)
  • How long is the period (duration variable)

As start and end variables can be either datetimes or timestamps, you can configure almost any period you want.

Duration

The duration variable is used when the time period is fixed. Different syntaxes for the duration are supported, as shown below.

# 15 seconds
duration: 15
# 6 hours
duration: 06:00
# 1 minute, 30 seconds
duration: 00:01:30
# 2 hours and 30 minutes
duration:
  # supports seconds, minutes, hours, days
  hours: 2
  minutes: 30

Examples

Here are some examples of periods you could work with, and what to write in your configuration.yaml:

Last 5 minutes: ends right now, last 5 minutes.

duration:
  minutes: 5

Today: starts at 00:00 of the current day and ends right now.

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

Yesterday: ends today at 00:00, lasts 24 hours.

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

This morning (06:00–11:00): starts today at 6, lasts 5 hours.

start: '{{ now().replace(hour=6).replace(minute=0).replace(second=0) }}'
duration:
  hours: 5

Current week: starts last Monday at 00:00, ends right now.

Here, last Monday is today as a timestamp, minus 86400 times the current weekday (86400 is the number of seconds in one day, the weekday is 0 on Monday, 6 on Sunday).

start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
end: '{{ now() }}'

Last 30 days: ends today at 00:00, lasts 30 days. Easy one.

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

All your history starts at timestamp = 0, and ends right now.

start: '{{ 0 }}'
end: '{{ now() }}'

Note:
The /dev-template page of your home-assistant UI can help you check if the values for start, end or duration are correct. If you want to check if your period is right, just click on your component, the start and end attributes will show the start and end of the period, nicely formatted.

Track updates

You can automatically track new versions of this component and update it by custom-updater (deprecated) or HACS.

For custom-updater to initiate tracking add this lines to you configuration.yaml file:

# Example configuration.yaml entry
custom_updater:
  track:
    - components
  component_urls:
    - https://raw.githubusercontent.com/Limych/ha-average/master/tracker.json
You can’t perform that action at this time.