Difference fonction with python under (Spyder)


I have a project where I need to calculate the energy of a PV_system.

I have uploaded the data in the InfluxDB database (locally).

I want to use a loop to calculate the daily energy during one year. Unfortunately I can’t use the function now() in my code, because the date of the measurement is in the past. But I know the beginning and can therefore query the energy in one day.**

from influxdb import InfluxDBClient
import matplotlib.pyplot as plt
import numpy
from datetime import datetime
import select

influxdb_client = InfluxDBClient(host=‘localhost’, port=8086, database=‘Projektarbeit_PV_Anlage’)

startdate = “2019-11-23T19:15:06Z”
shift = 0 #

for x in range(0,365):

response = influxdb_client.query('select difference(E_PV) from PV_Energy_2019_2020 where time >= 
1574536506 + 24h', database='Projektarbeit_PV_Anlage')

My question is how can I add the shift function to the query so that I can shift the time?
I want that the second run start on the second day and not the first.

I am grateful for any help.**


Hello @yokutte,
Are you talking about the Flux timeShift()?
If so, then you need to use the 2.x InfluxDB Python Client which has backwards compatibility with 1.x.

Unfortunately, this capability (of time shifting) doesn’t exist for InfluxQL. There is a |shift() node for Kapacitor within the TICK stack.

But I think the simplest solution is for you to use python to achieve the time shift. Something like:

import datetime
mydatetime = <date of my influxdb result>
onedaylater = mydatetime + datetime.timedelta(hours=24)

Finally, there are two advantages to using the 2.x Client with backwards compatibility:

  1. you can query with Flux, and therefore use the |>timeShift()
  2. you can convert your result to a pandas DataFrame easily which could make similar transformations easier

Let me know if there’s anything else I can do to help!

**Hello @Anaisdg
Thank you for your reply.

unfortunately this solution does not work

mydatetime = 1574536506
onedaylater = mydatetime + datetime.timedelta(hours=24)

**When I debug it. I get the error message

TypeError: unsupported operand type(s) for +: ‘int’ and ‘datetime.timedelta’

I will try to solve it differently**

1 Like

Cool let me know what solution you find. Yah I didn’t try that out. I just thought something along those lines might work.