# Queries for calculate the energy with integral function

#1

Hi all!
I’m a newbie of influxdb, and start to test influxDB with Grafana (latest version of both).
I’ve a data series of instant DC power (Watt) that flow in a rechargeable battery.
The power has positive value when battery discharge and negative value when battery charge.

I want to calculate the energy (Watt hour) for charge and discharge phase.

For do this, I’ve used the integral function using clause WHERE, and create these 2 queries in Grafana:

_cumulative_sum(integral(“value”)) / 3600 FROM “Battery_Power” WHERE (“value” > 1 ) AND \$timeFilter GROUP BY time(\$_interval)

_SELECT cumulative_sum(integral(“value”)) / 3600 FROM " Battery_Power " WHERE (“value” < 1) AND \$timeFilter GROUP BY time(\$_interval)

Often the query work fine, and show a chart as I expected, other times, on chart I not see a data of integral, but the value seem have an offset.
Someone has same issue, or I’ve completely wrong my query syntax?
Thank you!

#2

Hi, not sure if you solved this issue, but another attempt would it be to create sub-queries. I’m currently measuring the current power of an UPS, but if I want to have Wh, I need to first sum the instantaneous value for each phase, and then get an average estimate for that stream of data. Below a query with an example:

`SELECT mean("sum_xupsOutputWatts") FROM (SELECT sum("xupsOutputWatts") AS "sum_xupsOutputWatts" FROM "telegraf"."autogen"."xupsOutputTable" WHERE time > now() - 1h AND ("xupsOutputPhase"='1' OR "xupsOutputPhase"='2' OR "xupsOutputPhase"='3') GROUP BY time(:interval:) FILL(null))`