Hi all, is there a way to have a statistic sensor reset/clear at midnight so I can have a daily min & max temperature & if there is how? Thanks
I can’t find a time_pattern
option mentioned in the documentation for the Min/Max integration or in its source code.
Where did you learn about this option?
There’s no time_pattern
option in the documentation for the Statistics integration either. It’s also not found in the source code for the Statistics integration.
Why are you suggesting to use an option that doesn’t exist?
Same question here. Looks like it resets at the time I created the statistic sensor for the first time (or last reboot).
The following Trigger-based Template Sensor reports the highest daily temperature (based on whatever temperature sensor you specify).
- trigger:
- platform: time
at: '00:00:00'
- platform: state
entity_id: sensor.outdoor_temperature
sensor:
- name: 'Today Max Temp'
unit_of_measurement: '°C'
device_class: temperature
state: >
{% set t_new = states('sensor.outdoor_temperature') | float(-99) %}
{{ [t_new, this.state | float(-99)] | max if trigger.platform != 'time' else t_new }}
attributes:
temperature_updated: "{{ now() | as_local }}"
It’s initial value will be unknown
. It will begin reporting a value when the temperature sensor’s value changes. At midnight (start of a new day) it sets its value to whatever is the temperature sensor’s current value.
A similar Trigger-based Template Sensor can be created for reporting the daily minimum temperature (replace the template’s max
filter with min
).
Thx, max temp works. Min temps gives -99 as a result (I replaced the max in the formula with min).
Second question, can I use the same method for max/min temp for the week?
To maintain a long-term record of daily/weekly/monthly/yearly minimum and maximum values, I suggest using the Utility Meter integration. It’s how I keep a record of the number of hours my home’s furnace operates (daily, weekly, etc).
For example, if you want to record the maximum daily/weekly/monthly/yearly temperatures:
utility_meter:
max_temp_daily:
source: sensor.today_max_temp
name: Max Temp Daily
cycle: daily
max_temp_weekly:
source: sensor.today_max_temp
name: Max Temp Weekly
cycle: weekly
max_temp_monthly:
source: sensor.today_max_temp
name: Max Temp Monthly
cycle: monthly
max_temp_yearly:
source: sensor.today_max_temp
name: Max Temp Yearly
cycle: yearly
The data for Utility Meter sensors is stored in a table within Home Assistant’s database that is retained indefinitely and is not purged every 10 days (or whatever duration you may have set in the Recorder integration).
Cool thx! Works for MAX.
Any thoughts on the minimum temp setting itself to -99 ? I tried playing with it in the template editor but there I get the error UndefinedError: ‘trigger’ is undefined.
I tried deleting the (-99) after the float (just float() ) but that does not seem to do the trick.
edit: found it… I replaced -99 with 99 (suddenly thought of what it does) and now it works!
edit2: weekly and monthly MIN do not seem to get updated (stays on 99)
What is the minimum daily sensor reporting?
Daily min reports 8,5
Monthly MAX shows 0,2 which is also weird…
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:
If it still reports incorrect values tomorrow, we’ll need to investigate.
Still the same:
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
- Go to Developer Tools > Statistics
- Find
sensor.min_temp_weekly
in the list - Check if there’s a message indicating a problem
- 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.
- 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.
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
@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! )
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?
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.