Original query:

with var_ids as (select id from variables where controller_id = 12)
select
  to_char(datetime, 'DD.MM.YYYY HH24:MI:SS.MS') as datetime,
  json_agg(value order by variable_id) as values
from "values"
where "variable_id" = any (select * from var_ids)
  and values.datetime >= '2019-08-20T10:00:00+00:00'::timestamptz
  and values.datetime < '2020-08-20T10:00:00+00:00'::timestamptz
group by "datetime"
order by "values"."datetime" asc;