How to connect Wi-Fi smart plugs to the Energy Dashboard?

I’ve got a bunch of Nedis and Tuya Wi-Fi smart plugs with built-in power meters connected to HA using the standard Tuya integration.

How can I set them up to work with the Energy Dashboard?

The Tuya integration only provides momentary power readings, not cumulative power consumption. I saw a reference in “Integrating individual device energy usage” to using the “Integral (Riemann sum integral) Integration” for calculating a device’s energy usage, but there aren’t any clear examples of how to do it.

The exemples are in Integral (Riemann sum integral) Integration. i will help you to understand it:

sensor:
  - platform: integration
    source: sensor.current_power
    name: energy_spent
    unit_prefix: k
    round: 2
    max_sub_interval:
      minutes: 5

copy this code to your config.yaml

  • Replace sensor.current_power with the actual entity ID of your power sensor (Tuya smart plug).

  • energy_spent name of the new entity.

  • The unit_prefix: k ensures the energy is calculated in kilowatt-hours (kWh).

  • The round: 2 rounds the result to two decimal places for better readability.

1 Like

Thanks!

Just a few questions, if you don’t mind, so I can get a better idea of how things work “behind the scenes”:

  1. What does “max_sub_interval” mean in practice?
  2. Does “minutes: 5” refer to a sliding window of samples from the last 5 minutes or something else?
  3. Is this sensor data stored in the database too, and if so, how often?

BTW, this will, among other things, be used to read power consumption of things like air heat pumps and water heaters.

You’re welcome!

The max_sub_interval sets the maximum time for integration updates when the source value hasn’t changed. This ensures the integral is updated at least once during this time, even if there’s no change in the input. Setting it to 0 disables time-based updates and only updates when the value changes.

In a simple scenario, if your water heater is OFF, the sensor will continue to update multiple times. Once the integration detects that the value hasn’t changed (i.e., the water heater remains OFF), the sensor will update every 5 minutes until the value changes.

For more accurate readings, set it to 0. I didn’t include it in my setup because it’s optional, and the error between the actual monitor and the HA sensor wasn’t significant.

One small suggestion: it would be great to mark the answers as a solution so others with the same issue can quickly find their answer.

Thanks for the feedback!

Although it seems I still don’t fully understand how HA’s internals handle data usage. All Tuya power consumption values should already be available in the database, so I don’t get why integrals are needed at all. Shouldn’t it be possible to calculate an absolute value over a specific time frame directly? Or am I missing something here?

Also, are all these sensors (ie energy_spent_xx) stored in the database too? If so, how often is the sensor data updated?

Just a guess, but is max_sub_interval the preferred approach for a water heater that’s only turned on briefly every 4–5 hours for maintenance heating and runs more frequently during mornings and evenings?

But how about air heat pumps that run constantly during colder weather but vary in consumption depending on the temperature? Would it make sense to use something other than max_sub_interval in that case?

Btw, are the settings below correct for tracking energy usage over the last hour and last 24 hours for a water heater?

Last hour

sensor:
  - platform: integration
    source: waterheater1.current_power
    name: energy_spent_last_hour
    unit_prefix: k
    round: 2
    max_sub_interval:
      minutes: 60

Last 24 hours

sensor:
  - platform: integration
    source: waterheater1.current_power
    name: energy_spent_last_24_hours
    unit_prefix: k
    round: 2
    max_sub_interval:
      hours: 24

Thanks again!

EDIT:
If you happen to know of any good explanation or documentation on how HA stores and handles data points from sensors in this regard, feel free to share a link. Thanks!

It seems there was a misunderstanding. The interval setting needs to be small because shorter intervals provide more accuracy. If the interval is too large, such as 24 hours, you risk missing critical data when a device turns on and off frequently (e.g., every 3 to 4 hours).

Let me clarify: the interval isn’t about constant updates regardless of the device state. Instead, it limits how often calculations occur when the device is off. For instance, when a device is on, the kWh consumption increases, and Home Assistant fetches data from the Tuya plug at a default interval (e.g., every second). During this time, the calculations are frequent and accurate.

However, when the device is off, the kWh value doesn’t change. Home Assistant doesn’t need to calculate the consumption as often because the number stays the same. This is where the max_sub_interval comes in. It sets how often Home Assistant updates calculations when the value isn’t changing. For example:

  • If the interval is set to 5 minutes, Home Assistant will wait 5 minutes before recalculating when the value is static.
  • If the interval is set to 24 hours, Home Assistant won’t recalculate until the full 24 hours have passed, even if the device turns back on during that time. This means significant changes might be missed.

So, a shorter interval like 5 minutes ensures that calculations happen more frequently, even when the device is off, preventing missed updates if the device turns back on within a short time.

We use the Integral (Riemann Sum Integral) to convert from watts (W) to watt-hours (Wh) because watt-hours represent energy consumed over time. The power (in watts) varies with time, so integrating (summing) the power values over a time interval gives the total energy. This process calculates the area under the power vs. time curve, accurately accounting for variations in power usage. This is pure physics

Yes, the data will be saved over time, allowing you to view the diagram anytime in the logbook.

Great, thanks so much for the explanation!!

I do understand the physics, but it was the integration itself that I clearly didn’t fully grasp until now — specifically, that the “Riemann Integration” is essentially just a simple but effective resampling engine.

That said, I still don’t fully understand when and how the Riemann Integration stores the resampled values (as in this example using the sensor “energy_spent”) or the scope of the sampling itself (i.e., the start and end times). Is it controlled by the source when it updates, or does an internal timer trigger the resampling?

Found this:

I’m not an expert, but I think it might be possible to automate the transformation to a mostly lossless output if the sample rate is clean enough, as in the case of Tuya. There seem to be other metods like this suggestion: Riemann sum integral sensor not updating while source sensor value is stable · Issue #88940 · home-assistant/core · GitHub

It might also be feasible to perform some gradient analysis or fit a curve to better understand the data’s characteristics and automatically select the most suitable method. Alternatively, the integrations could provide useful insights to help you choose.

Do you think this would be too complicated to implement?