Mix template and statistics

Hello all,
I’m trying to create my first statistics sensor, but I don’t understand how to properly write it.
In my coniguration.yaml I have

template: !include sensors.yaml

Then in my sensors.yaml I have many sensors, such as

- sensor: 
  - name: "Corrente netta"
    unit_of_measurement: "kW"
    icon: mdi:flash
    device_class: power
    state_class: measurement
    state: >
      {% set consumo = states('sensor.module_din_connesso_1_power') | float(0) %}
      {% set solare = states('sensor.solaredge_current_power') | float(0) %}
      {{ (consumo - solare) | round(3, default=0) }}
    availability: >
      {{ states('sensor.module_din_connesso_1_power') | is_number
      and
      states('sensor.solaredge_current_power') | is_number }}

I want to add an mean value for another sensor, such as

  - platform: statistics
    name: "PM2.5 interno ogni 5 minuti"
    entity_id: sensor.vindriktning_ikea_vindriktning_pm25
    state_characteristic: mean
    max_age:
      minutes: 5

According to the Statistics help page, this should go under the sensors section, but I get this error:

'platform' is an invalid option for 'template'

How does it work when sensor is under template?

Thank you very much!
Fabio

This means that everything inside the sensors.yaml file is part of the Template integration.

template: !include sensors.yaml

The Statistics integration is not part of the Template integration so that’s why it complained when you put the configuration for a Statistics sensor into the configuration for Template entities.

Look in your configuration.yaml file for a sensor: key. It may also have an !include statement redirecting it to a file. That’s where you should put the configuration for the Statistics sensor.

I do not have the sensor key inside the configuration.yaml file, the only one I have is inside the template integration.
Can I add another sensor: key in configuration.yaml even if I already have it inside template?
Thank you!

Yes, because the two keys aren’t related.

Normally you would use something like this:

sensor: !include sensors.yaml

However, you are already using the sensors.yaml file for the Template integration.

template: !include sensors.yaml

So you will need to create and use a different file name. For example:

sensor: !include other_sensors.yaml

Or you can rename the file you’re currently using for the Template integration, like template_sensors.yaml, and use sensors.yaml for all other types of sensor integrations. It’s your choice.

template: !include template_sensors.yaml
sensor: !include sensors.yaml

Another option:

Packages is great if you want to be able to configure entities from multiple integrations in the same file, bundling entities in the way that makes sense to you and your needs.

I followed your suggestion to rename the file!
I now have

sensor:
  - platform: statistics
    name: "PM25 interno ogni 5 minuti"
    entity_id: sensor.vindriktning_ikea_vindriktning_pm25
    state_characteristic: mean
    max_age:
      minutes: 5

template: !include template_sensors.yaml

I find the new “PM25 interno ogni 5 minuti” sensor among the entities, after rebooting a couple times.

Thank you!

1 Like

Should be in configuration.yaml

Yes, my previous code is an extract of configuration.yaml, that now includes both a sensor: key and a template: key with include.

What exactly have you renamed then ?

I renamed from

template: !include sensors.yaml

to

template: !include template_sensors.yaml

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.