Multiple time series on one graph using one dynamic query


I am using Grafana (version 9.3.2) with TimescaleDB (version 14) as a data source. I want to plot 2 time series on the same graph using one dynamic query. I followed along some tutorials like:

I want to achieve same result as shown in 6:25 - multiple series on same graphs, based on my variables selection.

I did same thing with my data and my series are not grouped on the graph, they are just appended for same label, all together (selecting multiple variables ends up with adding just data to the same label)

this is my Grafana query:

SELECT time_bucket(‘$time_bucket’, time) AS time,
AVG(altitude) AS value,
device_id::text as metric
FROM vehiclealtitude
WHERE device_id IN ($device_id)
AND time >= $__timeFrom()::timestamptz AND time < $__timeTo()::timestamptz
GROUP BY time_bucket(‘$time_bucket’, time), device_id
ORDER BY time;

this is a graph when selecting just one variable:

And this is graph when I select more than one device_id:

Selecting variables adds new points to the graph, but I would like to have different series (different colour and label) on the graph. I know this topic was here on forum couple of times, but really I did all the solutions and still cannot make it work.

Does anyone have an idea what’s going on?
PS: Same applies when I take my category column as text based not integer.

Thank you

Go to Transform and add Prepare Time Series, then Multi-frame time series: