Skip to content

Loadpoints: track metrics#29143

Open
andig wants to merge 7 commits intomasterfrom
chore/loadpoints
Open

Loadpoints: track metrics#29143
andig wants to merge 7 commits intomasterfrom
chore/loadpoints

Conversation

@andig
Copy link
Copy Markdown
Member

@andig andig commented Apr 15, 2026

A follow-up to #23185, this PR will provide the basis for tracking heatpump consumption during non-boost times.

@naltatis we need to decide under which group and metric name we'd want to do this?

Next step
Per loadpoint chart. Currently grouped.

@andig andig added enhancement New feature or request heating Heating labels Apr 15, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In publishChargeProgress you unconditionally call lp.chargeEnergy.AddEnergy(...); consider guarding this with a nil check so that loadpoints without a chargeMeter (and thus without a collector) don't panic.
  • Creation of the metrics collector in NewLoadpointFromConfig currently aborts loadpoint initialization on error; if metrics are non-critical, consider logging the error and continuing so that a metrics failure does not prevent charger operation.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `publishChargeProgress` you unconditionally call `lp.chargeEnergy.AddEnergy(...)`; consider guarding this with a nil check so that loadpoints without a `chargeMeter` (and thus without a collector) don't panic.
- Creation of the metrics collector in `NewLoadpointFromConfig` currently aborts loadpoint initialization on error; if metrics are non-critical, consider logging the error and continuing so that a metrics failure does not prevent charger operation.

## Individual Comments

### Comment 1
<location path="core/loadpoint.go" line_range="1745-1754" />
<code_context>
 	lp.publish(keys.ChargedEnergy, lp.GetChargedEnergy())
 	lp.publish(keys.ChargeDuration, lp.chargeDuration)
+
+	// update energy, prefer totals
+	var importTotal *float64
 	if api.HasCap[api.MeterEnergy](lp.chargeMeter) {
-		lp.publish(keys.ChargeTotalImport, lp.chargeMeterTotal())
+		if f := lp.chargeMeterTotal(); f > 0 {
+			lp.publish(keys.ChargeTotalImport, f)
+			importTotal = &f
+		}
 	}
+
+	lp.chargeEnergy.AddEnergy(importTotal, nil, lp.chargePower)
 }

</code_context>
<issue_to_address>
**issue (bug_risk):** Guard `lp.chargeEnergy` before calling `AddEnergy` to avoid nil dereference when no charge meter is configured.

`lp.chargeEnergy` is only initialized when `lp.chargeMeter != nil` in `NewLoadpointFromConfig`. For loadpoints without a `chargeMeter`, `lp.chargeEnergy` remains nil, but `publishChargeProgress` still calls `lp.chargeEnergy.AddEnergy(...)`, causing a potential panic. Please add a nil check (or early return) before calling `AddEnergy`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread core/loadpoint.go Outdated
@andig andig mentioned this pull request Apr 15, 2026
6 tasks
Comment thread core/loadpoint.go Outdated

// add collector
if lp.chargeMeter != nil {
me, err := metrics.NewCollector(metrics.Charge, lp.GetTitle())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me. We have to keep in mind, that title is not technically required. When creating a loadpoint via UI you are forced to pick one and I guess very few people dont have loadpoints without title. But we should check that it's not empty.

Titles also dont have to be unique. But I think this is someone we could or even should enforce. At least for creating new loadpoints or changing names. Dont see this as a blocker, it's more an additional step we should take to avoid confusion.

Copy link
Copy Markdown
Member

@naltatis naltatis Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the failing integration tests: uniqueness error

df52070821cfc9a93734c462ad24500b5fa42b26

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the failing integration tests: uniqueness error

naltatis: wo kommen die her, also was sorgt dafür dass da neue metrics angelegt werden?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now creating the collector before loadpoint and passing it. Similar to settings and logs. This way we've access to proper names (lp-1...n for yaml and db:__ for db lp's). Solves the uniqueness problem and provides stable identifiers for later referencing.

@CiNcH83

This comment was marked as off-topic.

@naltatis

This comment was marked as off-topic.

@andig andig added the prio Priority label Apr 22, 2026
Comment thread core/metrics/collector.go
PV = "pv"
Home = "home" // meter and group (virtual measurement)
Battery = "battery"
Charge = "charge"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer loadpoint since this is what we're capturing here.

Comment thread cmd/setup.go Outdated

settings := coresettings.NewConfigSettingsAdapter(log, &conf)

collector, err := metrics.NewCollector(metrics.Charge, cc.Name)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 3 places now where we need to setup the collector which doesn't feel good.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only other option would be to pass name into loadpoint.

This is symmetrical to settings/logger. We might be able to refactor/extract this lp init code to make it more dry (static & dynamic). Not sure of its worth the indirection though ...

Copy link
Copy Markdown
Member

@naltatis naltatis Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just pushed a version with extracted lp init. now we only have one metrics.NewCollector call

Comment thread server/http_config_loadpoint_handler.go Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request heating Heating prio Priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants