Skip to content

Commit

Permalink
Merge pull request #216 from AnderzL7/codespace-congenial-trout-4v5v5…
Browse files Browse the repository at this point in the history
…9rprjw27p7q

Adds a trending_towards attribute when period is set
  • Loading branch information
Limych authored Oct 6, 2024
2 parents 51e4291 + 9503c38 commit 9ef41cd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,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
2 changes: 2 additions & 0 deletions custom_components/average/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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 +61,7 @@
ATTR_COUNT,
ATTR_MAX_VALUE,
ATTR_MIN_VALUE,
ATTR_TRENDING_TOWARDS,
]


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

Expand All @@ -176,11 +178,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 @@ -399,6 +403,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 @@ -453,15 +459,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 @@ -474,6 +483,8 @@ async def _async_update_state(
values = []
self.count = 0
self.min_value = self.max_value = None
last_values = []


# pylint: disable=too-many-nested-blocks
for entity_id in self.sources:
Expand All @@ -489,6 +500,8 @@ async def _async_update_state(

value = 0
elapsed = 0
trending_last_state = None


if self._period is None:
# Get current state
Expand Down Expand Up @@ -549,12 +562,16 @@ async def _async_update_state(
elapsed += last_elapsed
if elapsed:
value /= elapsed
trending_last_state = last_state

_LOGGER.debug("Historical average state: %s", value)

if isinstance(value, numbers.Number):
values.append(value)
self.available_sources += 1

if isinstance(trending_last_state, numbers.Number):
last_values.append(trending_last_state)

if values:
self._attr_native_value = round(sum(values) / len(values), self._precision)
Expand All @@ -563,6 +580,21 @@ async def _async_update_state(
else:
self._attr_native_value = None

if last_values:
current_average = round(
sum(last_values) / len(last_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
else:
self.trending_towards = None

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

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

0 comments on commit 9ef41cd

Please sign in to comment.