This commit is contained in:
Dona Maria Absi
2025-03-06 21:52:14 +03:00
parent 58228978a3
commit 8b276e9816
6 changed files with 94 additions and 4 deletions

View File

@ -35,7 +35,7 @@ time_differences AS (
event_time::date AS event_date, event_time::date AS event_date,
EXTRACT(EPOCH FROM (event_time - COALESCE(prev_timestamp, event_time))) AS time_diff_in_seconds EXTRACT(EPOCH FROM (event_time - COALESCE(prev_timestamp, event_time))) AS time_diff_in_seconds
FROM start_date FROM start_date
) ),
duration as ( duration as (

View File

@ -95,4 +95,4 @@ LEFT JOIN daily_empty_duration d
ON a.space_id = d.space_id ON a.space_id = d.space_id
AND a.occupancy_date = d.occupancy_date AND a.occupancy_date = d.occupancy_date
join "space" s join "space" s
on s."uuid" =a.space_id; on s."uuid" =a.space_id

View File

@ -0,0 +1,27 @@
SELECT
device.uuid AS device_id,
device.space_device_uuid AS space_id,
-- Mark none_flag if value is 'none'
CASE
WHEN "device-status-log".value = 'none' THEN 1
ELSE 0
END AS none_flag,
"device-status-log".event_time::timestamp AS event_time,
LAG("device-status-log".event_time::timestamp)
OVER (PARTITION BY device.uuid
ORDER BY "device-status-log".event_time) AS prev_timestamp,
LAG(
CASE
WHEN "device-status-log".value = 'none' THEN 1 -- identifies if there was a 'none' flag detected
ELSE 0
END
) OVER (PARTITION BY device.uuid
ORDER BY "device-status-log".event_time) AS prev_none_flag
FROM device
LEFT JOIN "device-status-log"
ON device.uuid = "device-status-log".device_id
LEFT JOIN product
ON product.uuid = device.product_device_uuid
WHERE product.cat_name = 'hps'
AND "device-status-log".code = 'presence_state'
ORDER BY device.uuid, "device-status-log".event_time

View File

@ -18,7 +18,7 @@ WITH device_logs AS (
ORDER BY "device-status-log".event_time) AS prev_value ORDER BY "device-status-log".event_time) AS prev_value
FROM device FROM device
LEFT JOIN "device-status-log" LEFT JOIN "device-status-log"
ON device.uuid::text = "device-status-log".device_id ON device.uuid = "device-status-log".device_id
LEFT JOIN product LEFT JOIN product
ON product.uuid = device.product_device_uuid ON product.uuid = device.product_device_uuid
WHERE product.cat_name = 'hps' WHERE product.cat_name = 'hps'
@ -68,5 +68,5 @@ INNER JOIN subspace
INNER JOIN "space" AS space INNER JOIN "space" AS space
ON space.uuid = device.space_device_uuid ON space.uuid = device.space_device_uuid
WHERE "device-status-log".code = 'presence_state' WHERE "device-status-log".code = 'presence_state'
GROUP BY 1, 2, 3, 4, 5,6 GROUP BY 1, 2, 3, 4, 5,6;

View File

@ -0,0 +1,9 @@
SELECT distinct(code)
FROM device
LEFT JOIN "device-status-log"
ON device.uuid = "device-status-log".device_id
LEFT JOIN product
ON product.uuid = device.product_device_uuid
where product.name='Power Clamp'
--and code='VoltageA'
--ORDER BY device.uuid, "device-status-log".event_time

View File

@ -0,0 +1,54 @@
WITH avg_set_temp AS (
-- Step 1: Compute the average set temperature per device per hour
SELECT
device.uuid AS device_id,
device.space_device_uuid AS space_id,
event_time::date AS date,
DATE_PART('hour', event_time) AS hour,
AVG("device-status-log".value::INTEGER) AS avg_set_temp
FROM device
LEFT JOIN "device-status-log"
ON device.uuid = "device-status-log".device_id
LEFT JOIN product
ON product.uuid = device.product_device_uuid
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
d.uuid AS device_id,
d.space_device_uuid AS space_id,
l.event_time::date AS date,
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
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
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)
WHERE p.name = 'Smart Thermostat'
and l.code = 'temp_current'
ORDER BY d.uuid, l.event_time;