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

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

AnderzL7
Copy link

@AnderzL7 AnderzL7 commented Jan 8, 2024

Breaking change

Proposed change

This adds a new trending_towards attribute that has the value of the expected average at the end of the period if the monitored entities keeps their current states. This is calculated by the following script block where current_average is the current average of the monitored entities:

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

This new attribute can be used to predict what the resulting average of this period will be or in my case to programmatically limit my power usage below a limit i have set by using automations.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to an this integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

# Example configuration.yaml
- platform: average
  name: Average power this hour
  entities:
    - sensor.emi_power
  start: "{{ now().replace(minute=0, second=0, microsecond=0) }}"
  end: "{{ now().replace(minute=59, second=59, microsecond=999) }}"
  unique_id: "average_power_h_hacs-2024-01"

Additional information

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • The code has been formatted using Black (black --fast custom_components)

If user exposed functionality or configuration variables are added/changed:

  • Documentation added/updated to README.md

custom_components/average/sensor.py Outdated Show resolved Hide resolved
@AnderzL7 AnderzL7 requested a review from Limych March 7, 2024 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a trend attribute for sensors with a period
2 participants