Daily maximum during a time period

Hey all, hoping you can help me with something seemingly simple. I’m pretty new with HA and am still trying to figure things out. I’ve got my home energy monitor synced to the HA and have set up a 30 minute running average sensor of my total energy usage called running_average_total_power

What I’m looking for is a way to grab the maximum value from that sensor between the hours of 1pm and 8pm. I tried to make an automation that runs at 8:00pm, but it doesn’t seem happy with running a -platform:statistics under the action: section.

I get the error : “Message malformed: extra keys not allowed @ data[‘action’][0][‘platform’]”

Thanks for your help

alias: Daily Max Demand
description: ''
mode: single
trigger:
  - platform: time
    at: '20:00:00'
condition: []
action:
- platform: statistics
  entity_id: sensor.running_average_total_power
  name: "Demand stats"
  sampling_size: 500
  max_age:
    minutes: 420
- platform: template
  sensors:
    demand_max:
      friendly_name: "demand max"
      value_template: "{{ state_attr('sensor.running_average_total_power', 'max_value') }}"
      unit_of_measurement: "Watts"

The statistics platform goes under the sensor section. You can’t make an automation using an integration configuration. Also, the statistics platform does not allow you to specify a static timeframe. It only allows you to set the max_age in relation to the current time.

So first, configure statistics properly.

sensor:
- platform: statistics
  entity_id: sensor.running_average_total_power
  name: "Demand stats"
  sampling_size: 500
  max_age:
    minutes: 420

Next make a template sensor that updates at 8pm using the created statistics sensor.

template:
- trigger:
  - platform: time
    at: '20:00:00'
  sensor:
  - name: Demand Max
    unique_id: demand_max
    state: "{{ state_attr('sensor.demand_stats', 'max_value') }}"
    unit_of_measurement: W
1 Like

Thanks for the direction. Having a bit of trouble still. I put this in my sensor.yaml and got it to give a check mark in the file editor. When I do check config before restarting i get the following error:

I am sure something is wrong with how i have this oriented or typed out.

Invalid config for [sensor.statistics]: extra keys not allowed @ data['minutes']. Got 420
extra keys not allowed @ data['template']. Got [OrderedDict([('trigger', [OrderedDict([('platform', 'time'), ('at', '20:00:00')])]), ('sensor', [OrderedDict([('name', 'Demand Max'), ('unique_id', 'demand_max'), ('state', "{{ state_attr('sensor.demand_stats', 'max_value') }}"), ('unit_of_measurement', 'W')])])])]
offset None should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F' for dictionary value @ data['max_age']. Got None. (See ?, line ?). 
                  
  - platform: statistics
    entity_id: sensor.running_average_total_power
    name: "Demand stats"
    sampling_size: 500
    max_age:
    minutes: 420
    template:
    - trigger:
      - platform: time
        at: '20:00:00'
      sensor:
      - name: Demand Max
        unique_id: demand_max
        state: "{{ state_attr('sensor.demand_stats', 'max_value') }}"
        unit_of_measurement: W

compare what I wrote to what you wrote. Your yaml format isn’t correct. Everything I wrote should go into configuration.yaml as-is. You’re trying to combine them, incorrectly.

Ok I cleaned up the spacing and have put both of those blocks within sensor.yaml as you typed them. I get the following error now:

Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 12). 

Here is the code from sensor.yaml

- platform: statistics
  entity_id: sensor.running_average_total_power
  name: "Demand stats"
  sampling_size: 500
  max_age:
    minutes: 420
    
- trigger:
  - platform: time
    at: '20:00:00'
  sensor:
  - name: Demand Max
    unique_id: demand_max
    state: "{{ state_attr('sensor.demand_stats', 'max_value') }}"
    unit_of_measurement: W

and here is my configuration.yaml (have not touched this besides adding the include sensor.yaml line previously)


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensor.yaml

again I appreciate your help. I feel quite useless, as this HA stuff is new to me. Back in the day I did a ton of C++ and Java coding, but its been many years and I’m not as smart as I once was.

Well, again, compare what I wrote to what you wrote. What section does the statistics platform belong in? What section does the template sensor belong in? Hint: Both are listed in my example.

Ok, so the statistics go in sensor.yaml, I just put the template part in scripts.yaml. Is that correct?

nope, the section is listed right in my example

Yes to this

no to this

I’ll give you another hint, it rhymes with schmemplate

also another hint… you can add things directly to configuration.yaml

Thank you, its now in schmemplate.yaml

:rofl:

1 Like

is it working? What do you have now?

Yup, just tested it its working perfectly. You were both helpful and entertaining, so thank you.

2 Likes

Since this was discussed, I believe there’s been a change to statistics platform. I’m trying to find the maximum daily solar energy procued in a year. Unfortunately this code only shows the maximum of each day


sensor:
  - platform: statistics
    name: "Max daily solar production over last year"
    entity_id: sensor.symo_10_0_3_m_1_energy_day
    state_characteristic: value_max
    sampling_size: 730
    max_age:
      days: 365
  - platform: statistics
    name: "Date of max daily solar production over last year"
    entity_id: sensor.symo_10_0_3_m_1_energy_day
    state_characteristic: datetime_value_max
    sampling_size: 730
    max_age:
      days: 365

Then displaying on the front end using a template

type: entities
entities:
  - type: custom:template-entity-row
    icon: mdi:solar-power
    name: Max solar production
    state: >-
      {{ (states('sensor.max_daily_solar_production_over_last_year') | float /
      1000) | round(1)}} kWh on {{
      as_timestamp(states('sensor.date_of_max_daily_solar_production_over_last_year'))
      | timestamp_custom('%a %-d-%b-%Y') }}

I see this

The actual maximum (which I can see from the energy dashboard) was on 77.4kWh on 24-Nov-2022. Any help would be appreciated

I solved this, my energy meter updates every 10 seconds, so I had to increase sample size to 3,153,600. Thinking there must be a better way to do this where HA just stores a single number for the max value.