Graphing Energy

No, you are missing the basics and simply want it all just like putting code together by copy and paste without the background.

You want an energy graph and most likely an hourly one and then a daily one and so on but that needs preparation cause energy is not delivered by the cheap chineses plugs / devices. You can get those and have to deal with nothing else but these are made in the EU by Shelly cause they provide another sensor beyond current, volts and watts which delivers you Wh

But you have to do that on your own with SonOff and what you call an integration is just the switching part and most likely not the accounting part.
Watt is power x time you will get Wh or energy consumption.

That is achieved by the riemann integration so you have to do the math first and create such sensor which will return a Wh figure you are keen to get a graph when and where what amount has been used.

You simply have to under stand that you need to calculate the area below a watt graph over time and that is achieved by the riemann integration.

SonOff Pow is simply the product name and there had been multiple revisions too cause I have had multiple of those before for measuring power production and consumption.
They work all the same way except some better ones but those are as mentined above usually not from china but by Shelly made in EU or hungary I guess. Those are doing the math for you and provide a Wh sensor which means they do count the energy too for you.
Easier to integrate but it is basically the same: understanding the correlation.
This is a set of power plugs and watt graphs over the last hour …
image
and the area below each graph has to be calculated for every tiny change and then in total the area represents the Wh consumption of each plug.

Here is the energy consumption of the past 24 hours of such yellow plug which has consumed since the last hour began 6 Watt hours and usually is consuming around 20 Wh which means that about 20 minutes of an hour are over …
image

I can only use what is easily available in local online stores. Shelly in our local store will get you some mobile phone cover https://www.daraz.pk/catalog/?q=shelly&_keyori=ss&from=input&spm=a2a0e.searchlist.search.go.26a96176NYDnmd :crazy_face:

Yes, I can understand the math but How do I implement it? Energy = Area under the curve of Time vs Power graph. We are end-user/noobs simplifying the installation/configuration step i.e copy/pasting the code will make Home assistant more accessible for more Noobs around here. There are only 35 installation of Home assistant in my Country. :upside_down_face:
Found Shelly’s official aliexpress store but shipping is prohibitive https://www.aliexpress.com/store/912134003?spm=a2g0o.productlist.0.0.3d9d405fqeo4io

Whatever you wanna achieve I had told you what you had to do to get it done right:

The Riemann integration, then most likely power metering and some other points.

This Riemann integration is well documented here in home assistant too if you do a search for it or follow the link below. You do not even have to do any math yourself cause it runs automatically based on your parameters which you also have to read and understand first cause otherwise you might face strange results caused by your parameters if you had chosen them not right / carefully.

And before you start doing anything further try to get a good old energy meter to get a calibration and to become aware what 1 kWh measured by SonOff is in reality worth cause there are many factors kicking in the whole challenge:

  • measurements from SonOff hardware / sensor
  • kWh integration by Riemann formula
  • time measurements by your HA server and
  • any kind of delays between server and sensors

Therefore I first plug in my energy meter that delivers reliable measurement results and then I push in the tuya or SonOff plug. After 1 week I can compare the figures of that particular tuya / sonoff plug versus my real power meter cause the difference can be quite high and might vary between each of those sonoff plugs, I mean even inside a batch of the same Pow R2 I had found deviations of 5 - 8% back then before I left the SonOff ship in 2019.

Maybe now you understand that this is stuff you really have to understand frm the basics and where good youtube explanation videos are quite rare that start from the very beginning to the Wh you are looking for - at least for local tuya which took me 2 weeks to figure out how to get these data, how to deal with iits decimals, delays, integrations and how to sum all those plugs up to get 1 figure over all cause once you have running the first POW2 meter you will add several more and asked for sums, then sums by hours / nights and then days, weeks, months and quarters.
And for that there is a lot to understand first but all of that starts in the basics of Watt sensor and getting the energy consumption as Watt over time by the riemann sum integral.

Do not forget: every result / measurement in HA can be crap as long as you do not have a good powermeter to cross check how well your SonOff unit is working / measuring, how well the HA server is doing the timing of the sample rate and the delay between sensor and server .

Do not trust the figures SonOff is providing cause otherwise you will be astonished how much bigger your electricity bill might be after one year than SonOff might have told you.

If those SonOff figures are off a lot from the good old power meter tool (which is about 30 - 60€ here in europe) than you might have to change Riemann parameters depending on the use case you have at this particular SonOff Powermeter / switch cause for high power devices connected to it and turned on and off quite often you can use a different parameter for conservative values which will mean that the consumption displayed by HA is higher than the real consumption and not vice versa.
The key parameter for that is called “method” and described as "Riemann sum method to be used. Available methods are trapezoidal , left and right

In case you have an appliance which produces spikey consumption (like an on / off electrical boiler) you should opt for the left method to get more accurate readings.

And beyond that there is also a parameter that touches the topic of sampling size or rate depending on which manufacturer’s plug or sensor you use.

Sampling_size: 20 for example is the maximum for local tuya implementations which describes that HA will ask the plug / sensor 20 times per minute or every 3 seconds for the most current value. That 20 sampling size will create 1200 values per hours from each sensor or plug just to give you the power curve over time for that particular plug and which then will also create the same amount of data for the Wh measurements cause once you get 1200 values per hour from sensor 1 the riemannn integral will also deliver 1200 counter values for the energy consumption cause that can change every 3 seconds too or even not at all if there is no consumption, but then the counter will still reply 1200 times the same amount of energy.

For example a 2.400 figure as reply (which of cause means Watt if you ask a watt sensor) from the son off plug would mean that

for those 3 seconds of an hour or 3 sec / 3600 seconds per hour or
3 / 3600 hours x 2400 Watt power are consumed or
1 / 1200 h x 2400 W = 2 Wh in total - but for the last 3 seconds only.

Can 2 Wh in 3 seconds really be true ?

Damn easy cause from 3 seconds to 3.600 seconds of 1 hour you have to multiply by 1.200 and if you multiply 2 Wh x 1200 you will get 2400 Wh
And a 2400 Watt device will have used 2400 Wh after 1 hour of usage.

Now: Your home assistance server has to do that riemann integration for every sensor and every 3 seconds over and over again.
Imagine you had turned the device of after those 3 seconds for the next 57 seconds then the total consumption would stay at those 2 Wh … but imagine your would have turned it on for 20 minutes at the end of this particular hour then you would have to add 20 / 60 hour x 2400 Won top of the first 2 Wh or 1/3 h x 2400 W + 2 Wh = 800 Wh + 2 Wh.
The total consumption of that 1 hour would end up in 20 minutes and 3 seconds running time and a total consumtion of 802 Wh in total.

And of cause this is an easy and simplified example cause in real life the integration is not counting how long it is running with the same Power (20 minutes x 2400 Watt), no, it is constantly polling the Watts used every 3 seconds and doing the math for that if you run the sensor with a sampling_size of 20 … and if the Power goes a bit up to 2460 W it will do it for 2460 W and the next 3 seconds for maybe 2340 W.
Remember: if the plug had used 2400 W from the last reading and now you would get a 3600 W reading your question might be how the riemann integral is calculating that as a digital jump after 3 sec. from 2400 W to 3600 W or is there a kind of extrapolation and linearisation by assuming that on the last third of the 3 seconds the power already rose half the way and reaches peak not at the beginning of the next measurement cycle but 1 second in then the results might be better or worse, but for sure different.

All this is what you need to be aware to understand such possible results and figures right or to raise questions if these figure can be true or not or if you might have made a mistake. And finally you will also have to deal at the same time with decimals which can also cause a lot of trouble and headache if you cut those too early and too much cause 0,5 Watt measured 1200 times an hour make a difference in the long run if you cut or round to 0 zero decimals cause the difference of 1 W over a year is simply a 9 times bigger figure - but in kWh.
Why ? 365 days 24 hours / day just means 8760 h or roughly 9000 times higher figure or 9 times bigger in kWh if you track in W. 1 Watt = 9 kWh more consumption in a 247 use case.

And these are just 2 steps on the journey to get and understand consumption figures, Riemann integration, sampling size and then decimals / round and so on to get 1 HA consumption figure which you then have to crosscheck with your energy meter results to find a calibration factor that will match or make the HA figure fit to the the real world figures of the power meter cause those are what you will have to pay for and you wanna know in advance.

Shelly ones make it easier cause they do deliver the Wh you wanted to have right now but the basics and misconeptions are the same for all those powersensors, even though the shelly deviation was at least a lot lower than back then when we had gotten a batch from SonOff (all Pow 2 so 2nd generation back then in 2019).

Good luck, take your time and go step by step and you will succeed but it might take quite more time than you had ever considered cause it took me overall to get everything right (building counters, building sums over plugs / powermeters, understanding the basics of calibration, getting behind the sum of riemann integrals and its possible errors, understanding that a to high sampling rate can stop the sensors from working for hours or even days, …)
If someone might give you SonOff formulas and examples then it might deliver you quick results that can be useless if you have not understood any single step and cause for mistakes cause I can tell you that you will never recognice what is happening. Why ?
If the sensor stop working the graph will not fall down but you might find in the past values like a straight line cause the W will stay the same in your graphs: you will see the 20,5 W for 6 hours but in reality the riemann formula will not have counted anything cause the overloaded tuya sensor tend to report the same value as before on the display but not in the math. After 3 days not checking each and every sensor there is close to no chance to get behind such event and mis measruement I can tell you. But I got behiind that due to a simple fact: my power andn energy meter had before shown that the tuya were delivering figures that were 100% right - spot on. But that one morning I had noticed that there was a gap between the power meter and real world compare to the HA values delivered by tuya and the Riemann integration. So I had to check it out and build a graph for each plug about the past 24 hours and there I found this:

This is in Zoom mode 96 hours ago and there is a 3 hours frame when the power consumption had not been working but Watts were shown … as having said 3 hours in big zoom over the past 96 hours (you can see about 24 hours 4 days ago). Now let us know where the error has happened and how to identify such an error without having the knowledge I have told you to gain ?

Here it is


Right in the beginning you see horizontal lines over 3 hours - how can you get such perfect horizontal lines over 3 hours even in total consumption if freezers kick in from time to time and have a specific pattern?

Check the blue graph in the upper graph and look for the spikes - those are from the fridge and the high peak caused by the compressor motor that needs high power to start to turn against high resistance — but over the 3 hours in that night there was not a single peak, not in overall consumption of kitchen nor in the fridge meter … cause all were dead or suffered from overload due to too high sample size of 20 ! Since I turned it down to 15 or every 4 seconds I do not have any issue - but I had to learn it the hard way and copying code to gether would never have taught me what to take care for and how to get behind such things.

What you can see here in these 2 report excerpts are Watt used, but those figures shown there during the period of full flat horizontal line were not true cause during that the system was simply only recording the last stage or last successfull report over and over again while the Wh were not count in the Wh sensor.

I hope you have learned a lesson from that. Getting code and copying that together can be dangerous if you do not understand what is really going on or here going wrong cause the interpretation needs your knowledge, expertise and not guessworks cause otherwise you will never trust a watt meter or such sensor again in my opinion.

4 Likes

Thank you very much for this info!
Did not know about it, gonna use it for calculating traffic volume…

Once you know about it then it is very helpfull. But the first time I had heard about I was not so happy cause I simply just wanted to have my desired Wh now … but once you got into it and back into the glory days of math at school and all the examins it felt like coming home.

And of cause any integral / area below a curve can be calculated by that riemann integral sum. So good luck for your project (and be carefull with the method ‘left’ cause that is really only usefull in an environment full of spikey values that jump around from top to bottom cause in most other cases it will deliver to high values).

Therefore I can suggest to do the riemann integral in parallel for all those 3 methods which will offer you all 3 values to make a good decision for the right method or even do mix of those. But of cause that makes only sense if you have another measurement device you can trust an rely on like in my case that power meter but I do not know if you have such to get to a right decision. With the 3 rieman integral methods you can finally create your own new one like a recipe in the kitchen you take for example 60% of the left method value and 40% of the trapoezoid method to create your own “integral” - but this will need a real figure you can compare with like my power meter values.

This has been usefull for a friend of mine who had evaluated his own formula or mix over 6 months till it fits his use cases which are also different during winter and summer so he switches his formula twice a year.

Hi Ildar,
thanks for these great examples back in december. I could achieve quite a lot but I am struggling at a simple task.

If I want to get the font in bold I have to do what ?

I have searched and looked through your example but did not find.
I have tried

font-style: bold; 
font_style: bold;

but did not succeed. font-style was just a guess after I had seen font-size …
I guess it is super easy but right now I have no clue what to try else.

Final question: if there is a chance to format the value of a sensor in the european number format like “#.##0,00” and not the british / american one ?

Here is an example where I would like to get 2 decimal and thousands for example for euro or cost

  ha-card .states.flex::after {
    color: white;
    font-style: bold;
    font-size: 24px;
    content: "Wh: {{ states('sensor.counter_1_Wh') }} pulses\A €: {{states('sensor.counter_1_euro')}}";
    white-space: pre;
    background-color: rgba(50,50,850,0.9);
    padding: 5px 5px 5px 5px;
    border-radius: 5px;
  }

thx

image

type: custom:mini-graph-card
entities:
  - sensor.cleargrass_1_temp
show:
  labels: true
  average: true
  extrema: true
style: |
  ha-card .header.flex .name.flex .ellipsis {
    color: green;
    font-weight: 100;
  }
  ha-card .header.flex .icon {
    color: red;
  }

Do not guess. Use what is specified in docs.
https://www.w3schools.com/cssref/pr_font_weight.asp

Cannot tell you unfortunately.
My advice - ask about conversion in a separate thread, it requires using jinjia2 skills or something similar…

1 Like

Thanks a lot , that 100 to 900 scale is really nice to play with to get a nice layout without the typical black and with bold or normal. Like it a lot.

But there are 2 other points I do not get right: the figure has become white, the unit stays in the primary color of user settings I guess

image

And then this is not working cause the weight stays the same and the white is not a real white if you look carefully

  ha-card .header.flex .name.flex {
    color: white;
    font-weight: 100;
    font-size: 24px;
  }

as you can see here where it looks “dirty” not white.
image

I hope to get rid of these format issues soon like here the dot instead of a comma but I guess that will need a hard core replace “.” by “,”

By default colors of value & unit are different - remove all styles and you can see it.

To manage colors, use "card-mod":

style: |
  ha-card .states.flex .state .state__value.ellipsis {
    color: orange;
  }
  ha-card .states.flex .state .state__uom.ellipsis {
    color: cyan;
  }

image

Regarding number format - check these settings, MAY BE they are supported by mini-graph-card:
image

1 Like

I tried and it worked like before which means it did not work out like expected.

The title line / header as the unit do not really “accept” these colours fully cause they get darker. The figure is pure white as expected, the unit ‘kg’ and title above do get a shadowed white or so.

After all same result as before - deleting the other lines did not change anything.

And of cause I have used the custom / user settings cause the internal HA cards do work well only all the custom cards seem to miss the number format support.
The only way around would be adding this

style: |
  ha-card .states.flex::after {
    color: white;
    font-weight: 400;
    font-size: 24px;
    content: "GRUNDUMSATZ:\A {{ states('sensor.scale_3_basal_metabolism') | round(1) | replace(".","%") | replace(",",".") | replace("%",",")  }} kcal\A BMI: {{states('sensor.scale_2_bmi')}}";
    white-space: pre;
    background-color: rgba(50,50,850,0.9);
    padding: 5px 5px 5px 5px;
    border-radius: 5px;
  }

But a lot will complain about that - even though I do not have any other solution to get a simple “#.##0,0” european decimal format.

thanks a lot

As having said I do no longer use SonOff products cause their measurements where too far off back in 2019 when I had gotten the first batch.

I do not know why you did not read the infos available on github and use such data that you had provided here.
Life would be a lot easier for you to do things carefully with full attention cause you simply missed all that what is important in that link. I had stopped using SonOff products due to bad performance or bad measurements as I had told you 2 years ago but at least I read your comment and the link carefully. And if you own a SonOff POW there is a simple method to learn what this integration can offer for your particular POW.

CTRL + ‘f’
where you type in POW and will get hopefully results.

Just from that screenshot you get a lot: 25 hits and the first one sounds really serious like POW refresh intervalls for accuracy

Then there is even a list of tested devices ( I have had POW R2 version or 2nd release)

There is also a lot to read about the different modes supported with and without cloud you should have read carefully to make the right decision cause in a hurry you will might miss again the basics as before cause other wise you would not have asked all those questions which have been answered by the ressource you had provided, not we here or I.

And at least I have read those stuff from the birds eye perspective even though I have HA just for 4 weeks running and left the SonOff ship back in 2019.

You will find in the link you had provided even all the answers for your questions with the basic CTRL + f search cause hit number 21 on that page talks about Pow Power consumption and its services, which can count locally up to 100 days of power consumption (according to this docs - but you have to check if those are different for different versions of that POW)

And there is also the setup described as “use automation” and the sensor creation which at least mentions the daily consumption and 10 day consumption

Finally do not forget to take your time and do not things in a rush on the hunt for fast success cause otherwise you might miss important lessons as I had mentioned before which i had called for the tuya world ‘sampling_size’ and is described here, but in a kind of different issue that can cause issues in your environment but not in every environment which seems to have been fixed too.

I hope that all other that are on the search for a serious solution to visualize energy consumption over time will come across this topic and learn the basics that way instead of going on the hunt for copy and paste success.

The SonoffLAN integration delivers that sensor needed for a visualisation that mini graph provides … but that sensor is the manufacturers own secret how he is doing the measurement of Wh and what kind of riemann integral he is using cause you have to trust EWE totally while the other way I had described would give you the chance to proof all the details by simply measuring the Watts on your own and if your Wattmeter claims 80W and the sonOff sensor also sends a 79,9 W figure you know the deviation of that unit.

And from there on you are the master by using the Riemann integral to get your energy consumption right … or as right as you can with low deviation - or you use the blackbox sensor of the manufacturer which does the math for you (measuring the Watts used in every second and of cause adding those consumption figures of every second up cause a 3600 W hair dryer burns 1 Wh in every second on full power).

I like to understand the things and to know who will be responsible for what. If you will use the simple Wh sensor provided by EWE you will stuck with all those risks but also can benefit in case your HA integtration might suck and miss to measure cause the onboard counter of each POW will continue to count even if you HA has died meanwhile.

Up to you to go the easy convenient copy cat road or to understand and measure things.

1 Like

Thanks for the detailed response. I did read that thoroughly but was trying to deal with SSL issue, since I have enabled SSL I can’t access Homeassistant on the Android app.
To keep it relevant, how do I plot the consumption data shown in the screenshot in mini-graph?

Are you kidding ?
4 days ago you did knew what to do


you had showed us these aquarium screenshots

And now after you had found out your sensor you have lost how to get a chart of such sensors values ?
I mean you had gotten all the replies needed and even how to create a template sensor which is all what you need. Again a successfull Copy and Paste hunt might be fine but does not really bring you any benefit cause if you had learned at least a tiny bit then you would have asked other questions how or why your sensors are totally off as I had warned you the whole time: understand the whole thing like how a riemann integral works and what it is needed for instead of hunting for a polished curve of doubtful quality / message.

It might be time to take things seriously and do some math first otherwise you will rely on figures that are wrong and you will plot a dream world but not reality.

Watt = Volt x Ampere

that is the usual equation and if I take your figures from your 3 graphs even you should get doubts 233,97 V x 0,31 A = 72,53 W

while your graph called W had shown a nice 43,7 W figure. Pretty much off.

And even your latest sensor values are painting a similiar picture
235,99 V x 0,49 A = 115,64 W but your sensor reports 70,5 W
image

Do you really need a nice looking polished graph of pure guesswork or something serious?
The consumption figures are a result of Watt over time but if the Watt figure is doubtfull the Wh consumption can not be less doubtfull regardless which card you choose.
And the mini graph card just simply needs a working sensor to plot a graph, such one you had plotted in the beginning called ‘aquarium’.

Here is a graph of an hourly consumption report of a plug that is working right and showed a 24 h consumption between 0 Wh and 50 Wh - of cause per hour, so 0 - 50 W range.
image
And such graph can be developed from your aquarium graph or any other temperature sensor or any other example mentioned before and shown on that page.
The mini graph card is doing its job right … once you deliver the right input.

sorry too late, cause I was writing a reply when you were writing your.
This time he had asked about the mini graph card so it was no longer off topic as you had mentioned cause he had asked how to plot that sensor .

1 Like

I am using Sonoff POW2 to measure Voltage, Current and Power. I can display all these values in a mini-graph easily. How do I chart energy consumption and power used? It will require math calculations.

If you have sensors with all necessary data you may use mini-graph-card. There are examples for power consumption here. You can also use “group_by” option.

You may define template sensors for this and keep using mini-graph-card.

any links to the former and how do I define template sensors?

sorry,
but first you need to do basics.
And voltage current are not needed if you have already power or Watts.

If you have those you have to do the math first, which means power x time or watts x hours and that is achieved by the riemann integration that is supported by HA . But that is something way of this topic and will lead to further questions like energy countes, resets, sampling size and so on

Get the data first and then you can decide about graphs not vice versa.

that is the point I am struggling with right now.

I can group my weekly counters by date but that will only show me daily increasing bar even though I need a grouping based on weeks or months.

But the only option mentioned that might fit is called “interval” but without any example on github nor here and even google did not find an example for interval and how to use it for grouping by week or month.

But maybe you have an idea for that issue here
image

created by this

type: 'custom:mini-graph-card'
name: COUCH
smoothing: false
hours_to_show: 336
points_per_hour: 1
line_width: 2
line_color: orange
animate: true
height: 80
entities:
  - sensor.kwh_2_3_weekly
font_size: 70
show:
  graph: bar
  points: true
  icon: false
  labels: true
  name: true
group_by: date
aggregate_func: max
decimals: 3
lower_bound: 0
upper_bound: 2

On github I foud that group_by: date can also be hour or interval but not how or where is inverval can be defined ?

Thx for your support.

1 Like