How to properly track energy consumption (MQTT & Tasmota)

Hello everyone,
well, I own a bunch of Shelly 2.5 and 1PM which are setup as MQTT sensors.
In addition, I own a bunch of power sockets running Tasmota.

What I would like to do is track the daily power consumption in kWh (or Wh).
I have setup InfluxDB and Grafana as well.

Current power consumption in W is easily tracked as each sensor offers this value. And hence just as easily displayed in Grafana by plotting that sensor.
However, the consumption per calendar day in kWh seems tricky because the sensors don’t all offer this natively.

For me it should be possible to create a fake sensor that simply calculates the power consumption based on the current consumption as tracked in the database.

So simply sum up all Watts between midnight and midnighr and multiply by 1000 to get kWh and then divide by 24 to get kWh.

But how is this done?

Ideally the sensor would also need to check the status before midnight because only changes in consumption trigger a new value push. So if at 23:59 1000 watts are being consumed, then this would still be the case at 00:01 but the fake sensor would think it is 0 W because there is no data point.

I hope someone can help me out :frowning:

Best regards
Alex

I think it’s going to be more complicated than you think.

watts are instantaneous power. you can’t just take those instantaneous watts and convert to kwh. you have to know how long the watts were at that value then do the conversion based on that time frame.

Oh, btw, your conversion was off.

kwh = w / 1000 x hours

for example you are running a light bulb that draws 100 watts. and that bulb is on for 2 hours.

the instantaneous draw in kw = 100 / 1000 = .1 kw

and since it ran for 2 hours kwh = .1 kw x 2 hours = .2 kwh

but you will have to do that continuously for every hour/minute/second the device is running.

you say the device only updates on a change in watts so you can take advantage of that by using the last_changed time.

So, you can create a sensor to hold the ever changing daily total of kwh then update that sensor each time the state of the first watts sensor changes.

you can calculate the new kwh total sensor by using the current number of watts for the amount of time between now and the last time the kwh sensor was updated.

and, again, the watts could change every second too so you will be doing that calculation for each sensor potentially every second.

Integrate the power sensor to get an energy sensor:

Feed the integral sensor to a utility meter with a daily cycle to get daily energy.

3 Likes

@finity : I apologize. Of course it should habe been devided and not multiplied. Stupid of me.

@tom_l : Quick question. If I feed an energy sensor (in watts) directly into Utility Meter as a daily sensor, what happens then? What result would I get?

Integrating power (W) gets you energy (kWh).

This energy sensor will keep increasing. The utility meter gives you the daily cycle.

It does exactly what you want, no maths required.

The only thing to make sure you get correctis the method. There is a note about it in the ducments.

That’s what I thought. But I created sensors in utility meter for all of them but in Grafana they only show up with the unit “W”. So it seems the daily consumption is not converted to kWh.

utility_meter:
  #Gosund/Tasmota
  daily_energy_plasma:
    source: sensor.plasma_energy_power
    cycle: daily

  daily_energy_travel:
    source: sensor.travel_energy_power
    cycle: daily
.
.
.

Is there something else I need to do? Is this sensor just adding up all energy consumed in Watts, so total wattage per day?

Show me your configuration for the Riemann sum integral sensors. That is what converts power to energy.

Then you use those sensors in the utility meter.

1 Like

Oh, okay, then I misunderstood. So I do need both, the Riemann sum and the utility meter. I thought Utility Meter had a built in Riemann sum.

1 Like

Yep you need both.

Your sensor (power) → Riemann sum (energy) → Utility Meter (energy, daily cycle).

1 Like

Cool.

I didn’t know about that.

Definitely makes the calculations easier. :laughing:

Hmmm, strangely when I set the unit to kilo it miscalculated. Seemingly kept the value but added the kilo. So 1500 Wh were then 1500 kWh.
Need to look into this tomorrow.

I am wondering about the method. What is really the difference. I do have an electrical boiler. I also have a fridge, big flatscreen and also smaller items like power amps or a raspberry pi. So could I set all of them to “left” or would that not work?
Especially since to my understanding left and right belong together and the shape (trapezoidal) should be opposite rectangle etc.

And I would have thought that middle would be better than left or right.

Don’t set the kilo prefix.

Unless they are continuously varying (like a sine wave) left is best to minimise the error.

Petro did a great write up here on why:

So, I have set everything up and let it run the last few days. Switched to method left yesterday.

But what confuses me is the way the utility meter data is output.
A daily cycle should have only one single data point per day. It should show the total energy of one day.
Mine however look like this:


Upper image showing the actual sensor data, so the Watts being consumed in real-time and the bottom one showing the utility meter daily which went through the Riemann sum first.

No, that is not how it works. The Utility meter resets to 0 at the beginning of the cycle (day) and counts up during the day so you can see how much has been used so far this cycle, before resetting again at the end of the day.

It is working as expected.

Okay, then I misunderstood.

And the Riemann sum keeps adding then? So it increases indefinitely whereas the utility meter resets daily?

Hmmmm… so is there another tool that gives me what I want?
I simply want to know how much I consumed per day. So, from what you say, I am gueasing this would be the max value of the daily output.

By the way, is there a way to have Riemann and utiliy meter be applied also to data recorded before creating/modifying those virtual sensors? The data of the sensors is stored in the database after all.

Correct.

You can make a triggered template sensor to record the value at the end of the day.

template:
  - trigger:
      - platform: time
        at: "23:59:55"
    sensor:
      - name: Daily Maximum Energy
        state: "{{ states('sensor.your_utility_meter_here') }}"
        unit_of_measurement: 'kWh'

No.