Making graphs in Grafana from PostgreSQL/ TimescaleDB databases

Of course you get an error. The query was meant for Pgadmin. For Grafana you have to adjust and add the time variables for grouping and filtering :wink:

I think you found the trick!!

Now the transfer to Grafana and we had a very fruitful Friday!

I think you should also take a look at: https://docs.timescale.com/latest/using-timescaledb/reading-data#locf and https://docs.timescale.com/latest/using-timescaledb/reading-data#gap-filling

Btw, many props to @CM000n for taking so much effort to dig your problem out!

1 Like

Hello,

I also use the LTS / Postgres, but I’m somehow too stupid to display the whole thing in Grafana …


I can display the table, but as soon as I switch to the visualize I get an error …

Where is my mistake? :frowning:

Thanks very much

EDIT: Fixed it:

Glad it worked for you :slight_smile:

You can also convert the values to integers or floats directly within the query. This would look like this.

SELECT
  "time" AS "time"
  , entitiy_id AS metric
  , state::FLOAT AS value
FROM ltss
WHERE
  entity_id = 'sensor.solar_production_daily'
ORDER BY 1

This is, IMHO, sometimes easier than searching for the right setting in the Grafana Gui.

1 Like

Thanks! :slight_smile:

is there an alternative to ALIAS BY that works with postgres? all data are now called “value” in the Legend -_- I only find solutions with ALIAS BY, but this is not recognized by me.

I’m not sure I understand you correctly and what you mean by “ALIAS BY”.
In general you should put the time values into a $__timeGroupAlias and $__timeFilter to use the time intervals of the Grafana dropdowns comfortably.
This documentation may also help you:
PostgreSQL | Grafana Labs
For the metric labels you can either take values from other existing columns or specify them directly manually. This does not matter then.

This query results in the following graphic for me, for example:

SELECT
  $__timeGroupAlias("time",$__interval,previous)
  , AVG(state::FLOAT) AS "PM2.5 - Ubstadt"
FROM ltss
WHERE
  $__timeFilter("time")
  AND entity_id = 'sensor.luftdaten_19149_p2'
  AND state != 'unavailable'
GROUP BY 1
ORDER BY 1

1 Like

Big Thanks,

I’ve tried only

SELECT
  "time" AS "time"
  , entitiy_id AS metric
  , state::FLOAT AS value
FROM ltss
WHERE
  entity_id = 'sensor.solar_production_daily'
ORDER BY 1

I tried to change value to “XYZ”
When I tried this, I got an error. So I thought it had to be “value” … but this made sure that I had 3 graphs, all of which were labeled “value”.

But with your solution it works perfectly :slight_smile:


big thank you

1 Like