Tuya energy monitoring only updating when app is open?

Hi @user512.

I actually tried tuya-local first, but it was back in the end of 2023.

Back then I was discovering HA for the first time, and I just got 2 or 3 tuya devices for the first time too.

I couldn’t make it work the way I wanted, but back then I naively thought you could get the total power meter value from those tuya “power meters”.

And I could not understand why the “add_ele”, which is DP17 and was supposed to be the accumulated power usage in kWh (you know, the “meter” part of my “metered” device - the reason I bought the device in the first place) was NOT CHANGING.

Then by playing directly with pytuya and python, I FINALLY UNDERSTOOD why it didn’t work, and COULD NEVER WORK because of the way tuya and HA are.

(if someone wants more details I can explain, but essentially when you query d17 you get the total amount of kWh SINCE THE LAST TIME DP17 WAS UPDATED. - so 99% of the time it stays the same, for example 100, which means “+0.1kWh”, and HA interprets this as “the meter value is 0.1”)

So I finally accepted the fact that even thought thoses devices have a memory, and can store data in it, and can measure the power usage, and where called “meters”, they didn’t store the power used in kWh in any usable way.… grrrrrr …

So I resigned myself to use the integral integration to create “meters” based on the power values (D19).

But then I discovered this thread’s focus : I wasn’t getting up to date values for most of my devices (3.3 and 3.4 alike).

Then on one forum someone talked about localtuya and how there was an “interval” option to force the update of the power values - I thought I would try it.

I installed it, and instantly my 3.3 devices were respecting the interval ! I was finally able to monitor my power usage with my devices … except for a couple of them.

Then I realised those were the 3.4 ones - came to the conclusion that localtuya was not forcing the update of the 3.4 devices the same way as with the 3.3 … and here we are.

– (sorry for the long rambling that doesn’t really answer your question) –

To answer your question : I will test with tuya-local, out of the box, and my 3.4 devices that have the potential issue.

I will try to do it tonight or tomorrow and I will tell you

Ok so I had the time so I tested it.

Fresh new HA install, on a Pi 4. Installed SSH addon → Installed HACS → Installed tuya-local (the one you linked).

I only tested it with two of my tuya devices - one 3.4 eu ANTELA plug, and one TO-Q-SY1-JWT TONGU rail device (also 3.4) - that I know won’t update with localtuya without the update_dps fix

Well … It’s not great :

  • The Power value is updated about every 60 sec (almost exactly) if the Smart Life App is closed
  • The Power value is updated about every 30 sec if the Smart Life App is open

And sometimes, at random, it takes a lot more than the 30 / 60 sec

So … for me it would not be fast enough at all to be accurate.

And tuya-local has less options - specifically, no way to ‘force’ an interval (I tried the “poll only” option on the devices - no change)

Also - just for information - when adding the device, the process fails if I leave the protol to “Auto”. I had to manually select “3.4” for my 3.4 devices.

So for me, considering that I can get it to work with localtuya by editing the one line of code, and then I have a system where my devices can be polled at a controllable per-device interval, and with tuya-local I get one update per 60 sec - which would result in totally false energy usage totals … I will stick with localtuya + edition of one line

(+ if you want you can just download the folder on github and paste it in “custom_components” - no need for HACS or SSH addon or File Editor, no need to edit a line of code - you would just need a way to paste it in the config folder - so maybe just Samba Share ?)

But to sum up, while in my case it’s clear localtuya + make sure the update_dps function includes 3.4 is CLEARLY the way to go, I suspect it will be more on a case by case depending on the devices

My ANTELA and TONGU devices clearly work ok with pytuya (which is the python library to talk to tuya devices) - which is used by localtuya - and they actually responds when they are polled with the update_dps function that “forces” them to. And clearly they’re not very talkative if you don’t force them to talk (they are shy lol). Maybe some other devices won’t be and will send the data every few seconds even if not forced to.

I can’t be sure (I only took a very quick look in the code on github) but tuya-local doesn’t seem to use pytuya.

So chances are, if your devices work well with pytuya → localtuya will work well
And it they don’t, tuya-local might work more reliably.

You are not supposed to use both at the same time (they would “talk over each other” because the tuya devices can only speak to one other device at a time), but … I know some people do it, and it can work …

For example, I just tried : I installed localtuya on top of my test installation - + file editor and edited the update_dps line - then added the two devices in localtuya, with the “update_dps” set to 2 sec.

So, now, localtuya sends a “what is the power value” to the two devices, every 2 seconds.
And so, every 2 seconds, the devices send a “the power is xxx” to HA

And this message is picked up by both localtuya and tuya-local, because they are both listening for tuya protocol messages.

So right now, my tuya-local entities are updated every 2 seconds … because its localtuya that sends the requests.

(Important note : for this to work - for tuya-local entities to be updated by messages initiated by localtuya - you need to make sure the “poll only” option for the device in tuya-local is not checked - if it’s checked it doesn’t seem to “process” values that it didn’t ask for itself)

(in theory, if tuya-local and localtuya tried to send a message to a device at exactly the same time, at least one of the requests would be lost. But considering they seem to repeat their requests a lot, and only every few seconds, I think it could work long term)


That makes me think that you could, if you really wanted to use tuya-local, and not have to install localtuya, and have every devices configured in both etc … you could in theory use a python script, using pytuya, to just send a “what is the power value” (basically sending a “update_dps” command) every few seconds, all the time, to a list of all your devices, and tuya-local would receive the values that comes back because the devices are now forced to respond and be more talkative.

With your tuya-local install that works well with your devices, at what interval approximatively are the values updated ?

1 Like

You are completely right… and my installation behaves exactly the same way. It takes 60 seconds to update (of course without app open, or it will be useless).

…this I didn’t noticed. I suppose I haven’t been so meticulous to check, but probably happen to me too.

Yes. I thought maybe I did something wrong, but if that happens to you too… it should be some bug on tuya-local.

60 seconds. They keep working for months without problems. Probably a faster update (probably, instantly when value changes), it will be better but for necessities right now, it is enough.

But if for some reason I use a device that needs a faster update, I think I will go with your method. The only benefit is that tuya-local works out of the box, and is something less to have in mind when you update addon, etc. (that I suppose it will break the line).

In any case, I suppose the best would be to have localtuya being updated with the ‘real’ last version with the 3.4 devices contemplated. Hope somebody around can explain why version on github has the line updated, and hacs addon is not downloading that release.

Thanks a lot Matt for your complete test.

You’re welcome.

I created a separate topic about why HACS is not installing the latest version of the github code (https://community.home-assistant.io/t/why-is-hacs-installing-an-older-version-of-localtuya-that-the-one-on-github/761934)

We will see if someone can tell me if I am missing something, or if we should contact HACS coders / localtuya coders / github.

Hopefully, in the near future the problem will be solved, and localtuya will then work out of the box (and respects the update intervals).

Have a nice day

1 Like

Ok so for everyone still interrested - from the other thread I created I got my answers:

  • It’s normal to not get the updated update_dps function if you install localtuya from HACS, because the latest version released (5.2.1) was BEFORE the update - so the code we can see (and download) on github has not been RELEASED (as an OFFICIAL RELEASED VERSION) yet

  • There is a fork of localtuya that is updated way more frequently (every month or every two months) and has the fix included - it’s this one : https://github.com/xZetsubou/hass-localtuya (Thank you @reste_narquois ! )
    (You can add the repo to HACS if you have it installed by clicking this link : https://my.home-assistant.io/redirect/hacs_repository/?category=integration&repository=hass-localtuya&owner=xZetsubou )

  • (EDIT : and … a few hours ago the rospogrigio fork of localtuya - the one you get by default from HACS without having to touch anything - released a new version - 5.2.2 - and it has the 3.4 fix, so, as of now, the ‘original’ localtuya (the one HACS directs you to install) works out of the box too.!! Yeah !! )

And I tested it - this version (the xZetsubou one) works OUT OF THE BOX (not surprising considering the update_dps function has the 3.4 fix - so the power values are updated at the interval you ask for) - so I highly recommand it for a new user, or if someone needs to reinstall HA - I have so many tuya devices already configured that I won’t make the switch YET, but when / if I need to reinstall, I will definitely use this fork

(It’s up to date - so for example with 3.5 devices starting to appear on the market it would support them, and with a new version every month or two any new fix or feature should be included rapidly. I also like how this fork lets you choose if you want to manually configure the entities, or let it do it automatically)

(There are a few differences from the original that I noticed right away, but they are details, and those differences are not necessarily bad, just needs to know - like how it names entities - like if I add a tuya device “MY TUYA DEVICE” and then configure the entity “POWER”, it names the entity “sensor.my_tuya_device_power” - which is not bad - great actually, but you need to know that, because it doesn’t on the rospogrigio one (the ‘original’ one), so I created a device “MY TUYA DEVICE”, and configured the entity (dp19) as “MY TUYA DEVICE POWER”, and that ends up with an entity names “sensor.my_tuya_device_my_tuya_device_power” lol)

So to sum up : use this fork : https://github.com/xZetsubou/hass-localtuya - Works out of the box - (with the power values updated at the interval you configure) and updated frequently (last version from july) (Again, thank you @reste_narquois) / EDIT : or re-try the “original” rospogrigio one - the one HACS include by default in its repos, the new version 5.2.2 (from today) should update the power values correctly)

1 Like