How do I write a template to calculate the average from recent, say last 2 hours, temperatures from a temperature sensor? I read some other posts and saw some samples. I played around with template section of this code in Developer Tools Template Editor without success:
- platform: template
sensors:
mean_temperature:
friendly_name: "Mean Temperature"
unit_of_measurement: "°C"
value_template: >
{% set values = states.sensor.esp_2_object.attributes.data | default([]) %}
{% if values %}
{{ values | map(attribute='state') | map('float') | sum / values | length }}
{% else %}
0
{% endif %}
In States, the sensor looks like this:
The sensor is the object temperature reading from a mlx90614 sensor via an ESP32.
Thanks!
Mark