The idea is that if the max and min being the same for long enough, I'm going to run an automation to restart my HomeKit. The setup has been working for months.
But my question being, is there a way to consolidate the 3 of those template sensors into one?
I think it is possible, but not quite sure where to start. Hence the question. Figured it could be a good opportunity to improve my jinja2-template-fu.
This is secondary at this point, since we are tracking last_reported thanks @Didgeridrew. However, still, as a practice for templates, is there a good way to merge these 3 sensors into one...?
I'm pretty sure it could also be done with a single trigger-based template sensor with an actions block. You could use the sql.query action to essentially do the same thing as the Statistics sensors are doing...
EDIT:
It would be something like the following, but SQL isn't my forte, so additional tweaks may be necessary:
template:
- triggers:
- trigger: time_pattern
minutes: /3
variables:
max_entity: sensor.ecobee_homekit_max_temp
min_entity: sensor.ecobee_homekit_min_temp
max_age_minutes: 90
sampling_size: 50
age_phrase: "{{ '-'~max_age_minutes~' minutes'}}"
actions:
- action: sql.query
data:
query: |
SELECT
states.state
FROM
states
INNER JOIN states_meta ON
states.metadata_id = states_meta.metadata_id
WHERE
states_meta.entity_id = '{{max_entity}}'
AND last_updated_ts <= strftime('%s', 'now', '{{age_phrase}}')
AND state != 'unavailable'
ORDER BY
last_updated_ts DESC
LIMIT '{{sampling_size}}'
response_variable: maxes
- action: sql.query
data:
query: |
SELECT
states.state
FROM
states
INNER JOIN states_meta ON
states.metadata_id = states_meta.metadata_id
WHERE
states_meta.entity_id = '{{min_entity}}'
AND last_updated_ts <= strftime('%s', 'now', '{{age_phrase}}')
AND state != 'unavailable'
ORDER BY
last_updated_ts DESC
LIMIT '{{sampling_size}}'
response_variable: mins
sensor:
- name: Ecobee Min/Max diff
state: |
{% set maximus = maxes.result | map(attribute='state') | select('is_number') | map('float') | max %}
{% set minimus = mins.result | map(attribute='state') | select('is_number') | map('float') | min %}
{{ (maximus - minimus)|round(2) }}
EDIT:
2026-05-28: The sql.query action is, apparently, only available if you have at least one SQL sensor already configured. If you do not currently have a need for a SQL sensor, the following is the simplest query to initiate the integration:
SELECT states.state
FROM
states
LIMIT
1
Screenshot of query in the SQL integration's UI config flow