from influxdb import InfluxDBClient
import time
from datetime import datetime
now = datetime.now().time() # time object
current_time = now.strftime(“%H-%M-%S”)
dbClient = InfluxDBClient(database=‘PV_data’)
results_max = dbClient.query(‘select max(ENERGY_Today) from mqtt_consumer WHERE time < current_time’)
results_min = dbClient.query(‘select min(ENERGY_Today) from mqtt_consumer WHERE time > '2021-01-01' and time < '2021-02-06'’)
for measurement in results_max.get_points(measurement=‘mqtt_consumer’):
kW_yesterday_max = measurement[‘max’]
zeit_max = measurement[‘time’]
for measurement in results_min.get_points(measurement=‘mqtt_consumer’):
kW_yesterday_min = measurement[‘min’]
zeit_min=measurement[‘time’]
%Run db_write_basic_kombiniert.py
Traceback (most recent call last):
File “/home/pi/Python_Scripts/db_write_basic_kombiniert.py”, line 11, in
results_max = dbClient.query(‘select max(ENERGY_Today) from mqtt_consumer WHERE time < current_time’)
File “/usr/local/lib/python3.7/dist-packages/influxdb/client.py”, line 539, in query
in data.get(‘results’, )
File “/usr/local/lib/python3.7/dist-packages/influxdb/client.py”, line 538, in
for result
File “/usr/local/lib/python3.7/dist-packages/influxdb/resultset.py”, line 25, in init
raise InfluxDBClientError(self.error)
influxdb.exceptions.InfluxDBClientError: invalid operation: time and *influxql.VarRef are not compatible
In addition to my suggestion re escaped single quote marks, I have two
comments:
results_max = dbClient.query(‘select max(ENERGY_Today) from mqtt_consumer
WHERE time < current_time’)
results_min = dbClient.query(‘select min(ENERGY_Today) from mqtt_consumer
WHERE time > '2021-01-01' and time < '2021-02-06'’)
In one query you are comparing the “time” field in your database with a
timestamp; in the other you are comparing with a datestamp. This seems odd to
me.
Does your mqtt_consumer table only contain data from today? If not, and
it includes data from previous days, then a query with “where time <
current_time” is going to show you the maxmimum value from any day in the
past, not just today.
@1: it ist just for checking, i tried also with current_date, without success (i am afraid it is working with strings only, not with avariable of type string …) @2:that is correct, you know … this code is just for verifying if the coding works, not already taking care of the logic …
Toni