thermostat

This commit is contained in:
Dona Maria Absi
2025-03-06 22:51:18 +03:00
parent 8b276e9816
commit 47693447da

View File

@ -14,41 +14,31 @@ WITH avg_set_temp AS (
WHERE product.name = 'Smart Thermostat'
AND "device-status-log".code = 'temp_set'
GROUP BY 1,2,3,4
),
avg_set_temp_filled AS (
SELECT
device_id,
space_id,
date,
hour,
avg_set_temp,
COALESCE(
avg_set_temp,
LAG(avg_set_temp) OVER (
PARTITION BY device_id
ORDER BY date, hour
)
) AS filled_avg_set_temp
FROM avg_set_temp
)
SELECT
SELECT DISTINCT
d.uuid AS device_id,
d.space_device_uuid AS space_id,
l.event_time::date AS date,
-- l.event_time, -- Include event_time in SELECT to use it in ORDER BY
DATE_PART('hour', l.event_time) AS hour,
CASE WHEN l.code = 'temp_current' THEN l.value END AS current_temp,
f.filled_avg_set_temp
COALESCE(f.avg_set_temp,
MAX(f.avg_set_temp) OVER (
PARTITION BY d.uuid
ORDER BY l.event_time
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)
) AS final_filled_avg_set_temp
FROM device d
LEFT JOIN "device-status-log" l
ON d.uuid = l.device_id
LEFT JOIN product p
ON p.uuid = d.product_device_uuid
LEFT JOIN avg_set_temp_filled f
LEFT JOIN avg_set_temp f
ON f.device_id = d.uuid
AND f.date = l.event_time::date
and f.date = l.event_time::date AND f.hour <= DATE_PART('hour', l.event_time)
AND f.hour = DATE_PART('hour', l.event_time) -- Ensure correct hour match
WHERE p.name = 'Smart Thermostat'
and l.code = 'temp_current'
ORDER BY d.uuid, l.event_time;
AND l.code = 'temp_current'
ORDER BY 1,3;