How to create an entity that holds the max temperature of the last 24 hours?

See my code above

Create a Trigger-based Template Sensor that reports the highest temperature and resets its value at 00:00.

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: state
        entity_id: sensor.outdoor_temperature
    sensor:
      - name: 'Temperature Daily Max'
        unique_id: 'temperature_daily_max'
        unit_of_measurement: '°C'
        device_class: temperature
        state: >
          {% set t_new = states('sensor.outdoor_temperature') | float(-50) %}
          {{ [t_new, this.state | float(-50)] | max if trigger.platform != 'time' else t_new }}
1 Like

ok,
thank you @finity and @123. I’ll be testing.

Hello @123,

I’m a noob so excuse the stupid questions, but:

  1. will your code work for minimal temp if I simply replaxe max in the last line with min?
  2. How would this code look like to get the min/max temperature of last 72h?

Thx very much and cheers,
zavjah

Yes, you can replace max with min. If you do that then you should also consider changing the default value in the float filter.

Regarding ‘last 72 hours’, that’s possible but the sensor should also report (in an attribute) the date when it starts a 72-hour period. Otherwise you won’t know if the sensor’s reported minimum value is for the last one, two, or three days.

Hello @123 ,

thx for your answers. I’ll change the default temp for min to 50, that should do it.

Regarding the 72h min/max: I think I understand your point, but - to be honest - I have no clue how the code should look like. Could you help me out?

Thx in advance,
Zavjah

Here’s something to get you started:

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: state
        entity_id: sensor.outdoor_temperature
    sensor:
      - name: 'Temperature 3 day Min'
        unique_id: 'temperature_3_day_min'
        unit_of_measurement: '°C'
        device_class: temperature
        state: >
          {% set t_new = states('sensor.outdoor_temperature') | float(50) %}
          {{ [t_new, this.state | float(50)] | min if trigger.platform != 'time' 
            else t_new if ((now() - as_datetime(0)).days % 3) is odd else this.state }}
        attributes:
          start_date: >
            {{ now().isoformat() if ((now() - as_datetime(0)).days % 3) is odd else this.attributes.start_date | default(now().isoformat(), true) }}

thx @123, I’ll look into this.