Statistics for today (since midnight)

Trying to create statistics sensors to report daily high and low values for the current day. I have tried several different methods of doing this but have not landed on the correct one yet.

- platform: statistics
  name: Daily High
  entity_id: sensor.eagles_nest_temp
  state_characteristic: value_max
  max_age:
    minutes: '{{ (now().hour * 60) + (now().minute) }}'

This configuration results in the following error upon running the Check Configuration:

Invalid config for [sensor.statistics]: expected float for dictionary value @ data[‘max_age’][‘minutes’]. Got ‘{{ (now().hour * 60) }} + {{ (now().minute) }}’. (See ?, line ?).

Replacing the (') characters with (") characters or removing them all together results in similar error messages.

I have checked the ‘time math’ in the developer tools template editor and it works:

Minutes since Midnight: {{ (now().hour * 60) + (now().minute) }}

Result:
image

How do I enter the {{ (now().hour * 60) + (now().minute) }} string and have it return the calculation instead of the actual string?

The sensor does pass validation with this code:

  max_age:
    days: 1

But it does not produce the behavior I’m looking for. days: 1 seems to function the same as hours: 24. It shows yesterday’s high temperature until today’s temperature exceeds that value or the value ages out of the data set.

The recently added Statistics Card does exactly what I’m looking for but I can’t use that card in the custom card I’m building for this weather dashboard.

Thank you for any assistance!

That’s not how the statistics sensor works… you cannot use templates everywhere, configuration keys that will accept templates as values are indicated in the docs with the a blue hyperlinked “template”. Compare the docs for Statistics - max_age to this example from the Template number entity.

If you want a daily max sensor you can use a trigger-based template sensor

Thank you for that clarification on the doc conventions, Mr. Drew! I appreciate it! I was already on the templates doc page but I had not drawn the correlation between what I was trying to accomplish and a trigger-based template. I think I can figure it out from here…

Happy Holiday’s!

I’m just not clear how it should be done.

Could you give an example?

Thanks.

The details are in the link Drew posted to the trigger-based template sensor. That link should take you directly to the post that discusses a daily max sensor.

Based on what I learned from that post, this is what I came up with for my daily max and min temperature sensors:

Daily Min Max Temperatures - Template Sensor
# Daily Min-Max outdoor temperature
- trigger:
  - platform: time
    at: '00:00:00'
  - platform: state
    entity_id: sensor.eagles_nest_temp
  sensor:
    - name: "Outdoor Daily High Temperature"
      unit_of_measurement: '°F'
      device_class: temperature
      state: >
        {% set t_high = states('sensor.eagles_nest_temp') | float(-99) %}
        {{ [t_high, this.state | float(-99)] | max if trigger.platform != 'time' else t_high }}
      attributes:
        temperature_updated: "{{ now() | as_local }}"
      unique_id: 4ec72f1a-3430-49cb-b8e6-56ce99bbe3ea

    - name: "Outdoor Daily Low Temperature"
      unit_of_measurement: '°F'
      device_class: temperature
      state: >
        {% set t_low = states('sensor.eagles_nest_temp') | float(120) %}
        {{ [t_low, this.state | float(120)] | min if trigger.platform != 'time' else t_low }}
      attributes:
        temperature_updated: "{{ now() | as_local }}"
      unique_id: 36b129f0-fe1a-4988-bc2b-f32e5b9f38da

This would go under the template: section in your configuration file.

Hope this helps.

1 Like

Thanks, I forgot to tell you.