Time series dynamically get/show legend based on data from db/influx

Hi, I am slightly new to the world of Grafana. I am able to make some simple panels graphs but the problem is that I don’t know what I don’t know and searching on google with the terms that make sense to me did not lead me to any result so I hope that I will find and answer here. What I want to do makes a lot of sense to me but it might not necessarily be possible or correct as I want it so I want to clarify.

Here is an example of data that I have in my database. Currently I have around 15 unique names, but this number is growing.

Instead of the aliases like in the picture below, I would like to be able to dynamically populate this field and show the legend of the graph based on the db name field.

What I have tried, and what works, is that I have a query for each unique name, and then give it an alias (see picture below). Even though this works, it is very cumbersome as I need to keep track of all the names and manually add them, which, for a home automation project, where I constantly add/remove/change devices, is not practical. What I am hoping for is that there is a way to somehow retrieve from the database the battery level and name, and that each name is plotted on the chart with it’s own battery percentage.

Thanks for the help,
All best, Alain