Getting last value from the field

#1

Hello all!
I need help with getting last value from field.

This is my dataset for influxdb:

def get_data_points(session,runNo):
timestamp=datetime.datetime.utcnow().isoformat()
datapoints = [
{
“measurement”: session,
“tags”: {“runNum”: runNo,
},
“time”: timestamp,
“fields”: {
“Voltage”:voltage_value,“Amperage”:amperage_value,“Power”:power_value
}
}
]
return datapoints

For query I tried to use the following:

voltage = “Voltage”
measurement = “11”

client = InfluxDBClient(host, port, user, password, dbname)

def read_voltage_data(voltage, measurement):
results = client.query((“SELECT %s from %s ORDER by time DESC LIMIT 1”) % (voltage, measurement))
points = results.get_points()
for item in points:
return item [voltage]

Value = read_voltage_data(voltage, measurement)
print(Value)

This code returns:
InfluxDBClientError: 400: {“error”:“error parsing query: found 11, expected identifier at line 1, char 21”}
which tells me that query syntax is wrong. But, I cannot see what I am doing wrong. FYI, Grafana’s query is (SELECT “Voltage” FROM “11” WHERE $timeFilter) and it works.

#2

I had to do this way:
querytext = (‘SELECT * from “%s” ORDER by time DESC LIMIT 1’) % (measurement)
#print(querytext)
results = clientdb.query(querytext)

Quotation marks had to be added in query.

#3

Hi @stekicar ! To give you more information, the query engine saw the 11 from your original query as a number rather than a string.

You original could be:

measurement = '"11"'

(that’s single quote double quote 11 double quote single quote)