Platform statistics: selectable period

In Home Assistant Lovelace I want to list the energy by aparatus during a selectable period.

In Lovelace I have a period selector and a list of aparatus:

type: grid
square: false
columns: 1
cards:
  - type: entities
    title: Select energy period
    entities:
      - entity: input_number.energy_period
        name: ' '
  - type: entities
    title: Energy over selected period
    entities:
      - entity: sensor.energy_boiler_period
        name: Boiler energy
      - entity: sensor.etc. ...

What works is a pre-defined period (age in days).

- platform: statistics
    name: "Energy Boiler period calculation"
    unique_id: energy_boiler_period_calculation
    entity_id: sensor.ts_boiler_energy
    state_characteristic: 'change'
    max_age:
      days: 7
    precision: 2

Where I fail to code is a selectable period.

  - platform: statistics
    name: "Energy Boiler period calculation"
    unique_id: energy_boiler_period_calculation
    entity_id: sensor.ts_boiler_energy
    state_characteristic: 'change'
    max_age:
      days: "{{ states('input_number.energy_period')|float(0) }}"
    precision: 2

Developer tools error message:

Invalid config for [sensor.statistics]: expected float for dictionary value @ data['max_age']['days']. Got "{{ states('input_number.energy_period')|float(0) }}". (See ?, line ?).

I have tried my homework, but do not succeed.
Any help will be highly appreciated!

You could use utility meter helper to display energy usage of device for given period.

Dear Daniel,
Apologies in case my question was not specified well enough.

What I understand from the Utility Meter Helper documentation is how to collect energy history during a predefined period. If this is a misconception, can you please elaborate how to make this possible for a user selectable period ?
My question is not about collecting energy history (my plugs successfully monitor them), my question is about how to retrieve the energy consumed in kWh, for each apparatus separately from the plug sensors, for a user selectable period, during a number of past ‘age:’ days.
In other words my dashboard has an input number card to specify the number of past days (any number of days between 1 and 365). Depending on the input I want to read the total kWh for that number of past days.

As I wrote, I can perform this for a predefined number of days in the patform statistics config.
My problem is that I do not succeed to calculate the energy on basis of the age as typed in the dashboard card. Replacing a fixed number (e.g. 7) by a selectable number of days (any number between 1 - 365) does not work. What I tried in yaml in the platform statistics is the problem:

max_age:
  days: "{{ states('input_number.energy_period')|float(0) }}"

Hope this better explains my question.
Again, any help is highly appreciated.

Ok I understand what you want to do. You want calculation of energy consumption by device for a given period of time. Period of time for calculation is a day.
I don’t know thr answer because I never did that.
But what I done is gas consumption calculation for energy that I can add to energy dashboard.
It calculates gas consumption per day based on furnace running time. In my point of view if this is working then adding a user specific time period is matter of summing up days.
I don’t know will this helps you but this is what I done for gas consumption
Edit:
I done this using chatgpt. Ai done everything wrong, every piece of code it wrote was wrong, but it gave me explanation what this should do. Examples and explanation from it helped me to understand what to do. Reading doc I was able to write it on my own.

Dear all,
(Thank you Daniel, but I am still stuck…)
Question remains: how do I replace a number in platform: statistics by yaml code of an input_number ? in my case the number of days?

- platform: statistics
    name: "Energy Boiler period calculation"
    unique_id: energy_boiler_period_calculation
    entity_id: sensor.ts_boiler_energy
    state_characteristic: 'change'
    max_age:
      days: 7
    precision: 2

I have tried, but failed:

    max_age:
      days: "{{ states('input_number.energy_period')|float(0) }}"

Developer tools error message:

Invalid config for [sensor.statistics]: expected float for dictionary value @ data['max_age']['days']. Got "{{ states('input_number.energy_period')|float(0) }}". (See ?, line ?).

Any help is highly appreciated.

That field cannot use a template.

I think what you want to do here will be hard in HA. The closest native option will be to decide on a lowest level of granularity, e.g. hourly and then to use a utility meter. Add a stats class entry for the resulting meter to your customize.yaml. You can then use something like the ApexChart card to aggregate it at higher levels, but you won’t be able to make the aggregations dynamic and UI controllable.

What you want to do will work best by exporting the data to InfluxDB and visualising things with Grafana.

Have a look at this custom card:

Thank you all for pointing me in the right direction.
I overlooked the last paragraph ‘Devices energy graph’ of the Home Assistant documentation ‘Energy Cards’, where is being explained that: ‘The devices energy graph show the energy usage per device, it is sorted by usage.’ (Energy Cards - Home Assistant)
A great - HA standard! - solution that fully answers my question!