Using variable in panel query

Hi guys, I’m just getting started with Grafana having installed TeslaMate. Think the UI and possible customisations are brilliant but I’m having trouble finding the documentation for and getting basic variables working. I know my way around the basics of HTML5, CSS, PHP, MySQL and a bit of jS but the small differences are giving me a headache! And as with most things like this, it either works or it doesn’t, there’s no ‘half output’.

In a dashboard I’ve set 3 variables which all preview the desired output, these being:

$miles = SELECT convert_km(odometer, ‘$length_unit’) FROM positions WHERE car_id = $car ORDER BY date desc limit 1;

$chargecost = SELECT sum(cost) FROM charging_processes WHERE car_id = $car;

$dieselcost = SELECT convert_km(odometer, ‘$length_unit’) * 0.15 FROM positions WHERE car_id = $car ORDER BY date desc limit 1;

So far so good. But then creating a panel, I set the query to simply $miles for example, and I get the error ** pq: syntax error at or near “662.2149769470715” **. This is the value of the variable so am I calling the variable incorrectly? From the documentation I’ve seen it should just be as simple as $miles.

For another panel I’d want $dieselcost - $chargecost to show fuel savings. Any considerations with doing a sum?

Thanks in advance and sorry for the noob question, I’m sure it’s a simple answer.