Skip to content

Commit

Permalink
Resolve "Aproximar la potència a partir de dos valors d'energia conse…
Browse files Browse the repository at this point in the history
…cutius" (et/somenergia-jardiner!133)

Closes #200

Merge branch '200-aproximar-la-potencia-a-partir-de-dos-valors-d-energia-consecutius' into 'main'
  • Loading branch information
polmonso committed Mar 14, 2024
2 parents 819e5e4 + 5b659c9 commit 5dcde7a
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{ config(materialized='view') }}


with instant_energy as (
select
ts,
plant_name,
plant_uuid,
signal_value as meter_energy_kwh
from {{ ref('int_dset_responses__values_incremental') }}
where
ts > now() - interval '3 hours'
{#- filter out old readings before massaging #}
and metric_name = 'energia_activa_exportada_instantania'
and signal_value is not null
),
instant_energy_delta as (
select
ts,
plant_name,
plant_uuid,
meter_energy_kwh,
lead(ts) over plant_window as previous_ts,
lead(meter_energy_kwh) over plant_window as previous_energy,
ts - lead(ts) over plant_window as delta_ts,
meter_energy_kwh - lead(meter_energy_kwh) over plant_window as delta_meter_energy_kwh
from instant_energy
window plant_window as (partition by plant_uuid order by ts desc)
order by ts desc
),
inferred_meter_power as (
select
ts,
plant_name,
plant_uuid,
meter_energy_kwh,
60 / extract(minute from delta_ts) * delta_meter_energy_kwh as inferred_meter_power_kw,
previous_energy,
delta_meter_energy_kwh,
previous_ts,
delta_ts,
60 / extract(minute from delta_ts) as dt_hour_normalized
from instant_energy_delta
order by ts desc
)
select
plant_uuid,
ts,
plant_name,
meter_energy_kwh,
inferred_meter_power_kw,
previous_energy,
delta_meter_energy_kwh,
previous_ts,
delta_ts,
dt_hour_normalized
from inferred_meter_power
order by plant_uuid asc, ts desc
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,97 @@

{# Assumim que una planta o bé té potencia_activa o bé té les tres fases potencia_activa_fase#}
with plant_power_sum as (
select
plant_uuid,
min(ts) as instant_power_plant_kw_registered_at,
max(ts) - min(ts) as diff_ts,
round(sum(signal_value), 1) as instant_power_plant_kw
from {{ ref('int_dset__last_registries') }}
where ts at time zone 'Europe/Madrid' >= current_date
and device_type = 'inverter'
and metric_name ilike 'potencia_activa%'
group by
plant_uuid,
device_type
select
plant_uuid,
min(ts) as instant_power_plant_kw_registered_at,
max(ts) - min(ts) as diff_ts,
round(sum(signal_value), 1) as instant_power_plant_kw
from {{ ref('int_dset__last_registries') }}
where
ts at time zone 'Europe/Madrid' >= current_date
and device_type = 'inverter'
and metric_name ilike 'potencia_activa%'
group by
plant_uuid,
device_type
),

instant_power_plant as (
select *
from plant_power_sum
where diff_ts <= interval '15 min'
select *
from plant_power_sum
where diff_ts <= interval '15 min'
),

last_irradiancia as (
{#
Cada planta té un o diversos tipus de senyals d'irradancia. Així, per cada planta, triem una senyal
d'irradancia. l'Order by permet fer una jerarquia quan a una planta hi ha més d'un tipus de irradància.
#}
select distinct
on (plant_uuid, ts)
plant_uuid,
ts,
signal_value
from {{ ref("int_dset__last_registries") }}
where
metric_name = 'irradiancia'
and ts at time zone 'Europe/Madrid' >= current_date
order by
plant_uuid,
ts,
case
when signal_name = 'irradiancia'
then 1
when signal_name = 'irradiancia_sonda_bruta'
then 2
when signal_name = 'irradiancia_sonda_neta'
then 3
when signal_name = 'irradiancia_sonda'
then 4
end
select distinct
on (plant_uuid, ts)
plant_uuid,
ts,
signal_value
from {{ ref("int_dset__last_registries") }}
where
metric_name = 'irradiancia'
and ts at time zone 'Europe/Madrid' >= current_date
order by
plant_uuid,
ts,
case
when signal_name = 'irradiancia'
then 1
when signal_name = 'irradiancia_sonda_bruta'
then 2
when signal_name = 'irradiancia_sonda_neta'
then 3
when signal_name = 'irradiancia_sonda'
then 4
end
),

last_instant_power as (
select distinct on (plant_uuid)
plant_uuid,
ts,
inferred_meter_power_kw
from {{ ref("int_dset__power_from_instant_energy") }}
order by plant_uuid asc, ts desc
),

plant_production_daily_previous_day as (
select
plant_uuid,
nom_planta as plant_name,
dia as "day",
energia_exportada_comptador_kwh as meter_exported_energy_kwh,
energia_esperada_solargis_kwh as solargis_meter_expected_energy_kwh
from {{ ref('dm_plant_production_daily') }}
where dia = current_date - interval '1 day'
select
plant_uuid,
nom_planta as plant_name,
dia as "day",
energia_exportada_comptador_kwh as meter_exported_energy_kwh,
energia_esperada_solargis_kwh as solargis_meter_expected_energy_kwh
from {{ ref('dm_plant_production_daily') }}
where dia = current_date - interval '1 day'
)

select
p.plant_uuid,
p.plant_name,
p.municipality,
p.province,
p.technology,
p.peak_power_kw,
p.nominal_power_kw,
i.instant_power_plant_kw_registered_at,
i.instant_power_plant_kw,
ir.ts as irradiance_w_m2_last_registered_at,
ir.signal_value as irradiance_w_m2,
ppd.day,
ppd.meter_exported_energy_kwh,
ppd.solargis_meter_expected_energy_kwh
p.plant_uuid,
p.plant_name,
p.municipality,
p.province,
p.technology,
p.peak_power_kw,
p.nominal_power_kw,
coalesce(i.instant_power_plant_kw_registered_at, ip.ts) as instant_power_plant_kw_registered_at,
coalesce(i.instant_power_plant_kw, ip.inferred_meter_power_kw) as instant_power_plant_kw,
ir.ts as irradiance_w_m2_last_registered_at,
ir.signal_value as irradiance_w_m2,
ppd.day,
ppd.meter_exported_energy_kwh,
ppd.solargis_meter_expected_energy_kwh,
case
when i.instant_power_plant_kw is not null then 'directa'
else 'inferida'
end as instant_power_source
from {{ ref('raw_gestio_actius_plant_parameters') }} as p
left join instant_power_plant as i on p.plant_uuid = i.plant_uuid
left join plant_production_daily_previous_day as ppd on p.plant_uuid = ppd.plant_uuid
left join last_irradiancia as ir on p.plant_uuid = ir.plant_uuid
left join instant_power_plant as i using (plant_uuid)
left join plant_production_daily_previous_day as ppd using (plant_uuid)
left join last_irradiancia as ir using (plant_uuid)
left join last_instant_power as ip using (plant_uuid)
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ select
when overview.day > now() - interval '2 days' then
round(((overview.meter_exported_energy_kwh - overview.solargis_meter_expected_energy_kwh) / 1000)::numeric, 2)
end as energia_perduda_ahir_mwh,
round(((overview.meter_exported_energy_kwh - overview.solargis_meter_expected_energy_kwh) / 1000)::numeric, 2) as energia_perduda_mwh
round(((overview.meter_exported_energy_kwh - overview.solargis_meter_expected_energy_kwh) / 1000)::numeric, 2) as energia_perduda_mwh,
overview.instant_power_source
from {{ ref("int_plants_overview_instant") }} as overview
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{ config(materialized="view") }}

with inverter_power as (
select
plant_uuid,
plant_name,
ts,
sum(signal_value) as inverter_power_kw,
count(*) as num_inverters
from {{ ref("int_dset_responses__values_incremental") }}
where ts > now() - interval '7 days' and metric_name = 'potencia_activa'
group by plant_uuid, plant_name, ts
),
instant_power_comparison as (
select
plant_uuid,
instant_power.plant_name,
ts,
inverter_power.inverter_power_kw,
instant_power.inferred_meter_power_kw,
round(abs(inverter_power.inverter_power_kw - instant_power.inferred_meter_power_kw)::numeric, 2) as delta_power_instant_vs_inferred,
inverter_power.num_inverters,
instant_power.meter_energy_kwh,
instant_power.previous_energy,
instant_power.delta_meter_energy_kwh,
instant_power.previous_ts,
instant_power.delta_ts
from {{ ref("int_dset__power_from_instant_energy") }} as instant_power
left join
inverter_power
using (plant_uuid, ts)
)
select *
from instant_power_comparison
order by plant_uuid asc, ts desc

0 comments on commit 5dcde7a

Please sign in to comment.