Pulling Maximum Value

Hi,

I have a physical weather station in my garden which provides UV, wind and other metrological data back to HA.

I am trying to display the daily maximum value for a sensor on card. I don’t want to use the statistical graph as it provides too much information.

image

I thought I set it up correctly however clearly I am missing something.

It doesn’t seem to work properly, for example the daily max was 4 on the UV scale, but it records a 3.

image

I created a sensor using the below code and then read the result back in a love lace card.

-   platform: statistics
    name: "UV Max"
    entity_id: sensor.uv_index
    state_characteristic: value_max
    max_age:
      hours: 24

What am I missing here. Expect it’s something very obvious – any help would be gratefully received.
I appreciate I am recording the last 24 hours in the above code, ideally it would reset at midnight - but that might be a phase II project :slight_smile:

Many thanks

You need to specify a sample_size. The default sample_size is twenty. Once twenty samples are recorded it starts to reuse the sample storage places so even if you specify 24 hours as max_age that sample was likely overwritten. So if a sample is taken every 15 minutes then for 24 hours you need 24*4=96 sample slots.

As you can see this can get kind of wasteful of memory especially if you only want to save the maximum value. A better solution would be to write a little automation that checks the max (input_number) versus the current temp and keeps the higher value on every temperature change plus just keeps the current value as maximum at midnight.

Hi and thanks for your reply, I have added the sample size but it still doesnt really work.

Using the below code including the sample size it isn’t pulling the highest value over the 24h period, which is closer to 15c opposed to the 8.45c reported. I suppose this might be because the sensor is providing a lot of readings and in this instant the last 96 may only go back a hour or so?

I understand your idea on the automation but not sure where to start, is there any reading you can recommend?

Thanks

-   platform: statistics
    entity_id: sensor.outdoor_temperature
    name: "Max Temp"
    max_age:
      hours: 24 
    sampling_size: 96

That’s, again because of your sample size.
The temperature seems to report much more often than every 15 minutes just by looking at the graph.
Calculate the sample size you need based on how often it reports.

I was just giving you an example. My weather station provides an update every 15 seconds. If the temperature changed every update then I would need at least 24604=5760 spaces for a 24 hour period.

Thanks Micque, seems my approach might be overly extensive to capturing the daily highs.
I am experimenting with this HAC integration.

2 Likes

I am having the same issue and want to try the automation route. I am still a little new to Home Assistant Can you provide some guidance on how to go about this?

Here is a triggered sensor that will keep the max value and reset at midnight. When first started the sensor will report ‘unknown’ until sensor.outside_temp changes for the first time.

template:
  - trigger:
      - platform: time_pattern
        hours: 0
        minutes: 0
        id: 'midnight'
      - platform: state
        entity_id: sensor.outside_temp
        id: 'change'
    sensor:
      - name: Max Temp
        state: >
          {% set outt = states('sensor.outside_temp')|float(None) %}
          {% set maxt = states('sensor.max_temp')|float(None) %}
          {% if  outt != None  and (trigger.id == 'midnight' or maxt == None
            or outt > maxt) %} {{ outt }}
          {% else %} {{ maxt }} {% endif %}
        unit_of_measurement: F
5 Likes