Skip to content

Commit

Permalink
Add calculation of median, mode and last values. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Jan 12, 2021
1 parent c7abea2 commit b9f0a4e
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 69 deletions.
63 changes: 53 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![GitHub Release](https://img.shields.io/github/tag-date/Limych/ha-average?label=release&style=popout)](https://github.com/Limych/ha-average/releases)
[![GitHub Activity](https://img.shields.io/github/commit-activity/y/Limych/ha-average.svg?style=popout)](https://github.com/Limych/ha-average/commits/master)
[![License](https://img.shields.io/badge/license-Creative_Commons_BY--NC--SA_License-lightgray.svg?style=popout)](LICENSE.md)
![Requires.io](https://img.shields.io/requires/github/Limych/ha-average)
[![Requires.io](https://img.shields.io/requires/github/Limych/ha-average)(https://requires.io/github/Limych/ha-average/requirements/)

[![hacs](https://img.shields.io/badge/HACS-Default-orange.svg?style=popout)][hacs]
![Project Maintenance](https://img.shields.io/badge/maintainer-Andrey%20Khrolenok%20%40Limych-blue.svg?style=popout)
Expand Down Expand Up @@ -36,7 +36,10 @@ I also suggest you [visit the support topic][forum-support] on the community for

## Breaking changes

* Since version 1.3.0 the default sensor name is “Average” instead of “Average Temperature”
* Since version ???:
* the default sensor name is “Mean Sensor” instead of “Average”;
* the `precision` configuration variable is renamed to `round_digits` for compatibility with `min_max` sensor;
* ~~Since version 1.3.0 the default sensor name is “Average” instead of “Average Temperature”~~

## Known Limitations and Issues

Expand Down Expand Up @@ -109,10 +112,15 @@ I put a lot of work into making this repo and component available and updated to
> **_Note_**:\
> You can use groups of entities as a data source. These groups will be automatically expanded to individual entities.
**type**:\
_(string) (Optional)_\
The type of sensor: `min`, `max`, `last`, `mean`, `median` or `mode`.\
_Default value: "mean" (for historical reasons)_

**name**:\
_(string) (Optional)_\
Name to use in the frontend.\
_Default value: "Average"_
_Default value: type + " Sensor" (ex. "Mean Sensor")_

**start**:\
_(template) (Optional)_\
Expand All @@ -126,19 +134,22 @@ I put a lot of work into making this repo and component available and updated to
_(time) (Optional)_\
Duration of the measure.

**precision**:\
**round_digits**:\
_(number) (Optional)_\
The number of decimals to use when rounding the sensor state.\
_Default value: 2_

**precision**:\
**Deprecated** use `round_digits` instead.

**process_undef_as**:\
_(number) (Optional)_\
Process undefined values (unavailable, sensor undefined, etc.) as specified value.\
\
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.

> **_Note_**:\
> This parameter does not affect the calculation of the count, min and max attributes.
> This parameter does not affect the calculation of the `count`, `min`, `max` and `last` attributes.

### Average Sensor Attributes

Expand All @@ -148,24 +159,45 @@ I put a lot of work into making this repo and component available and updated to
**end**:\
Timestamp of the end of the calculation period (if period was set).

**sources**:\
**sensors**:\
Total expanded list of source sensors.

**count_sources**:\
**count_sensors**:\
Total count of source sensors.

**available_sources**:\
**available_sensors**:\
Count of available source sensors (for current calculation period).

**count**:\
Total count of processed values of source sensors.

**min**:\
**min_value**:\
Minimum value of processed values of source sensors.

**max**:\
**min_entity_id**:\
Entity ID of source sensor with minimum value of processed values.

**max_value**:\
Maximum value of processed values of source sensors.

**max_entity_id**:\
Entity ID of source sensor with maximum value of processed values.

**mean**:\
Arithmetic mean of processed values of source sensors.

**median**:\
Median (middle value separating the greater and lesser halves of a data set) of processed values of source sensors.

**mode**:\
Mode (most frequent value in a data set) of processed values of source sensors.

**last**:\
Last produced value of processed values of source sensors.

**last_entity_id**:\
Entity ID of source sensor with last produced value of processed values.

## 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:
Expand Down Expand Up @@ -277,6 +309,17 @@ end: '{{ now() }}'

You can automatically track new versions of this component and update it by [HACS][hacs].

## Troubleshooting

To enable debug logs use this configuration:
```yaml
# Example configuration.yaml entry
logger:
default: error
logs:
custom_components.average: debug
```
... then restart HA.
## Contributions are welcome!

This is an active open-source project. We are always open to people who want to
Expand Down
44 changes: 34 additions & 10 deletions custom_components/average/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
from datetime import timedelta

# Base component constants
from homeassistant.components.min_max.sensor import (
ATTR_MEDIAN,
ATTR_MIN_VALUE,
ATTR_MAX_VALUE,
ATTR_COUNT_SENSORS,
ATTR_MEAN,
ATTR_MAX_ENTITY_ID,
ATTR_MIN_ENTITY_ID,
ATTR_LAST,
ATTR_LAST_ENTITY_ID,
)

VERSION = "dev"
ISSUE_URL = "https://github.com/Limych/ha-average/issues"

Expand All @@ -17,26 +29,38 @@
CONF_PERIOD_KEYS = [CONF_START, CONF_END, CONF_DURATION]
CONF_PROCESS_UNDEF_AS = "process_undef_as"

DEFAULT_NAME = "Average"

ATTR_START = "start"
ATTR_END = "end"
ATTR_SOURCES = "sources"
ATTR_COUNT_SOURCES = "count_sources"
ATTR_AVAILABLE_SOURCES = "available_sources"
ATTR_SENSORS = "sensors"
ATTR_AVAILABLE_SENSORS = "available_sensors"
ATTR_COUNT = "count"
ATTR_MIN_VALUE = "min_value"
ATTR_MAX_VALUE = "max_value"
ATTR_MODE = "mode"

ATTR_TO_PROPERTY = [
ATTR_START,
ATTR_END,
ATTR_SOURCES,
ATTR_COUNT_SOURCES,
ATTR_AVAILABLE_SOURCES,
ATTR_SENSORS,
ATTR_COUNT_SENSORS,
ATTR_AVAILABLE_SENSORS,
ATTR_COUNT,
ATTR_MAX_VALUE,
ATTR_MAX_ENTITY_ID,
ATTR_MIN_VALUE,
ATTR_MIN_ENTITY_ID,
ATTR_MEAN,
ATTR_MEDIAN,
ATTR_MODE,
ATTR_LAST,
ATTR_LAST_ENTITY_ID,
]

UPDATE_MIN_TIME = timedelta(seconds=20)

SENSOR_TYPES = {
ATTR_MEAN: "mean",
ATTR_MIN_VALUE: "min",
ATTR_MAX_VALUE: "max",
ATTR_MEDIAN: "median",
ATTR_MODE: "mode",
ATTR_LAST: "last",
}
Loading

0 comments on commit b9f0a4e

Please sign in to comment.