mirror of
https://github.com/SyncrowIOT/data.git
synced 2025-07-10 07:07:18 +00:00
added motion and presence breakdown
This commit is contained in:
@ -80,7 +80,7 @@ missing_seconds_per_day AS (
|
|||||||
occupied_seconds_per_day AS (
|
occupied_seconds_per_day AS (
|
||||||
SELECT
|
SELECT
|
||||||
space_id,
|
space_id,
|
||||||
missing_date,
|
missing_date as date,
|
||||||
86400 - total_missing_seconds AS total_occupied_seconds
|
86400 - total_missing_seconds AS total_occupied_seconds
|
||||||
FROM missing_seconds_per_day
|
FROM missing_seconds_per_day
|
||||||
)
|
)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
--This model shows the number of times a presence was detected per hour, per day.
|
--This model shows the number of times a presence was detected per hour, per day.
|
||||||
|
|
||||||
|
|
||||||
WITH device_logs AS (
|
WITH device_logs AS (
|
||||||
SELECT
|
SELECT
|
||||||
device.uuid AS device_id,
|
device.uuid AS device_id,
|
||||||
@ -22,37 +21,39 @@ WITH device_logs AS (
|
|||||||
LEFT JOIN "device-status-log"
|
LEFT JOIN "device-status-log"
|
||||||
ON device.uuid = "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' -- presence sensors
|
WHERE product.cat_name = 'hps'
|
||||||
AND "device-status-log".code = 'presence_state'
|
AND "device-status-log".code = 'presence_state'
|
||||||
ORDER BY device.uuid, "device-status-log".event_time
|
ORDER BY device.uuid, "device-status-log".event_time
|
||||||
)
|
),
|
||||||
|
|
||||||
, presence_detection AS (
|
presence_detection AS (
|
||||||
SELECT *,
|
SELECT *,
|
||||||
CASE
|
CASE
|
||||||
WHEN value IN ('presence', 'motion') AND prev_value = 'none' THEN 1 -- detects a change in status from no presence to presence or motion
|
WHEN value = 'motion' AND prev_value = 'none' THEN 1 ELSE 0
|
||||||
ELSE 0
|
END AS motion_detected,
|
||||||
|
CASE
|
||||||
|
WHEN value = 'presence' AND prev_value = 'none' THEN 1 ELSE 0
|
||||||
END AS presence_detected
|
END AS presence_detected
|
||||||
FROM device_logs
|
FROM device_logs
|
||||||
)
|
),
|
||||||
|
|
||||||
|
presence_detection_summary AS (
|
||||||
, presence_detection_summary AS (
|
SELECT
|
||||||
SELECT device_id,
|
pd.device_id,
|
||||||
subspace_id,
|
d.subspace_id,
|
||||||
space_id,
|
pd.space_id,
|
||||||
event_time::date AS event_date,
|
pd.event_time::date AS event_date,
|
||||||
EXTRACT(HOUR FROM date_trunc('hour', event_time)) AS event_hour,
|
EXTRACT(HOUR FROM date_trunc('hour', pd.event_time)) AS event_hour,
|
||||||
sum(presence_detected) AS count_presence_detected
|
SUM(motion_detected) AS count_motion_detected,
|
||||||
FROM presence_detection
|
SUM(presence_detected) AS count_presence_detected,
|
||||||
LEFT JOIN device
|
SUM(motion_detected + presence_detected) AS count_total_presence_detected
|
||||||
ON device."uuid" = device_id
|
FROM presence_detection pd
|
||||||
|
LEFT JOIN device d ON d.uuid = pd.device_id
|
||||||
GROUP BY 1, 2, 3, 4, 5
|
GROUP BY 1, 2, 3, 4, 5
|
||||||
)
|
),
|
||||||
|
|
||||||
-- Generate all 24 hours for each unique event_date
|
all_dates_and_hours AS (
|
||||||
, all_dates_and_hours AS (
|
|
||||||
SELECT device_id, subspace_id, space_id, event_date, event_hour
|
SELECT device_id, subspace_id, space_id, event_date, event_hour
|
||||||
FROM (
|
FROM (
|
||||||
SELECT DISTINCT device_id, subspace_id, space_id, event_date
|
SELECT DISTINCT device_id, subspace_id, space_id, event_date
|
||||||
@ -60,13 +61,15 @@ WITH device_logs AS (
|
|||||||
) d,
|
) d,
|
||||||
generate_series(0, 23) AS event_hour
|
generate_series(0, 23) AS event_hour
|
||||||
)
|
)
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
adah.*,
|
adah.*,
|
||||||
COALESCE(pds.count_presence_detected, 0) AS count_presence_detected
|
COALESCE(pds.count_motion_detected, 0) AS count_motion_detected,
|
||||||
|
COALESCE(pds.count_presence_detected, 0) AS count_presence_detected,
|
||||||
|
COALESCE(pds.count_total_presence_detected, 0) AS count_total_presence_detected
|
||||||
FROM all_dates_and_hours adah
|
FROM all_dates_and_hours adah
|
||||||
left JOIN presence_detection_summary pds
|
LEFT JOIN presence_detection_summary pds
|
||||||
ON pds.device_id = adah.device_id
|
ON pds.device_id = adah.device_id
|
||||||
AND pds.event_date = adah.event_date
|
AND pds.event_date = adah.event_date
|
||||||
AND pds.event_hour = adah.event_hour
|
AND pds.event_hour = adah.event_hour
|
||||||
ORDER BY 1,4,5;
|
ORDER BY 1, 4, 5;
|
||||||
|
Reference in New Issue
Block a user