Use of "hourSelection()" with Python

Dear community,

I am desperate about using “houselection()” in my Python Script. I searched the whole world wide web for an example. Let here be one soon…

Please don´t laugh, I tried:
query = ‘SELECT Mean(“leistung”) FROM “power” |> hourSelection(start: 9, stop:10)’
query = ‘SELECT Mean(“leistung”) FROM “power” WHERE hourSelection(start: 9, stop:10)’
query = ‘SELECT Mean(“leistung”) FROM “power” WHERE hourSelection(9, 10)’
query = ‘SELECT Mean(“leistung”) FROM “power” WHERE hourSelection.start =9 AND hourSelection.stop =10)’

…and dozen more variants. Can sombody please write down this magic query?

Code looks in principle like this:

client = InfluxDBClient(‘localhost’, 8086, ‘admin’, ‘admin’, ‘home’)
query = ‘SELECT Mean(“leistung”) FROM “power” |> hourSelection(start: 9, stop:10)’
reply = client.query(query)
print(reply)

Thanks a lot.

Hello @dr-insaner,
No laughing here! The differences between 1.x and 2.x are vast! Thank you for being curious!! I think you’re confusing InfluxQL with Flux.

If you have 1.8+ you can use the v2 client to query with Flux (in other words the v2 client has 1.8 compatibility).

Your query would look like:

query = " from(bucket:"power")\
  |> range(start:-90d)\
  |> filter(fn: (r) => r._field == "leistung" )\
  |> mean()
  |> hourSelection(start: 9, stop: 10)\

Please use this client, it’s the only one that is supported. GitHub - influxdata/influxdb-client-python: InfluxDB 2.0 python client

Please look at InfluxDB 1.8 compatibility:

GitHub

influxdata/influxdb-client-python

InfluxDB 2.0 python client. Contribute to influxdata/influxdb-client-python development by creating an account on GitHub.

How and where are you running InfluxDB remotely? If you’re using InfluxDB Cloud for example, your client would look like this for 1.8:
username = ‘username’
password = ‘password’

database = ‘power’
retention_policy = ‘autogen’

bucket = f’{database}/{retention_policy}’
client = InfluxDBClient(url=“InfluxDB, token=f’{username}:{password}’, org=’-’)