Skip to content

Prepared report templates - #2300

Open
Flix6x wants to merge 10 commits into
feat/2288-report-automationsfrom
feat/2288-report-templates
Open

Prepared report templates#2300
Flix6x wants to merge 10 commits into
feat/2288-report-automationsfrom
feat/2288-report-templates

Conversation

@Flix6x

@Flix6x Flix6x commented Jul 11, 2026

Copy link
Copy Markdown
Member

Description

Defining a report means writing reporter configuration, and writing it from nothing is the step that
stops people defining reports at all. FlexMeasures now ships prepared report templates: a reporter
class, a complete configuration and a parameters skeleton, ready to fill in and run.

Two templates, packaged under flexmeasures/data/templates/reports/ and shipped in the wheel.
energy-costs uses the ProfitOrLossReporter to report energy costs over the reporting window, from
a power or energy sensor and a consumption price sensor, as positive values. self-consumption uses
the PandasReporter to report the share of produced energy consumed on-site, from a production and a
consumption sensor: the elementwise minimum of the two, averaged per reporting period, over average
production.

Each template is complete except for the sensors. It carries a name, a description, the reporter,
a working configuration, and a parameters skeleton whose sensors are FILL_IN placeholders. It also
carries a recommended rolling window — start-offset: -1D,DB, end-offset: DB — which is what makes
a template useful to a recurring report rather than only a one-off.

Discovering them. flexmeasures show report-templates lists the templates with their reporter and
description; --name prints one in full, as commented YAML, ready to redirect to a file and edit.

Using them. --template on flexmeasures add report and on
flexmeasures add automation --type reporting loads the template's reporter, configuration and
parameters as defaults:

flexmeasures show report-templates --name self-consumption > my-report.yml
# fill in the FILL_IN sensor ids, then:
flexmeasures add automation --asset 3 --name "Daily self-consumption" \
  --cron "0 1 * * *" --type reporting --template self-consumption --parameters my-report.yml

What the user writes always wins, at a deliberate granularity. An explicit --reporter is kept
over the template's. Top-level keys of a --config or --parameters file override the template's.
Timing fields — start, end and the offsets, whether in the parameters file or given as CLI
options — replace the template's recommended window as a group, so a half-overridden window can
never be assembled out of two different intentions.

Placeholders left unfilled abort with a validation error naming the exact fields still holding
FILL_IN, rather than failing later inside the reporter with something less legible. Combining
--template with --source is refused, since the data source already determines the reporter and
its configuration, and --template on a non-report automation is refused too.

  • Added changelog item in documentation/changelog.rst

How to test

See the manual test walkthrough in the PR comments.

pytest flexmeasures/cli/tests/test_report_templates.py

Eight tests. Both templates' configurations, and their filled-in parameter skeletons, are validated
against their reporter's own schemas, so a template that drifts from the reporter it names fails here
rather than in a user's terminal. show report-templates is checked to list both templates and to
print valid YAML for each. The end-to-end test fills in self-consumption's sensors, creates the
automation, runs it, and checks the stored daily self-consumption shares. The error paths are covered
too: unfilled placeholders, an unknown template, --template on a non-report automation, --template
combined with --source, and an override file that is not a mapping. One test pins what
self-consumption means with no production at all, which is undefined rather than zero.

Further improvements

  • Two templates is a starting set rather than a catalogue. The mechanism is the point here; further
    templates are cheap to add once its shape is agreed.
  • Templates are packaged with FlexMeasures. Letting a plugin contribute its own would follow the same
    discovery path, and is not attempted here.
  • The templates were verified to be present in the built wheel — hatchling ships everything under the
    flexmeasures package — so no MANIFEST or package-data change was needed.

Related items

Part of the automations story #2334, and of #2288. Stacked on #2297, which it needs for
--type reporting.


Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or another incompatible license.

Hosts can now discover and use ready-made report definitions (reporter
class + complete config + parameters skeleton) instead of authoring
YAML from scratch:

- Two templates ship in flexmeasures/data/templates/reports:
  energy-costs (ProfitOrLossReporter) and self-consumption
  (PandasReporter), each with FILL_IN sensor placeholders and a
  recommended rolling reporting window for recurring use.
- `flexmeasures show report-templates` lists them; `--name <name>`
  prints the full YAML (with comments) to pipe to a file.
- `flexmeasures add report` and `flexmeasures add automation --type
  reports` accept `--template <name>`, using the template as defaults
  under any --config/--parameters files and other options (user keys
  win; user-provided timing fields replace the template's window).
- Unfilled placeholders produce a clear validation error listing the
  fields to fill in.

Part of #2288

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Rbix8k1JfeUWNXEmHEZVpX
Flix6x and others added 9 commits July 11, 2026 22:26
Context:
- PR #2300 is stacked on #2297, whose automation, timezone, permission, and CLI validation changes advanced after the template branch diverged.

Change:
- Merge the updated report-automation branch while preserving prepared-template commands and combining them with the current automation CLI validation and output-scope invariant.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- Combining a prepared template with an existing data source silently discarded the template reporter configuration, while non-object override files crashed during dictionary merging.

Change:
- Make source and template selection mutually exclusive and route report override files through the shared mapping validator so invalid YAML shapes produce actionable CLI errors.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- The template roundtrip did not exercise output-scope enforcement, competing source selection, malformed override files, or reporting periods without production.

Change:
- Target the automation's actual output asset and add regressions proving ambiguous inputs are rejected, override files require mappings, and zero production leaves self-consumption undefined.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- Prepared templates need explicit guidance for competing stored sources and for self-consumption periods where the denominator is zero.

Change:
- Document that templates cannot accompany an existing source and that zero production yields an undefined, missing self-consumption value.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…plates

Context:
- Picking up the review changes from #2290 and the automation work below this branch.
- Both sides had a check that refuses forecaster options on a schedules automation, in two generations: this branch still guessed from configuration values that differ from their default, while upstream only counts options actually given on the command line.

Change:
- Kept this branch's report template handling and upstream's stricter check, rather than either side wholesale, which would have dropped one or reverted the other.
- Regenerated the OpenAPI specs.

Signed-off-by: F.N. Claessen <felix@seita.nl>
…to HEAD

Signed-off-by: F.N. Claessen <felix@seita.nl>
…to HEAD

Signed-off-by: F.N. Claessen <felix@seita.nl>
…plates

Follows the automation type rename that came up the stack: `--type reports` is now `--type reporting`, in the template CLI options, their tests, `flexmeasures show report-templates` and the reporting chapter.

Kept both sides of `flexmeasures add automation`: the template resolution and its placeholder check, and the dry-run guard the base added.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@BelhsanHmida
BelhsanHmida marked this pull request as ready for review September 11, 2026 11:43
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.

2 participants