# Calculate kWh

**URL:** https://discuss.elastic.co/t/calculate-kwh/188419
**Category:** Kibana
**Created:** [July 1, 2019, 10:58pm UTC](https://discuss.elastic.co/t/calculate-kwh/188419 "2019-07-01T22:58:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![livio.zanol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/livio.zanol/32/49222_2.png) [@livio.zanol](https://discuss.elastic.co/u/livio.zanol)
#### Post date: [July 1, 2019, 10:58pm UTC](https://discuss.elastic.co/t/calculate-kwh/188419/1 "2019-07-01T22:58:39Z")

</div>

Good day/night everyone! I'm new to ELK stack, and was able to make a lot of visualizations that I needed, but I'm having some trouble creating this specific Metric vusualization. Can anybody help me?

* * *

TL;DR:  
Having the measure in Watts every 5 seconds from 3 different sensors pushed to Kibana, I need to create these 3 Metrics visualization:

1. Current selected period kWh (kilo watts per hour);
2. Last month kWh;
3. Current mont KWh.

* * *

Below are the values pushed to Elastic Search by Logstash every ~ 5 seconds, which are from 3 different sensors:

**val.ap\_pw** is a number in Watts that I want to make the calculation.

||Time|@timestamp|sensor|val.raw| **val.ap\_pw** |val.irms|

||Jul 1, 2019 @ 19:31:36.445|Jul 1, 2019 @ 19:31:36.445|en\_sensor2|508| **129,11** |1,02|  
||Jul 1, 2019 @ 19:31:35.426|Jul 1, 2019 @ 19:31:35.426|en\_sensor1|521| **345,15** |2,72|  
||Jul 1, 2019 @ 19:31:30.359|Jul 1, 2019 @ 19:31:30.359|en\_sensor3|506| **84,37** |0,66|  
||Jul 1, 2019 @ 19:31:29.341|Jul 1, 2019 @ 19:31:29.341|en\_sensor2|501| **122,36** |0,96|  
||Jul 1, 2019 @ 19:31:28.323|Jul 1, 2019 @ 19:31:28.323|en\_sensor1|522| **344,54** |2,71|

So, using pure math, to calculate kWh for any given time period I need to do the following:

- Calculate arithmetic mean (Average) for the individual sensors for the selected period (Let's call them M1 to M3)
- Sum the means (Let's call it Msum = M1 + M2 + M3)
- Subtract initial date from final date in the selected period (Tdifference = Tfinal - Tinitial (presuming the result will be timestamp in seconds))
- Calculate the hours in the current period (Thours = Tdifference \* 60 \* 60)
- Multiply the hours in the currently by the Sum of the means (Wh = Msum \* Thours)
- Divide by 1000 to have kWh (kWh = Wh / 1.000)

Or: **kWh = (M1 + M2 + M3) \* (Tfinal - Tinitial) /60 /60 / 1000**

Reading other questions related (like 'calculate bytes per second'), I've created this Metric below using Visual Builder, but is not showing the right value:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/9/2907f8d31180be7eb3f0a3bcdf0494382ec19752.png)

Actual usage:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/9/0901c98bf02638f93d99b4a39a88caef5d0d5f54.png)

As can be see, the result should have been something around 0,55 kWh

Can somebody help me on this? Thanks in advance.

---

<div class="post-metadata">

### Author: ![livio.zanol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/livio.zanol/32/49222_2.png) [@livio.zanol](https://discuss.elastic.co/u/livio.zanol)
#### Post date: [July 2, 2019, 2:23pm UTC](https://discuss.elastic.co/t/calculate-kwh/188419/2 "2019-07-02T14:23:02Z")

</div>

Managed to do similar calculations using this math function:

_params.ap\_pw \* (last(params.\_all.ap\_pw.timestamps)-first(params.\_all.ap\_pw.timestamps)) / 60 / 60 / 1000 / 1000_, but I still have 2 problems:

1. The Avarage calculation _excludes_ missing values, but the initial/final timestamp doesn't, which will give wrong kWh results when you have missing values on the dataset.
2. I couldn't agreggate the 3 values to generate only one kWh value.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/f/2fec58e47047eef2e313b69a763724ac9ef8982b.png)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 30, 2019, 2:23pm UTC](https://discuss.elastic.co/t/calculate-kwh/188419/3 "2019-07-30T14:23:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
