# Calculate Energy Consumsion manually

Hi.

I am logging my energy consumsion, but my sensor gives me only the current energy consumsion, e.g. 200W, not the total kWh.

Can I manually calculate the total kWh with thus “current” Watt? At the end I want a statistic of the kWh per hour/day/month/year…

Thanks

The calculation, in principle, is simple. Watts times hours divided by 1000
equals kilowatt-hours, kWh.

How easy this is to do in practice depends mainly on when you get updates from
the sensor.

If you get regular Watt readings every second, or every minute, or every hour,
then you can very easily convert this into kWh - if it’s every second,
multiply by 3.6; if it’s every minute, divide by 16.67; if it’s every hour,
divide by 1000.

The challenge comes if you do not get Watt readings on a regular basis,
because then you need to know the time interval since the last reading, as
well as whether it is safe to assume that the power consumption has been level
during that period of time, in order to calculate the energy consumption.

If your sensor tells you when the power consumption level changes, you should
probably multiply the time since the previous reading by the value of the
previous reading, in order to get the energy consumption during that time,
because the new power level has only just taken effect, so until then, it was

Either way, in order to get the total energy consumption over an hour / day /
month / year you then need to sum these calculated values over the appropriate
time interval, which is pretty simple to do in Grafana.

All I can say beyond that is that the ease of finding out what the previous
power level was, and the time interval between that previous measurement and
the new measurement, depends on what your back-end data store is.

With any luck, your power sensor will give you regular measurements every N
seconds and you can do the simpler calculations.

Regards,

Antony.

Hi! Thanks for the quick answer. That is exactly what I was searching for. Unfortunately my sensor does not give me measurements on a regular time.

I will have a look how I can calculate the values. I am using Openhab, so I think I can implement a rule which will do this for me und will save the data to my InfluxDb.

Thanks a lot!

What @pooh said is spot on. In terms of the irregularity of your readings - it sounds like you’re using Influx as your data source, in which case I think you can account for that irregularity by adjusting your query.

For example

``````SELECT mean(power) FROM measurement WHERE \$timeFilter GROUP BY time(1s) fill(last)
``````

will return a set of values with constant one-second spacing, where for any periods without data the last recorded value is filled in. You can then use the above in a subquery to do the kind of math that @pooh describes.

Perfect! Thanks! I will try it within the next days.

Hey… I am here again

Yes I have an InfluxDB as data source.
Could you help me to set up the query with your Idea(s)?

Here is my query, which does not give me any data:
`SELECT mean("value")*3.6 FROM (SELECT mean("value") FROM "autogen"."Item_Garten_Steckdose_Solarpanel_Poolhuette_EnergyCurrent" WHERE \$timeFilter GROUP BY time(1s) fill(previous)) WHERE \$timeFilter GROUP BY time(1h) fill(null)`

I think you have a small error in your query. If you run your sub query by itself (always a good idea by troubleshooting!) you’ll probably find its output field is called `mean` and not `value` - so your outer query needs to be adjusted accordingly. Just my guess

Perfect! Thanks! I think it´s working

I will check it within the next days…

Hi, it seems that the data/calculation for the 1 day per hour is wrong. Is it really necessary to multiply it by 3.6? Or is it correct this way?
`SELECT mean("mean") FROM (SELECT mean("value") FROM "autogen"."Item_Garten_Steckdose_Solarpanel_Poolhuette_EnergyCurrent" WHERE \$timeFilter GROUP BY time(1s) fill(previous)) WHERE \$timeFilter GROUP BY time(1h) fill(null)`

Also the 14 days per day statistic seems wrong:
`SELECT (count(distinct("mean")) *3.6)/1000 FROM (SELECT mean("value") FROM "autogen"."Item_Garten_Steckdose_Solarpanel_Poolhuette_EnergyCurrent" WHERE \$timeFilter GROUP BY time(1s) fill(previous)) WHERE \$timeFilter GROUP BY time(1d,-2h) fill(null)`

Any idea?

Well it depends on if you take the `sum()` or the `mean()` of your readings. If your readings are in Watts, and you are taking the `mean()` while grouping per hour (as you appear to be) then you just need to divide by 1000. In other words, if your mean load is 1000W, over the course of 1 hour that’s 1kWh of energy. The 3.6 factor comes into play if you’re doing the `sum()`.

I’m afraid I don’t understand what’s happening in that query. Specifically, what’s the purpose of the `count(distinct("mean")`? (also could you clarify what you mean by “14 days per day”?)

Sorry, my english is very bad :-/

This is my 1 days (per hour) statistic:

Seems to be corect with “mean”. The readings are in Watt. So I don´t need to divide by 1000, because I want Wh for this graph.

This is my 14 days statstic:

I think know it is clear what I mean, or? I got this query from an example…

thanks for your help all the time! Very nice/kind!

Yup, that looks good!

I’m also now clear about what you meant by the 14-day daily chart. You basically need daily values. Try something like

``````SELECT sum("mean")/3600 FROM (
SELECT mean("value") FROM "autogen"."Item_Garten_Steckdose_Solarpanel_Poolhuette_EnergyCurrent" WHERE \$timeFilter GROUP BY time(1s) fill(previous)
) WHERE \$timeFilter GROUP BY time(1d,-2h) fill(null)
``````

Your inner query will yield mean power values on a second-by-second basis. You can think of each of those representing a number of “Watt-seconds”. Each Watt-second is 1/3600th of a Watt-hour. So if you sum all of the Watt-seconds you have and divide by 3600, you’ll get Watt-hours.

In fact you can also represent your first query in exactly the same way, i.e.

``````SELECT sum("mean")/3600 FROM (
SELECT mean("value") FROM "autogen"."Item_Garten_Steckdose_Solarpanel_Poolhuette_EnergyCurrent" WHERE \$timeFilter GROUP BY time(1s) fill(previous)
) WHERE \$timeFilter GROUP BY time(1h) fill(null)
``````

just with a different aggregation period (1h instead of 1d). Here `sum("mean")/3600` is equivalent to `mean("mean")` since there will be 3600 readings per hour.

Hope this helps. By the way I also use Grafana for solar power monitoring also, so it’s cool to see your use case!

Yeah! Perfect! Seems that the values are correct now.

Thank you very much.

You said you also use it to monitore your solar power, do you have any other ideas?

What I want is the total kWh. I am doing it like that:
`SELECT sum("mean")/3600/1000 FROM (SELECT mean("value") FROM "autogen"."Item_Garten_Steckdose_Solarpanel_Poolhuette_EnergyCurrent" GROUP BY time(1s) fill(previous))`
Correct, or?

What would be nice, would be a per month and per year statistic, but I read with FluxDb it is not possible, I need Influx for that? Or is it possible?