-- 15min agregace telemetrie TČ pro dashboard (TUV křivka v SocTuvChart).
-- Teploty: avg přes přítomné řádky je OK (idle-skip ředí řádky, ale teplota
-- není výkon — viz telemetry.md Idle-skip; nikdy z toho nepočítat energii).

drop view if exists ems.vw_telemetry_heat_pump_15m_7d;

create view ems.vw_telemetry_heat_pump_15m_7d as
select
  time_bucket(interval '15 minutes', t.measured_at) as slot_start,
  t.site_id,
  round(avg(t.tuv_tank_temp_c)::numeric, 1) as avg_tuv_c,
  round(avg(t.water_outlet_temp_c)::numeric, 1) as avg_water_out_c,
  round(avg(t.water_inlet_temp_c)::numeric, 1) as avg_water_in_c,
  max(t.operating_mode) as operating_mode,
  count(*) as sample_count
from ems.telemetry_heat_pump t
where t.measured_at > now() - interval '7 days'
group by 1, 2;

comment on view ems.vw_telemetry_heat_pump_15m_7d is
  '15min agregace TČ telemetrie za 7 dní pro dashboard (TUV/voda křivky).';

grant select on ems.vw_telemetry_heat_pump_15m_7d to ems_anon;
