Utility Meter with delta values and a data source that has almost always the same value

Hello,
I have a bunch of Tuya smart plugs with energy monitoring, set up with Tuya Local (the one from “make-all”). Among the sensors available, there is Power (in W) and Energy (in Wh). Energy is updated every half-hour with the energy consumption of the last half-hour. So to use it with HA’s Energy dashboard, I would need to make a running sum of that sensor. It seems to be what the helper Utility Meter was designed for, with delta values on.

It works well for loads that change all the time, because the energy consumption is different (even if slightly) every half hour, so Utility Meter helper picks up on the new value and add it to the running total. But for loads which are constant and long running, the Tuya Local Energy sensor has the same value every half-hour, and the Utility Meter helper ignore those value (it seems it thinks the sensor was simply not updated, while in fact it was updated but with the same value than before).

Is there any way to to “force” the update ? Either on a schedule (i.e. every half-hour, look at the sensor value and update the Utility Meter) or by detecting updates with the same value than before and treating them as real updates (and in that case, should the change be in the Utility Meter code and Tuya Local code)?

So far, I’m integrating Power instead (with the Integral helper), but it seems less reliable.

Thanks!

One way around it would be to feed your power sensor to the Riemann Sum integral helper to generate an ever increasing energy sensor. This can be done in the UI or YAML. Make sure to use method: left.

Yes, that’s what I’m doing in the meantime. But it would be reliable to use the on-device energy meter than integrating the power (for example, in case of very transient high power usage, that might not be registered by the power sensor)

The device probably performs an integration too, because what it will be measuring will be power.

Which will not matter too much for very short spikes. Whichever way it’s done will end up to be a function of resolution. Do you know how well the device deals with this? It’s best to check this assumption, because it may not matter.

Yes, you’re right, I should have checked the data before worrying. In the end, I found a resolution of 3 seconds for the Power sensor during short spikes. It’s good enough to just integrate that.

Here are all the details for the curious.

It seems to report power every one minute and 3 seconds (eh, why not) and energy every 29 minutes to 31 minutes. Here’s the last 24-hour data for a dehumidifier connected to one the Tuya plugs. The dehumidifier is either on (200W constant load, so 100Wh every half-hour) or off.

select
	to_timestamp(s.last_reported_ts),
	s.state,
	(to_timestamp(s.last_reported_ts) - to_timestamp(sold.last_reported_ts)) as last_reported_ts_diff,
	(to_timestamp(s.last_updated_ts)  - to_timestamp(sold.last_updated_ts))  as last_updated_ts_diff,
	(to_timestamp(s.last_changed_ts)  - to_timestamp(sold.last_changed_ts))  as last_changed_ts_diff
from states s inner join states sold on(s.old_state_id=sold.state_id)
where s.metadata_id = XXX and now() - to_timestamp(s.last_reported_ts) < '1 day'
order by s.state_id desc

First the Energy sensor. There should be 48 lines (one every half-hour) but there are only 22

Timestamp Energy last_reported_ts_diff last_updated_ts_diff last_changed_ts_diff comment
2024-09-08 23:23:23.591 +0200 92 00:30:35.505596 00:30:01.547341 null
2024-09-08 22:52:48.086 +0200 16 00:29:54.983868 00:30:01.444915 null
2024-09-08 22:22:53.102 +0200 25 00:29:38.365291 00:30:01.476069 null
2024-09-08 21:53:14.737 +0200 80 00:30:23.037163 01:56:33.910489 null device was on for two hours straight, so the next line (100Wh) should be repeated 4 times. Instead, there is a 1 hour 56 minutes “gap” in last_changed here
2024-09-08 21:22:51.699 +0200 100 01:56:17.41834 00:28:40.496219 null and a 1 hour 56 minutes “gap” in last_updated here
2024-09-08 19:26:34.281 +0200 28 00:29:03.35405 00:30:01.403074 null
2024-09-08 18:57:30.927 +0200 88 00:29:53.631866 00:30:01.485157 null
2024-09-08 18:27:37.295 +0200 100 00:29:58.132772 00:28:19.626944 null
2024-09-08 17:57:39.162 +0200 63 00:28:08.376277 00:30:01.532578 null
2024-09-08 17:29:30.786 +0200 17 00:30:23.2228 00:30:01.453531 null
2024-09-08 16:59:07.563 +0200 12 00:29:41.548219 00:30:01.637751 null
2024-09-08 16:29:26.015 +0200 11 00:30:10.24083 00:30:01.514036 null
2024-09-08 15:59:15.774 +0200 1 00:30:13.911094 00:57:11.874087 null
2024-09-08 15:29:01.863 +0200 100 00:57:13.865532 11:51:37.292819 null and here it was off for almost 12 hours (2W or 1Wh/half-hour consumption anyway). This line has a large last_changed_ts_diff
2024-09-08 14:31:47.998 +0200 1 11:51:07.968869 00:41:23.949532 null and this one a large last_reported_ts_diff
2024-09-08 02:40:40.029 +0200 22 00:41:37.022479 00:30:01.478981 null
2024-09-08 01:59:03.006 +0200 100 00:29:57.519526 00:29:16.488079 null
2024-09-08 01:29:05.487 +0200 69 00:29:12.580562 00:30:01.686263 null
2024-09-08 00:59:52.906 +0200 34 00:30:07.357224 00:30:51.55735 null
2024-09-08 00:29:45.549 +0200 1 00:30:45.208748 00:00:06.005327 null
2024-09-07 23:59:00.340 +0200 27 00:00:28.189583 00:16:53.88813 null

And now the Power sensor. There are 399 lines for the same 24-hour period. I’m going to put some sample lines only.

Nominal situation, it reports power every 1 minute and 3 seconds:

Timestamp Energy last_reported_ts_diff last_updated_ts_diff last_changed_ts_diff
2024-09-08 23:57:58.726 +0200 208.0 00:01:03.384975 00:01:03.386932 null
2024-09-08 23:56:55.341 +0200 207.6 00:01:03.386127 00:01:03.38292 null
2024-09-08 23:55:51.955 +0200 204.8 00:01:04.000283 00:01:03.895344 null
2024-09-08 23:54:47.954 +0200 207.9 00:01:03.282763 00:01:04.004766 null
2024-09-08 23:53:44.672 +0200 209.1 00:01:03.998324 00:01:08.399444 null
2024-09-08 23:52:40.673 +0200 206.9 00:01:08.504565 00:01:03.487604 null
2024-09-08 23:51:32.169 +0200 208.4 00:01:03.685843 00:01:03.594678 null
2024-09-08 23:50:28.483 +0200 206.8 00:01:03.292785 00:01:03.282886 null
2024-09-08 23:49:25.190 +0200 206.7 00:01:03.282035 00:01:03.381635 null
2024-09-08 23:48:21.908 +0200 206.1 00:01:04.000916 00:01:03.904194 null
2024-09-08 23:47:17.907 +0200 208.0 00:01:03.27463 00:01:03.678603 null
2024-09-08 23:46:14.632 +0200 205.5 00:01:03.701491 00:01:03.293378 null

And here is the 12-hour off period:

Timestamp Energy last_reported_ts_diff last_updated_ts_diff last_changed_ts_diff
2024-09-08 14:07:58.800 +0200 198.2 00:01:03.288253 00:01:03.383096 null
2024-09-08 14:06:55.511 +0200 197.1 00:01:03.382172 00:01:03.38382 null
2024-09-08 14:05:52.129 +0200 192.8 00:01:03.286845 00:01:02.984811 null
2024-09-08 14:04:48.842 +0200 186.0 00:01:00.559992 12:28:13.872513 null
2024-09-08 14:03:48.282 +0200 0.0 12:28:12.758239 00:00:27.909855 null
2024-09-08 01:35:35.524 +0200 201.3 00:00:31.544755 00:01:03.282112 null
2024-09-08 01:35:03.979 +0200 200.9 00:01:03.179935 00:01:03.36821 null
2024-09-08 01:34:00.800 +0200 200.2 00:01:03.488474 00:01:03.09289 null
2024-09-08 01:32:57.311 +0200 200.0 00:01:03.072788 00:01:03.281923 null

But if necessary, the plug will report more frequently. Here is a different device (the server on which Home Assistant is installed). Normally, it uses about 36W of power, but during intensive tasks it can increase to about 100W. The Tuya plug will report the Power sensor as frequently as 3 seconds.

Timestamp Energy last_reported_ts_diff last_updated_ts_diff last_changed_ts_diff
2024-09-08 22:29:44.553 +0200 38.8 00:01:39.58801 00:00:05.012924 null
2024-09-08 22:28:04.965 +0200 65.0 00:00:03.332723 00:00:35.007881 null
2024-09-08 22:28:01.632 +0200 38.6 00:00:36.686139 00:00:05.010981 null
2024-09-08 22:27:24.946 +0200 111.1 00:00:03.553253 00:00:35.005162 null
2024-09-08 22:27:21.392 +0200 37.4 00:00:36.462266 00:00:05.010357 null
2024-09-08 22:26:44.930 +0200 108.8 00:00:16.883631 00:27:04.963157 null
2024-09-08 22:26:28.047 +0200 36.3 00:26:48.192249 01:00:00.333855 null
2024-09-08 21:59:39.854 +0200 36.2 01:00:13.490923 01:00:00.174339 null