Statistics sensors in dedicated configuration file

Hello,

In my configuration.yaml , I created this statistics sensor :

sensor:
  - platform: statistics
    name: "Wind max force 10min"
    entity_id: sensor.rdc_anemometre_wind_strength
    state_characteristic: value_max
    max_age:
      minutes: 10

I seems to work well, but I want to move it into a dedicated configuration file.

Then in configuration.yaml, I replaced the sensor by this line :

sensor: !include_dir_list sensors/

And I created a file sensors/statistics.yaml. At this step : no sensor but no error.

To add sensor in this file, I tried several things, without success :

platform: statistics
  name: "Wind max force 10min"
  unique_id: stats.windmax
  entity_id: sensor.rdc_anemometre_wind_strength
  state_characteristic: value_max
  max_age:
    minutes: 10

→ Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/sensors/statistics.yaml”, line 2, column 7

  - platform: statistics
    name: "Wind max force 10min"
    unique_id: stats.windmax
    entity_id: sensor.rdc_anemometre_wind_strength
    state_characteristic: value_max
    max_age:
        minutes: 10

→ Invalid config for ‘sensor’ at sensors/statistics.yaml, line 1: expected a dictionary ‘’, got [{‘platform’: ‘statistics’, ‘name’: ‘Wind max force 10min’, ‘unique_id’: ‘stats.windmax’, ‘entity_id’: ‘sensor.rdc_anemometre_wind_strength’, ‘state_characteristic’: ‘value_max’, ‘max_age’: {‘minutes’: 10}}]

For sure a silly mistake I did, what’s wrong ?

Thank you.

You were close. There is an example here. Yours should be:

platform: statistics
name: "Wind max force 10min"
unique_id: stats.windmax
entity_id: sensor.rdc_anemometre_wind_strength
state_characteristic: value_max
max_age:
  minutes: 10

Note that you can only have the one sensor in this file. You have to create a new file (with any filename you want) for each new sensor.

Yes it worked ! But I thought to be able to add all statistics in a single file.

With your comment I understood I used the wrond include.

With !include_dir_list I could add 2 statistics in same file :

- platform: statistics
  name: "Wind max force 10min"
  unique_id: stats.windmax
  entity_id: sensor.rdc_anemometre_wind_strength
  state_characteristic: value_max
  keep_last_sample: true
  max_age:
    minutes: 10
    
- platform: statistics
  name: "Wind max force 24h"
  unique_id: stats.windmax2
  entity_id: sensor.rdc_anemometre_wind_strength
  state_characteristic: value_max
  max_age:
    hours: 24

I added variable keep_last_sample to avoid value “unknown” when the sensor value didn’t change in last 10 minutes

  keep_last_sample: true