Emerson Possamai
Emerson Possamai

Reputation: 11

Python & InfluxDB: get most recent measurement

First, some context about my application:

I need to write a Python script to read the database. I want to fetch only the most recent measurement value, not all values.

Here is my code:

from influxdb import InfluxDBClient

client = InfluxDBClient(database='test')
client = InfluxDBClient()

rs = cli.query("SELECT * from CoreTemperature")
CoreTemperature = list(rs.get_points(measurement='CoreTemperature'))

print(CoreTemperature)

I am stuck because of:

  1. The output of this code is: **NameError: name 'cli' is not defined**
  2. Filtering by measurement and select * from CoreTemperature will read all points saved for the measurement, but I only want the most recent value.

Upvotes: 1

Views: 2663

Answers (3)

Emerson Possamai
Emerson Possamai

Reputation: 11

Good morning everyone I reach the target with the following code:

>from influxdb import InfluxDBClient

>dbClient = InfluxDBClient()
>loginRecords = dbClient.query('select last(*) from CoreTemperature', 
database='test')
>print(loginRecords)

Thanks for all comments and help me!

Upvotes: 0

Ramya Gangadharan
Ramya Gangadharan

Reputation: 11

The first error : NameError: name 'cli' is not defined is because you defined the influxdb client as client, and in the query you are calling it as cli

rs = client.query("SELECT * from CoreTemperature")

this will resolve this error.

The second error will be solved by:

client.query('SELECT last(<field_name>), time FROM CoreTemperature ')

Upvotes: 1

Z4-tier
Z4-tier

Reputation: 7978

It sounds like you want the most recent measurement value written to CoreTemperature. You can get that using the LAST selector:

LAST()
Returns the field value with the most recent timestamp.

Like this:

SELECT LAST(*) FROM CoreTemperature

Upvotes: 0

Related Questions