text: ---------------------------------------------------------------------------
  -- water data errors calc
  
  alter type iot.error_t add value if not exists 'error.above_max_cod';
  alter type iot.error_t add value if not exists 'error.above_max_debit';
  alter type iot.error_t add value if not exists 'error.above_max_nh3n';
  alter type iot.error_t add value if not exists 'error.below_min_ph';
  alter type iot.error_t add value if not exists 'error.above_max_ph';
  alter type iot.error_t add value if not exists 'error.above_max_tss';
  
  create function iot.errors (
      p iot.params_water,
      cod_ float, debit_ float, nh3n_ float, ph_ float, tss_ float
  ) returns iot.error_t[] as $$
      select array_agg(err) from (
          select case when p.has_cod and cod_>p.max_cod then 'error.above_max_cod'::iot.error_t else null end
          union
          select case when p.has_debit and debit_>p.max_debit then 'error.above_max_debit'::iot.error_t else null end
          union
          select case when p.has_nh3n and nh3n_>p.max_nh3n then 'error.above_max_nh3n'::iot.error_t else null end
          union
          select case when p.has_ph and ph_<p.min_ph then 'error.below_min_ph'::iot.error_t else null end
          union
          select case when p.has_ph and ph_>p.max_ph then 'error.above_max_ph'::iot.error_t else null end
          union
          select case when p.has_tss and tss_>p.max_tss then 'error.above_max_tss'::iot.error_t else null end
      ) errs (err)
      where err is not null
  $$ language sql stable;
  
  
  create function iot.errors(r iot.data_water) returns iot.error_t[] as $$
      select iot.errors(
          (select p from iot.params_water p where p.device_id = r.device_id),
          r.cod, r.debit, r.nh3n, r.ph, r.tss
      )
  $$ language sql stable;
  
  