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.
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?
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.
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?
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?