Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a trending_towards attribute when period is set #216

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ I put a lot of work into making this repo and component available and updated to
**max**:\
Maximum value of processed values of source sensors.

**trending_towards**:\
The predicted value if monitored entities keep their current states for the remainder of the period. Requires "end" configuration variable to be set to actual end of period and not now().

## 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
5 changes: 3 additions & 2 deletions custom_components/average/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
The Average Sensor.
"""The Average Sensor.

For more details about this sensor, please refer to the documentation at
https://github.com/Limych/ha-average/
Expand Down Expand Up @@ -50,6 +49,7 @@
ATTR_COUNT: Final = "count"
ATTR_MIN_VALUE: Final = "min_value"
ATTR_MAX_VALUE: Final = "max_value"
ATTR_TRENDING_TOWARDS: Final = "trending_towards"
#
ATTR_TO_PROPERTY: Final = [
ATTR_START,
Expand All @@ -60,6 +60,7 @@
ATTR_COUNT,
ATTR_MAX_VALUE,
ATTR_MIN_VALUE,
ATTR_TRENDING_TOWARDS,
]


Expand Down
24 changes: 24 additions & 0 deletions custom_components/average/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
ATTR_MIN_VALUE,
ATTR_START,
ATTR_TO_PROPERTY,
ATTR_TRENDING_TOWARDS,
CONF_DURATION,
CONF_END,
CONF_PERIOD_KEYS,
Expand Down Expand Up @@ -142,6 +143,7 @@ class AverageSensor(SensorEntity):
ATTR_COUNT,
ATTR_MAX_VALUE,
ATTR_MIN_VALUE,
ATTR_TRENDING_TOWARDS,
}
)

Expand All @@ -166,11 +168,13 @@ def __init__(
self._precision = precision
self._undef = undef
self._temperature_mode = None
self._actual_end = None

self.sources = expand_entity_ids(hass, entity_ids)
self.count_sources = len(self.sources)
self.available_sources = 0
self.count = 0
self.trending_towards = None
self.min_value = self.max_value = None

self._attr_name = name
Expand Down Expand Up @@ -387,6 +391,8 @@ async def _async_update_period(self): # pylint: disable=too-many-branches
if start > end:
start, end = end, start

self._actual_end = end

if start > now:
# History hasn't been written yet for this period
return
Expand Down Expand Up @@ -443,15 +449,18 @@ async def _async_update_state(
p_start, p_end = p_period

# Convert times to UTC
now = dt_util.as_utc(now)
start = dt_util.as_utc(start)
end = dt_util.as_utc(end)
actual_end = dt_util.as_utc(self._actual_end)
p_start = dt_util.as_utc(p_start)
p_end = dt_util.as_utc(p_end)

# Compute integer timestamps
now_ts = math.floor(dt_util.as_timestamp(now))
start_ts = math.floor(dt_util.as_timestamp(start))
end_ts = math.floor(dt_util.as_timestamp(end))
actual_end_ts = math.floor(dt_util.as_timestamp(actual_end))
p_start_ts = math.floor(dt_util.as_timestamp(p_start))
p_end_ts = math.floor(dt_util.as_timestamp(p_end))

Expand All @@ -462,6 +471,7 @@ async def _async_update_state(

self.available_sources = 0
values = []
current_values = []
self.count = 0
self.min_value = self.max_value = None

Expand Down Expand Up @@ -535,6 +545,7 @@ async def _async_update_state(
last_elapsed = end_ts - last_time
value += last_state * last_elapsed
elapsed += last_elapsed
current_values.append(last_state)
AnderzL7 marked this conversation as resolved.
Show resolved Hide resolved

if elapsed:
value /= elapsed
Expand All @@ -551,6 +562,19 @@ async def _async_update_state(
else:
self._attr_native_value = None

if current_values and values:
current_average = round(
sum(current_values) / len(current_values), self._precision
)
if self._precision < 1:
current_average = int(current_average)
part_of_period = (now_ts - start_ts) / (actual_end_ts - start_ts)
to_now = self._attr_native_value * part_of_period
to_end = current_average * (1 - part_of_period)
self.trending_towards = to_now + to_end

_LOGGER.debug("Current trend: %s", self.trending_towards)

_LOGGER.debug(
"Total average state: %s %s",
self._attr_native_value,
Expand Down