# Energy daily/monthly usage with two tariffs and displayed in grafana

**URL:** <https://community.influxdata.com/t/energy-daily-monthly-usage-with-two-tariffs-and-displayed-in-grafana/25365>\
**Category:** InfluxDB 2\
**Tags:** grafana\
**Created:** [June 10, 2022, 7:34am UTC](https://community.influxdata.com/t/energy-daily-monthly-usage-with-two-tariffs-and-displayed-in-grafana/25365 "2022-06-10T07:34:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RWilliams](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@RWilliams](https://community.influxdata.com/u/RWilliams)\
**Post date:** [June 10, 2022, 7:34am UTC](https://community.influxdata.com/t/energy-daily-monthly-usage-with-two-tariffs-and-displayed-in-grafana/25365/1 "2022-06-10T07:34:40Z")

</div>

Hi, New to influx and im trying to display energy usage per day(7day view) and monthly total.

My data is cumulative

| 0 | energy | Canal\_Side\_Supply\_KWH\_Value | 76722665 | 2022-06-03T06:57:56.117Z | 2022-06-10T06:57:56.117Z | 2022-06-03T07:00:00.000Z | U\_Energy\_Flux |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 0 | energy | Canal\_Side\_Supply\_KWH\_Value | 76722780.66666667 | 2022-06-03T06:57:56.117Z | 2022-06-10T06:57:56.117Z | 2022-06-03T07:30:00.000Z | U\_Energy\_Flux |
| 0 | energy | Canal\_Side\_Supply\_KWH\_Value | 76722997.86666666 | 2022-06-03T06:57:56.117Z | 2022-06-10T06:57:56.117Z | 2022-06-03T08:00:00.000Z | U\_Energy\_Flux |
| 0 | energy | Canal\_Side\_Supply\_KWH\_Value | 76723218.53333333 | 2022-06-03T06:57:56.117Z | 2022-06-10T06:57:56.117Z | 2022-06-03T08:30:00.000Z | U\_Energy\_Flux |
| 0 | energy | Canal\_Side\_Supply\_KWH\_Value | 76723440.46666667 | 2022-06-03T06:57:56.117Z | 2022-06-10T06:57:56.117Z | 2022-06-03T09:00:00.000Z | U\_Energy\_Flux |
| 0 | energy | Canal\_Side\_Supply\_KWH\_Value | 76723667.2 | 2022-06-03T06:57:56.117Z | 2022-06-10T06:57:56.117Z | 2022-06-03T09:30:00.000Z | U\_Energy\_Flux |
| 0 | energy | Canal\_Side\_Supply\_KWH\_Value | 76723893.46666667 | 2022-06-03T06:57:56.117Z | 2022-06-10T06:57:56.117Z | 2022-06-03T10:00:00.000Z | U\_Energy\_Flux |

What im trying to achive is the following with enegy usage for each time window ie 00 - 07 tariff 1 and 07 - 00 tariff 2

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/d/d58668be849354f8d2a12fe4f38aac9fb0b4f477.png)

what im struggling with is to display the difference between a set time rage that spans over multiple days

this is what i have so far, this is one query for 00-07 and i have another one for 07-00

> import “date”  
> from(bucket: “utilities”)  
> |\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
> |\> filter(fn: (r) =\> r[“\_measurement”] == “energy”)  
> |\> filter(fn: (r) =\> r[“LogGroup”] == “U\_Energy\_Flux”)  
> |\> filter(fn: (r) =\> r[“\_field”] == “Canal\_Side\_Supply\_KWH\_Value”)  
> |\> filter(fn: (r) =\> {hour = date.hour(t: r.\_time) return hour \>= 0 or hour \< 7})  
> |\> aggregateWindow(every: 1d, fn: last, createEmpty: false)  
> |\> difference(nonNegative: false, columns: [“\_value”])

i think i need to eithe use max - min or map to condition my data but thats where im failing

any help would be apriciated

thanks

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [June 13, 2022, 9:18pm UTC](https://community.influxdata.com/t/energy-daily-monthly-usage-with-two-tariffs-and-displayed-in-grafana/25365/2 "2022-06-13T21:18:55Z")

</div>

Hello @RWilliams,  
I’m a little confused. What do you mean by

> What im trying to achive is the following with enegy usage for each time window ie 00 - 07 tariff 1 and 07 - 00 tariff 2  
> I’m not sure what tariff or those numbers refer to.

Maybe you could annotate some of your screen shots?

Are you querying for two sets of data and then wanting to find the difference between them? Like e.g. the you have 7 day windows and you want  
(value from today (06/13) - last monday (06/06) )  
(the value from sunday (06/12) - last sunday (06/05)  
etc for the whole week?

If you’re looking to do that, I’d look into the timeshift function:

> **[timeShift() function | Flux Documentation](https://docs.influxdata.com/flux/v0/stdlib/universe/timeshift/)**
>
> timeShift() adds a fixed duration to time columns.

Also don’t feel shy to tag me so I don’t miss you.

---

<div class="post-metadata">

**Author:** ![Patrick808](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@Patrick808](https://community.influxdata.com/u/Patrick808)\
**Post date:** [June 14, 2022, 4:29pm UTC](https://community.influxdata.com/t/energy-daily-monthly-usage-with-two-tariffs-and-displayed-in-grafana/25365/3 "2022-06-14T16:29:44Z")

</div>

You are going to need to pivot the data into a column for each tariff after your aggregateWindow() function. That means you need to eliminate the filter for hour on your data, and push that into the record processing. You would end up with one record per day with a column for each time range. Someone else will be better than me at the actual flux code though…

---

<div class="post-metadata">

**Author:** ![RWilliams](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@RWilliams](https://community.influxdata.com/u/RWilliams)\
**Post date:** [June 22, 2022, 6:04am UTC](https://community.influxdata.com/t/energy-daily-monthly-usage-with-two-tariffs-and-displayed-in-grafana/25365/4 "2022-06-22T06:04:03Z")

</div>

Hi Anaisdg,

I have one data set of data at 30 min intervals. This is the energy used and is cumlitive.

i have two energy tariffs.  
1 Between 00:00 and 07:00 this is tariff 1 at is charged at 20p per kwh used  
2 Between 07:00 and 00:00 this is tariff 2 at is charged at 27p per kwh used

what i would like to achive is to extract the data into three columes

1 date ( by day)  
2 difference in data between 00:01 and 07:00 (first and last values)  
3 difference in data between 07:01 and 00:00 (first and last values)

from the three columes make a stacked bar graph that stacks columes 2 and 3 and used the day to group all data.

i hope this makes sence.

thanks
