Statistic sensor reset/clear at midnight for daily min & max temperature

Daily min reports 8,5

Monthly MAX shows 0,2 which is also weird…

Schermafbeelding 2022-12-19 225101

Check the weekly/monthly values tomorrow and see if it sorts itself out overnight.

Data produced by the Utility Meter integration is stored as long-term statistics. This is the same as energy sensors and there’s this warning for them:

image

If it still reports incorrect values tomorrow, we’ll need to investigate.

1 Like

Still the same:
image

Here is my code:

- trigger:
    - platform: time
      at: '00:00:00'
    - platform: state
      entity_id: sensor.buienradar_temperature
  sensor:
    - name: 'Max temp nw'
      unit_of_measurement: '°C'
      device_class: temperature
      state: >
        {% set t_new = states('sensor.buienradar_temperature') | float(-99) %}
        {{ [t_new, this.state | float(-99)] | max if trigger.platform != 'time' else t_new }}
      attributes:
        temperature_updated: "{{ now() | as_local }}"
- trigger:
    - platform: time
      at: '00:00:00'
    - platform: state
      entity_id: sensor.buienradar_temperature
  sensor:
    - name: 'Min temp nw'
      unit_of_measurement: '°C'
      device_class: temperature
      state: >
        {% set t_new = states('sensor.buienradar_temperature') | float(99) %}
        {{ [t_new, this.state | float(99)] | min if trigger.platform != 'time' else t_new }}
      attributes:
        temperature_updated: "{{ now() | as_local }}"
utility_meter:
  max_temp_weekly:
    source: sensor.max_temp_nw
    name: Max Temp Weekly
    cycle: weekly
  max_temp_monthly:
    source: sensor.max_temp_nw
    name: Max Temp Monthly
    cycle: monthly
  min_temp_weekly:
    source: sensor.min_temp_nw
    name: Min Temp Weekly
    cycle: weekly
  min_temp_monthly:
    source: sensor.min_temp_nw
    name: Min Temp Monthly
    cycle: monthly

  1. Go to Developer Tools > Statistics
  2. Find sensor.min_temp_weekly in the list
  3. Check if there’s a message indicating a problem
  4. If there’s no problem, you can click the icon (at the end of the line) which offers you the ability to delete specific values.
  5. You can try to delete incorrect values and hope it fixes the problem (no guarantee this will fix it).

Repeat the steps for the other long-term sensors.

1 Like

No change unfortunately. Still strange values…

You still have the daily minimum/maximum values which is what you originally asked for in your first post.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

Post a new topic asking for assistance to create weekly/monthly sensors that report correct values. Hopefully it will attract a wider audience and someone will be able to help you.

Opened new topic for my issue. I can not mark your post as solution since I am not the topic starter for this one. Anyway, many thanks so far :slight_smile:

1 Like

@123, this was exactly what I needed for a daily stats view I’m working on. Thanks for contributing!
(If I could mark this as the solution, I would! :smile: )

1 Like

Based on this topic I have defined a trigger sensor to get the max. PV power for the day.

- trigger:
    - platform: time
      at: "00:00:00"
    - platform: state
      entity_id: sensor.sg_total_dc_power
  sensor:
    - name: sg_total_dc_power_max_today
      unique_id: sg_total_dc_power_max_today
      unit_of_measurement: "W"
      device_class: power
      availability: "{{ states('sensor.sg_total_dc_power') | is_number}}"
      state: >
        {% set t_new = states('sensor.sg_total_dc_power') | float(-99) %}
        {{ [t_new, this.state | float(-99)] | max if trigger.platform != 'time' else t_new }}
      attributes:
        power_updated: "{{ now() | as_local }}"

The source entity is a modus register. This register is read every 10 seconds. So the timestamp of the attribute power_updated is changed every 10 seconds. What I would like to achieve is that the attribute has the timestamp when the value of this sensor is changed the last time. So it tells when the maximum occured.

What’s the best way to get the timestamp for the max. value as attribute?

1 Like

Tried to find a solution using the statistics sensor. But using this, I could only set a max_age. But I could not tell the sensor to use the data from today midnight until now.

I created a helper for it and an automation to update it based on the forecasted peak time provided by the forecast.solar integration

alias: Set PV peak time
description: >-
  Update the PV peak time for today. Always update at sunrise. For updates,
  check the validity of the time set.
trigger:
  - platform: sun
    event: sunrise
    offset: 0
    id: sunrise
  - platform: state
    entity_id:
      - sensor.energy_production_today
    id: update
condition:
  - condition: template
    value_template: "{{ states('sensor.power_highest_peak_time_today') != 'unavailable' }}"
    alias: Not unavailable
action:
  - if:
      - condition: trigger
        id: update
      - condition: or
        conditions:
          - condition: time
            after: input_datetime.pv_peak_time
            alias: Do not update if peak time has passed
          - condition: template
            value_template: >-
              {{
              as_local(as_datetime(states('sensor.power_highest_peak_time_today')))
              < now() }}
            alias: Do not set time to the past
    then:
      - stop: ""
    alias: Stop if update conditions are not met
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.pv_peak_time
    data:
      datetime: >-
        {{ as_local(as_datetime(states('sensor.power_highest_peak_time_today')))
        }}
mode: queued
max: 10

Interesting idea.
Until now I use the peak time from SolCast integration for Node-Red flows with which I distribute the charge of the battery over the day.

However, for my sensor with the highest output of the day, I want to store the time when my PV modules delivered the highest output.
For this I now use the following definition:

      attributes:
        power_updated: "{{ states.sensor.sg_total_dc_power_max_today.last_changed | as_local }}"
2 Likes

This might be a bit out of the discussion, but it’s still related so will give it a try.

I’m looking for a long term statistic of the outdoor average temperature for each day (from midnight to midnight). A sensor providing daily outside temperature as average, max and min combined would be all in.

Long description of reason: this is for tracing power usage vs. outside temperature. And since I do have power consumption for each day easily available a daily average of the outside temperature would be a great way to monitor the performance. Heating is heat pump, 3x16kW air-to-water heat pumps, and the arrangement of the outside modules is questioned for low performace (mounted too close, and short circuit of air) so any changes made could affect power consumtion, but if outside temperature differs is’t nothing worth when comparing.

I have invested quite some time in this and thought I want to share the results:

To create two sensors that saves the max/min with timestamp as an attribute and resets periodically:

template:
  - trigger:
      - platform: state
        not_to:
          - unknown
          - unavailable
        entity_id: sensor.my_temp_sensor
      - platform: template
        value_template: "{{ now().hour == 0 }}" #resets each day
    action:
      - variables:
          source: "{{ states('sensor.my_temp_sensor') | float }}"
          now: "{{ now() }}"
    sensor:
      - name: Todays Maximum Temp
        unique_id: today_temp_max_d
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
        icon: mdi:thermometer-chevron-up
        state: |
          {% set current = (this.state or trigger.to_state.state) | float(source) %}
          {{ source if trigger.platform == 'template' else [source, current] | max }}
        attributes:
          datetime: |
            {% set current = (this.state or trigger.to_state.state) | float(source) %}
            {{ now if (trigger.platform == 'template' or source > current) else this.attributes.datetime|default(now) }}
           
      - name: Todays Minimum Temp
        unique_id: today_temp_min_d
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
        icon: mdi:thermometer-chevron-down
        state: |
          {% set current = (this.state or trigger.to_state.state) | float(source) %}
          {{ source if trigger.platform == 'template' else [source, current] | min }}
        attributes:
          datetime: |
            {% set current = (this.state or trigger.to_state.state) | float(source) %}
            {{ now if (trigger.platform == 'template' or source < current) else this.attributes.datetime|default(now) }}

Change this for other intervals:

value_template: "{{ now().hour == 0 }}" #resets every day
value_template: "{{ now().weekday() == 0 }}" #resets every week
value_template: "{{  now().day == 1 }}" #resets every month
value_template: "{{ now().month == 1 }}" #resets each year

Thanks to @Didgeridrew for the idea.

3 Likes

Hello
Using the utility meter to retain non changing records longer is neat, thanks for that.
I seem to have implemented what you suggested successfully. One qq please, the timestamp recorded with the daily reading doesn’t appear to get stored in the utility helper attributes. Is there a way to add that too?
Apologies if I missed a detail along the way.

The only timestamp attribute recorded by a Utility Meter sensor is when it is reset (last_reset).

1 Like

Wow … I didn’t expect so complicate when trying to get outdoor min temperature of the day :crazy_face:
First I tried with a custom intergration “Daily Sensor” but got non reliable results
I will learn these template and try to play with it
Thank you

1 Like

I’m gonna reply here. We now have the statistic sensor in the GUI. I set it on 24 hour interval for some test value, but there is no option to reset it at midnight. It looks like it resets at the time I created the sensor… Is there a way to fix this?

I have a little bit different need of a maximum sensor value of the last calendar day (=yesterday). How to do that?

I’ve added a WTH (again) :slight_smile:

WTH is it so difficult to get yesterday’s total - Month of “What the heck?!” - Home Assistant Community