How do I get the TIME for the max_value in a statistic

I can get the max_value attribute from the statistics of a temperature sensor, but how do I get the time that the actual max_value occurred? I know the times are stored in the database because I can see them in a popup on the chart as I move the cursor over the chart line.

2 Likes

Very easy if you want to display the info.
I use the custom mini graph card with the extrema flag.

type: custom:mini-graph-card
entities:
  - sensor.purpleair_aqi_a
unit: AQI
name: Air Quality (24hr)
icon: none
hour24: true
show:
  fill: true
  legend: false
  labels: false
  name: true
  extrema: true
  average: true
  points: false
  name_adaptive_color: true
  icon_adaptive_color: true
  show_legend: false
font_size: null
line_width: 3
points_per_hour: 4
hours_to_show: 24
color_thresholds_transition: hard
color_thresholds:
  - value: 0
    color: '#68FF43'
  - value: 50.5
    color: '#FFFF55'
  - value: 100.5
    color: '#EF8533'
  - value: 150.5
    color: '#EA3324'
  - value: 200.5
    color: '#8C1A4B'
  - value: 300.5
    color: '#731425'

If you want to pull that data out for use in an automation, then this won’t help.

You could look at min/max or statistics template sensors that might get you closer.

Glenn - I’m already using a template to return the max_value; I should have included the configuration :

  - platform: statistics
    name: 'attic_temp_stats'
    entity_id: sensor.h5174_c58b_temp
    sampling_size: 1600
    max_age:
      minutes: 1440

    attic_max:
      friendly_name: 'Attic Max'
      unit_of_measurement: '°F'
      value_template: "{{ state_attr('sensor.attic_temp_stats', 'max_value') | round(0) }}"

I’ve tried variations of as_timestamp, like below, but it doesn’t work:

    time_attic_max:
      friendly_name: 'Time Attic Max'
      value_template: "{{ as_timestamp('sensor.attic_temp_stats', 'max_value') | round(0) }}"

Try this in the Template Editor

{{ states.sensor.attic_max.last_changed }}

I’m assuming you left out pasting the - platform: template etc… from your attic_max template sensor as the way it is shown above won’t work.

Yes, I left out the platform: template, etc. Like I said, I do get the maximum temperature.

I just tried your suggestion and although it does return a time (in UTC), but for some reason it is the last time the sensor reported a temperature (basically current time), not the time of max_value.

It should report:

Time the state changed in the state machine in UTC time

for the template sensor.

Has the sensor reached it’s max value yet, or is it still increasing?

Yes, it reached it’s 24-hour maximum about 5 hours ago. Either I’m doing something wrong, or the last_changed is not working against the max_value attribute in the database (the database does have the full 24 hours in it).

You have to search the database using an sql query. That’s the only way.

Yeah, I guess so. I was really hoping there was an easy way, like your suggestion. What’s weird is that if you click on the sensor you get a popup window that shows the current/last value and a graph of the temperature during the last 24 hours. If you move the mouse cursor over any part of the graph line you get a small popup that shows the temperature and time at that point on the graph.

Petro - I didn’t realize that a 2nd Moderator had jumped in when I said “an easy way, like your suggestion”. It was Tom_I that made the first suggestion.

Yes, because it did a query and has the data. There’s nothing built into HA that will give you what you want. You’ll have to use the sql integration.

Thanks Petro.