-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add sensor summation to area card #20749
base: dev
Are you sure you want to change the base?
Conversation
Firstly thanks very much, Area level summation is really needed!! My question is wit big, but should we be doing this in the frontend, or should we do this in the new backend "areas" that we then pass to the frontend? If we do it in the backend then it will also be usable for many purposes |
Thanks for the feedback! I haven't dived too deeply into the backend code yet, but the ideal solution may well involve both backend and frontend changes. Some kind of relation between a sensor class and a default function would be handy, and that feels like a backend task. The min/max/avg/sum/etc. operations themselves are effectively display formatters and seem like good candidates for frontend. The more I think about how I want to use these cards, the more I think it would be nice to have the ability to specify one or more statistical display functions for a given sensor class within an area card. Might be nice to see the min/max temperature on one area card, for example. It feels like there's some opportunity to use helpers here. That's what I usually reach for if I need to sum a bunch of sensors. |
If you feel the PR is ready for review, be sure to mark it as ready for review. It's still in draft status right now. |
Has this PR been abandoned? I would very much like to have this fix in place. Happy to help if I can. |
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe changes introduced in the pull request enhance the handling of device classes within the Changes
Assessment against linked issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
33f2968
to
216f7fa
Compare
const sum = values.reduce( | ||
(total, entity) => total + Number(entity.state), | ||
0 | ||
); | ||
return `${formatNumber(sum / values.length, this.hass!.locale, { | ||
const displayValue = deviceClass | ||
? this._deviceClasses[deviceClass].default_display_function(values) | ||
: ""; | ||
return `${formatNumber(displayValue, this.hass!.locale, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change specifically addresses the issue at hand (all Area card numeric sensor values were averaged). All other changes are ancillary to these lines.
d3d4a9e
to
6ce96c8
Compare
6ce96c8
to
3989cdc
Compare
@coderabbitai reassess how this PR addresses the linked issue. Pay particular attention to src/panels/lovelace/cards/hui-area-card.ts L122-124, L284-287. |
Tip For best results, initiate chat on the files or code changes. @seevee: After reassessing how this PR addresses the linked issue (#20014), I can confirm that it effectively resolves the problem of area cards averaging power and energy sensor values when they should be summing them. In
Therefore, the implementation you've provided directly addresses the issue by summing the values of power and energy sensors in area cards, ensuring accurate representation of total consumption. Analysis chainScripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Extract lines 122-124 and 284-287 from src/panels/lovelace/cards/hui-area-card.ts
sed -n '122,124p;284,287p' src/panels/lovelace/cards/hui-area-card.ts
Length of output: 367 |
@coderabbitai edit your initial review to reflect the conclusion from your last comment. |
@seevee: I've updated my review to reflect the conclusion from my last comment. After re-evaluating the changes, I confirm that this PR effectively addresses the linked issue by summing power and energy sensor values in area cards, ensuring accurate representation of total consumption. Thank you for your contribution! Actions performedComments resolved. |
@coderabbitai The walkthrough section in your initial comment still shows that the linked issue is not directly addressed. Please amend. |
@seevee: Thank you for pointing that out. I've amended the walkthrough section in my initial review to accurately reflect that your PR directly addresses the linked issue. Please let me know if there's anything else I can assist with. Actions performedComments resolved. |
It's not the best bot and it will keep talking to you whenever it feels to. |
This PR looks good but it feels like a start of adding more and more logic into this card.
I've been going through your PR and this makes perfect sense of my thinking as well. We do have a helper that allows min/max/mean/avg/sum and more. The problem that remains is that the helper requires a set of entities, while the area card starts to calculate that automatically based on a sensor device class. |
Perhaps it would be better not to have this unique by-device-class statistical behavior at all? Instead just specify one specific entity in the area per device class to be displayed. That way, if you want an average, sum, min, max, etc. you would create a stats helper and point the card at it. |
Thank you both for reviewing and brainstorming with me. I think we have a consensus that the approach in this implementation is likely not the best path forward. I agree that adding more logic like this to the Area card feels like the wrong move, and that either replacing or supplementing the existing logic in favor of helpers and entities seems indicated. My assumption is that this is going to require a bigger refactor, but I'm still new to this codebase and I need to deepen my understanding of how helpers and areas interact as well as the overall development approach before I form an implementation plan. I'm currently reviewing documentation and searching the discord to this end. Some assumptions:
Some open questions:
|
It's going to be a big refactor indeed. Having some experience with this before, I would suggest to think about it first and then get a sort of approval from the people that maintain Home Assistant before you start to implement. Adding cards to a dashboard or configuring cards is not really going through a config flow right now. In the past, it was accepted to create helpers from an automation (no longer need to go to helpers page to create one). I would advise that for this one as well, initiating a helper config flow from the area card config. I would say that the logic of min_max can be reused, but we're working on a different set of entities. One that can change dynamically whenever entities are added to the area or removed from. Given that, it sounds more suitable to have a "area min_max helper" (that name can be refined). It would have a prefilled area picker in the config flow, along with the sensor class and a statistical option (min, max, ...). |
Proposed change
Area cards currently average the values of all sensors within the given area. These changes provide a mechanism for specifying the type of function to use for a given sensor type - A further improved solution might allow for selecting any statistical function (mean, min, max, avg, sum) for any given sensor type.
Type of change
Example configuration
I tested this by adding two dummy power sensors and two dummy temperature sensors (all with different values) then assigning them to a single Area. Adding the area card to a dashboard view showed the average temperature and the summed power.
Additional information
Checklist
If user exposed functionality or configuration variables are added/changed:
Summary by CodeRabbit
New Features
Bug Fixes