I’m using InfluxDB 1.8 where data from several temperature sensors are collected. Each sensor is identified by its own id. So I have a measurement with 3 fields, temp, id and battery. The simple query is:
SELECT “temp” FROM “frost” WHERE id=141
Now I want to migrate to 2.4 and wondering how I can achieve the same here. Flux query like the below gives temp data from all sensors.
from(bucket: “hometest/autogen”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “frost”)
|> filter(fn: (r) => r[“_field”] == “temp”)
|> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
|> yield(name: “last”)
How can I retrieve temp values from a certain sensor only?
id
is a field and not a tag?
yes, it is a field. Data is being retrieved from MQTT via node-red. So temp, id and battery are being treated as fields.
Honestly I haven’t looked into the concept of tags.
OK. Anyway, in that case, this could work:
import "influxdata/influxdb/schema"
from(bucket: “hometest/autogen”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “frost”)
|> filter(fn: (r) => r[“_field”] == “temp” or r[“_field”] == “id”)
|> schema.fieldsAsCols()
|> filter(fn: (r) => r[“id”] == 141)
|> aggregateWindow(every: v.windowPeriod, fn: last, column: "temp", createEmpty: false)
|> yield(name: “last”)
If id
was a tag, query would be simpler (just one more filter
) and faster ( depending on how many sensors and their data you have of course).
...
|> filter(fn: (r) => r[“_field”] == “temp”)
|> filter(fn: (r) => r[“id”] == "141") // JUST ADD THIS
|> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
...
Note: I’ve edited the query - I forgot you have to specify on which column aggregateWindow
should operate on in case of pivoted data.
Thanks a lot!
The schema function was what I have missed. And I’ll have a look at the aggregateWindow to just show the temp values if sensor id 141.