Statistics sensor configuration

Hi

I have looked all over the place to figure this out, but somehow can’t manage to get it working. So sorry if it is something obvious I’m missing, but here is it anyway.

I am trying to calculate the average power during the current quarter hour. Not the last 15 minutes, but the minutes 00-15, 15-30, 30-45 and 45-60 from the hour. I want this value since a lot of the pricing of our electricity consumption is based on this value as of last january (Belgium).

I have an imported power sensor that tracks the power consumed from the power net, this is the value that needs to be averaged. To compute this average, I thought of using a statistics sensor to have a continuous 15min running average value, and then snapshot that value every 15/30/45/00 minutes of the hour. Like this:

sensor:
#continuous 15min peak power consumption average
  - platform: statistics
    name: "Average Peak Power"
    entity_id: sensor.power_consumption
    state_characteristic: average_linear
    max_age:
      minutes: 15

I have this in the ‘old’ sensor format since I get errors when trying to include a platform value in the template sensor, which seems quite logical to not work. I have not made it using a helper since that seems to be missing the max_age option, which I obviously need. The power_consumption sensor does show up in the history tab (so it is being recorded). The issue is that this statistics sensor does not show up in the developer tools, or anywhere else for that matter. I only have one ‘sensor’ tag in the entire configuration file, but it is the only sensor in that list, all other sensors are under the template tag.

Then I snapshot this value every quarter hour using this template sensor:

#15min quarter average peak power consumption
  - trigger:
      - platform: time_pattern
        minutes: "/15"
    sensor:
      - name: QuarterHour Average Peak Power
        unit_of_measurement: kW
        state: >-
          {{ states('sensor.average_peak_power') | float(0) / 1000 | round(1) }}

This returns 0, as the states('sensor.average_peak_power') is simply unknown so the default value of zero is returned.

What am I missing here? Does the statistics sensor need to be under the template tag? And if so, how do I format it correctly since I can’t seem to figure it out.

Thanks a lot for your help, this forum has been a great help so far!

There is no “new” sensor format for the statistics sensor platform.

No, it is not a template sensor. It is the statistics integration. You use the platform it provides under the sensor: integration as you showed correctly above.

So the question is, why does this not work?

sensor:
#continuous 15min peak power consumption average
  - platform: statistics
    name: "Average Peak Power"
    entity_id: sensor.power_consumption
    state_characteristic: average_linear
    max_age:
      minutes: 15

Where is this (in which file)?

How many times does sensor: appear in this file?

Are there any relevant errors in Settings → System → Logs ?

This is written in the configuration.yaml file in /config/configuration.yaml

How many times does sensor: appear in this file?

As stated above, this sensor tag only appears once in the entire file.

Are there any relevant errors in Settings → System → Logs ?

No logs of the current setup, I have errors from before while trying to set it up any other way, but with the configuration below it gives no errors when reloading the yaml config.
Do I need to fully restart Home Assistant for this to work? Only a yaml reload, right?

This is the configuration.yaml file stripped of all other template sensors:

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

lovelace:
  resources:
    - url: /local/mini-media-player-bundle.js?v=1.16.2
      type: module
    - url: /local/mini-graph-card-bundle.js?v=0.11.0
      type: module
    - url: /local/swipe-card.js
      type: module


recorder:
  purge_keep_days: 10000
  include:
    domains:
      - sensor
      - switch
      - binary_sensor
      - input_boolean
      - input_select
      - water_heater
      - counter
      - cover
      - number
      - select
  exclude:
    entity_globs:
      - sensor.sun*

python_script:

template:
#15min quarter average peak power consumption
  - trigger:
      - platform: time_pattern
        minutes: "/15"
    sensor:
      - name: QuarterHour Average Peak Power
        unit_of_measurement: kW
        state: >-
          {{ states('sensor.average_peak_power') | float(0) / 1000 | round(1) }}


sensor:
#continuous 15min peak power consumption average
  - platform: statistics
    name: "Average Peak Power"
    entity_id: sensor.power_consumption
    state_characteristic: average_linear
    max_age:
      minutes: 15


Fully restart.

Fully restart.

:man_facepalming: sorry. Why does the yaml reload not work for this?

A post was split to a new topic: Hot tub template sensor problem

You added a new integration. Statistics. Whenever you do that the first time you need to restart home assistant. You can use YAML reload for new template or stats sensors from ow on.

1 Like