I was trying to get some help in another thread but now I don’t think interpolated_duration_in
is really the fix for my problem so I’m starting a new thread.
I am pushing machine state changes (currently only Running
, Stopped
, and Offline
) from Tulip to TimescaleDB. I’m then putting into 12 hour time buckets aligned to shift start times.
Right now I have two problems:
-
The durations for complete shifts does not add up to 12 hours (sometimes not even close).
My current assumption is that perhaps the state changes are not being carried over
time_bucket
intervals and I don’t see a way to force LOCF. -
I’m trying to use the
running time / total time
to calculate utilization but the math isn’t right most of the time. I added extraction of the durations into seconds and then cast them to float which changed the values but they’re still wrong.
Here’s my current sql statement:
--- OEE Utilization reporting by shift, cell, asset
SELECT shift_start_time, shift_abcd as shift_abcd, cell, asset, duration_in(agg, 'Running') as running, duration_in(agg, 'Stopped') as stopped,
duration_in(agg, 'Offline') as offline, duration_in(agg, 'Running') + duration_in(agg, 'Stopped') + duration_in(agg, 'Offline') as total,
CASE
WHEN extract(SECONDS from duration_in(agg, 'Running')) + extract(SECONDS from duration_in(agg, 'Stopped')) = 0
THEN 0
ELSE cast(extract(SECONDS from duration_in(agg, 'Running')) as float) / cast(extract(SECONDS from duration_in(agg, 'Running')) + extract(SECONDS from duration_in(agg, 'Stopped')) as float)
END as utilization
from (SELECT time_bucket('12 hours', timestamp - interval '5 hours', 'America/Chicago') + interval '5 hours' as shift_start_time,
shift_abcd.shift as shift_abcd, cell, asset, state_agg(timestamp, state) as agg
FROM machine_state
left join shift_abcd on time_bucket('12 hours', timestamp - interval '5 hours', 'America/Chicago') + interval '5 hours' = public.shift_abcd.shift_start
GROUP BY shift_start_time, shift_abcd, cell, asset
ORDER BY shift_start_time desc)sub;
I really need to get this working properly.