Add export plan guard to block Deye export against plan.
Some checks failed
CI and deploy / migration-check (push) Failing after 39s
CI and deploy / deploy (push) Has been skipped

Force PASSIVE/no-export when sell is negative or export_mode is NONE,
and alert NEG_SELL_EXPORT in plan_actual_slot_guard when export still occurs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Dusan Vojacek
2026-05-29 00:14:52 +02:00
parent 620a557a89
commit a7dff75e58
8 changed files with 230 additions and 4 deletions

View File

@@ -24,7 +24,8 @@ as $fn$
s.interval_start,
ai.actual_grid_power_w,
ai.deviation_grid_w,
pi.grid_setpoint_w as plan_grid_w
pi.grid_setpoint_w as plan_grid_w,
pi.effective_sell_price as plan_sell_czk
from slots s
inner join ems.audit_interval ai
on ai.site_id = p_site_id
@@ -41,6 +42,12 @@ as $fn$
b.deviation_grid_w,
case
when b.plan_grid_w is null or b.deviation_grid_w is null then null::text
when coalesce(
b.plan_sell_czk,
ems.fn_effective_sell_price(p_site_id, b.interval_start)
) < 0
and coalesce(b.actual_grid_power_w, 0) < -4000
then 'NEG_SELL_EXPORT'
when b.plan_grid_w < -2000 and coalesce(b.actual_grid_power_w, 0) > 2500
then 'GRID_IMPORT_VS_EXPORT_PLAN'
when b.plan_grid_w <> 0
@@ -60,6 +67,22 @@ as $fn$
end as reason_code,
case
when b.plan_grid_w is null or b.deviation_grid_w is null then null::text
when coalesce(
b.plan_sell_czk,
ems.fn_effective_sell_price(p_site_id, b.interval_start)
) < 0
and coalesce(b.actual_grid_power_w, 0) < -4000
then format(
'záporná vykupní %s Kč/kWh, skutečnost síť %s W (vývoz nad práh 4 kW)',
round(
coalesce(
b.plan_sell_czk,
ems.fn_effective_sell_price(p_site_id, b.interval_start)
)::numeric,
4
),
coalesce(b.actual_grid_power_w, 0)
)
when b.plan_grid_w < -2000 and coalesce(b.actual_grid_power_w, 0) > 2500
then format(
'plán síť %s W vs skutečnost %s W (plán vývoz, skutečnost silný odběr)',
@@ -154,7 +177,7 @@ as $fn$
$fn$;
comment on function ems.fn_plan_actual_slot_guard_site(int, timestamptz) is
'Poslední 2 uzavřené 15min sloty: fatální odchylka síť plán vs. audit → insert plan_fatal_deviation_sent (dedup); vrátí JSON s alerts k odeslání na Discord.';
'Poslední 2 uzavřené 15min sloty: fatální odchylka síť plán vs. audit (včetně NEG_SELL_EXPORT při sell<0 a vývozu >4 kW) → insert plan_fatal_deviation_sent (dedup); JSON alerts pro Discord.';
create or replace function ems.fn_plan_actual_slot_guard_all_active(
p_now timestamptz default now()