How to retrieve data from a Statistics sensor?

I have successfully created a statistics sensor for some temperature data. However I now want to display the max & min values so I used a template to extract the relevant data. But it’s not working. This is what I’ve got:

  - platform: statistics
    name: "Greenhouse Stats"
    entity_id: sensor.greenhouse_temperature
    sampling_size: 10000
    max_age:
      minutes: 1440

  - platform: template
    sensors:
      greenhouse_temp_max:
        value_template: "{{ state_attr('sensor.greenhouse_stats_mean', 'max_value') }}"
      greenhouse_temp_min:
        value_template: "{{ state_attr('sensor.greenhouse_stats_mean', 'min_value') }}"

However both greenhouse_temp_min and greenhouse_temp_max are saying “Unknown” despite having values when I examine the “Greenhouse Stats” entity. There’s no errors/warnings in the logs concerning these entities.

I’m sure there’s some simple config error here I’m not seeing.

Your sensor name is set to

name: "Greenhouse Stats"

So the entity id should be

sensor.greenhouse_stats

but you are trying to extract data from a sensor named

sensor.greenhouse_stats_mean

According to the documentation for Statistics “It exports the mean value as state and the following values as attributes”.

The answers to other questions on here say that there is a _mean suffix:

But I assure you I have tried it without the _mean suffix as well and it didn’t work.

I am trying to replicate your issue but for me it works.

Have a look:
Here’s my yaml entry:

- platform: statistics
  name: "Home Temp Stats"
  entity_id: sensor.home_temperature
  sampling_size: 10000
  max_age:
      minutes: 1440

Here’s the created sensor

Note that there is no

_mean

in the entity_id.

And here’s the output from the template

You can test the template output using the developer-tools.

Also in your config, try to define the type of your sensor

  - platform: template
    sensors:
      greenhouse_temp_max:
        unit_of_measurement: '°C'
        value_template: |
              {{ state_attr('sensor.greenhouse_stats', 'max_value') | float }}

OK, so I cut it down to just one section in the template and it works without the _mean. So added the second one, and now that works as well. I have no idea why it didn’t work before.

I’m happy that it’s working properly, and thank you for your interest. But it’s frustrating that I followed what I could find in the manual and on the forum without any luck. Presumably something has changed in the last few years.

It used to create sensor.object_id_mean as the sensor entity id. It was changed to drop the _mean a few releases ago but the state is still the mean value. So if you saw an example somewhere it could be out of date.

in addition to what Tom said, ALWAYS check the entity exists when you see these types of errors. Dev tools will show the correct entity name for you