tuya / tuya-home-assistant

Home Assistant integration for controlling Powered by Tuya (PBT) devices using Tuya Open API, maintained by the Home Assistant Community and Tuya Developer Team.
MIT License
874 stars 206 forks source link

Power monitor smart plugs only updating usage rapidly when Smart Life app is opened #211

Open jellekpsn opened 3 years ago

jellekpsn commented 3 years ago

Describe the bug Elvico smart power monitor plug integrates well into Tuya V2 well, but current and power metrics are only updated infrequently (once a minute, on average) However, when I open the Smart Life app and view the 'Electric' tab, the values are updated every 2 seconds or so.

Expected behavior To have the power and current values from the smart plug update every few seconds, even when the Smart Life app is not is use.

Screenshots Not applicable

Home Assistant Version

Device info (please complete the following information, which can be found in log): like this: { "active_time": 1610826786, "biz_type": 18, "category": "cz", "create_time": 1610826786, "icon": "smart/device_icon/eu1510865353607K7oJW/bf2a17e8f775e3fc3fqqqk161435492361950.png", "id": "bf2a17e8f775e3fc3fqqqk", "ip": "REDACTED", "lat": "REDACTED", "local_key": "REDACTED", "lon": "REDACTED", "name": "Kitchen Spots", "online": true, "owner_id": "489452", "product_id": "j0zozzoarutv0nu1", "product_name": "Wifi Plug", "status": [ { "code": "switch_1", "value": true }, { "code": "countdown_1", "value": 0 }, { "code": "add_ele", "value": 6 }, { "code": "cur_current", "value": 105 }, { "code": "cur_power", "value": 149 }, { "code": "cur_voltage", "value": 2318 }, { "code": "relay_status", "value": "last" } ], "sub": false, "time_zone": "+01:00", "uid": "REDACTED", "update_time": 1626178437, "uuid": "REDACTED" }

Device specifications (please complete the following information, which can be found in log): 2021-07-24 23:54:42 DEBUG (SyncWorker_4) [tuya iot] Request: method = GET, url = https://openapi.tuyaeu.com/v1.0/devices/bf2a17e8f775e3fc3fqqqk/specifications, params = None, body = None, headers = {'client_id': 'REDACTED', 'sign': 'B50E83C8A2F46CD097F3ABABBF37D9A37634FFBD238261E2D63B2E69D3447F45', 'sign_method': 'HMAC-SHA256', 'access_token': 'REDACTED', 't': '1627163682915', 'lang': 'en'} 2021-07-24 23:54:42 DEBUG (SyncWorker_4) [tuya iot] Response: { "result": { "category": "cz", "functions": [ { "code": "switch_1", "type": "Boolean", "values": "{}" }, { "code": "countdown_1", "type": "Integer", "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}" }, { "code": "relay_status", "type": "Enum", "values": "{\"range\":[\"power_off\",\"power_on\",\"last\"]}" } ], "status": [ { "code": "switch_1", "type": "Boolean", "values": "{}" }, { "code": "countdown_1", "type": "Integer", "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}" }, { "code": "add_ele", "type": "Integer", "values": "{\"unit\":\"kW·h\",\"min\":0,\"max\":50000,\"scale\":3,\"step\":100}" }, { "code": "cur_current", "type": "Integer", "values": "{\"unit\":\"mA\",\"min\":0,\"max\":30000,\"scale\":0,\"step\":1}" }, { "code": "cur_power", "type": "Integer", "values": "{\"unit\":\"W\",\"min\":0,\"max\":80000,\"scale\":1,\"step\":1}" }, { "code": "cur_voltage", "type": "Integer", "values": "{\"unit\":\"V\",\"min\":0,\"max\":5000,\"scale\":1,\"step\":1}" }, { "code": "relay_status", "type": "Enum", "values": "{\"range\":[\"power_off\",\"power_on\",\"last\"]}" } ] }, "success": true, "t": 1627163682952 } Additional context Not actually sure this is a bug in the Tuya V2 integration of that the smart plug needs some sort of 'nudge' to relay energy values every few seconds.

Thanks!

Bjarkes commented 3 years ago

I have the same issue with the Denver SHP-100 Smart Plug. It is the same behavior when using the API directly (from iot.tuya.com or any library), call it directly or look at the Device Log on iot.tuya.com. If the app is not open in the "Electric" tab, then the updates comes very infrequent, sometimes more than an hour in-between.

joe-sydney commented 3 years ago

Same here with the Kogan smart plug with energy monitoring and 2 USB ports

AdmiralStipe commented 3 years ago

Same issue here with Robaxo RSP-025 plug - obviously NOT flashed with Tasmota, but this is the only link I found on them. Log shows nothing unusual, just the consumption isn't updating in HA unless I have TuyaSmart app opened all the time on Electricity tab - then it works perfectly, but of course only for the plug, which I'm observing in TuyaSmart.

calvinwankhede commented 3 years ago

Same problem with the Wipro Smart plug. Description of the issue matches AdmiralStipe's down to the T.

AdmiralStipe commented 3 years ago

If this is of any help to developers, this link from another git issue with a concept of requesting updates (with so called "command 18") might give a way towards the solution (the issue was not resolved at original git, as it has gone somehow dormant).

brunokelo commented 3 years ago

same problem here,

burnworld3d commented 3 years ago

@tsutsuku is there anything we can provide to help you to track this issue? Or is more a thing that you need to forward to the IOT api team? thanks

Bjarkes commented 3 years ago

Here we traced the problem down to the device being more picky with the payload of the updateDPS command: https://github.com/jasonacox/tuyapower/issues/7 I have solved my problem temporarily by having a Python script using tinytuya, that call the UpdateDPS with the correct payload periodically, which will then result in data coming in via tuya-home-assistant correctly.

burnworld3d commented 3 years ago

Here we traced the problem down to the device being more picky with the payload of the updateDPS command: jasonacox/tuyapower#7 I have solved my problem temporarily by having a Python script using tinytuya, that call the UpdateDPS with the correct payload periodically, which will then result in data coming in via tuya-home-assistant correctly.

I see, however this is a very 'custom' solution tho, specially if you plan to use with LocalTuya and similar (as you need to have the key for each of your devices) Since we get official support, Tuya team needs to fix this thingy on their end, since they have all our devices data

But thanks anyway to let me know about your solution, very interesting!

Bjarkes commented 3 years ago

Yes I very much agree that this should be fixed by the Tuya Team. Either they fix the firmware for these devices, or make sure to call the updateDPS in the correct way for these devices.

And yes, my temporary fix will not work with LocalTuya (I have tried), as tinytuya use the same type of connection as LocalTuya, and the device does only allow one such client to be connected at any time, so they battle over the connection and take turns getting a disconnected error.

joe-sydney commented 3 years ago

Yes I very much agree that this should be fixed by the Tuya Team. Either they fix the firmware for these devices, or make sure to call the updateDPS in the correct way for these devices.

And yes, my temporary fix will not work with LocalTuya (I have tried), as tinytuya use the same type of connection as LocalTuya, and the device does only allow one such client to be connected at any time, so they battle over the connection and take turns getting a disconnected error.

Excellent tip. I was playing around with updateDPS, etc, with localtuya, but it didn't work due to the reason you mentioned. Here with Tuya v2, it has worked well. I use node-red to trigger the tinytuya updateDPS with different intervals depending on what the plug was used to connect (only use it to detect if something is operating so having a real time stream was not really required).

Leatherface75 commented 3 years ago

Got it working with tinytuya with a script that updates every second. Not sure how to do this with localtuya/tuya2 plugin but can't see how this would be hard to implement.

import tinytuya
import time

d1 = tinytuya.OutletDevice('<device id>', '<ip>', '<device key>', 'device22') # DELTACO Smart Plug SH-P02 1
d2 = tinytuya.OutletDevice('<device id>', '<ip>', '<device key>', 'device22') # DELTACO Smart Plug SH-P02 2
d3 = tinytuya.OutletDevice('<device id>', '<ip>', '<device key>', 'device22') # DELTACO Smart Plug SH-P02 3

d1.set_version(3.3)
d2.set_version(3.3)
d3.set_version(3.3)
d1.set_socketPersistent(True)
d2.set_socketPersistent(True)
d3.set_socketPersistent(True)

while(True):
    payload1 = d1.generate_payload(tinytuya.UPDATEDPS)
    payload2 = d2.generate_payload(tinytuya.UPDATEDPS)
    payload3 = d3.generate_payload(tinytuya.UPDATEDPS)
    d1.send(payload1)
    d2.send(payload2)
    d3.send(payload3)
    time.sleep(1)
AdmiralStipe commented 3 years ago

To the developers, please, implement the solution Martin commited here. It polls updates with a heartbeat (10s) and it works perfectly.

joe-sydney commented 3 years ago

Hey all. I just got a charge from Tuya for exceeding quotas. All I have are 2 of the Kogan plugs, and triggering them to update energy data via the tinytuya method discussed above. (See bill info below).

Is this expected and caused by the energy data updates? Anyone else experience this, and how can we minimise the API calls/messages to stay within quota?

API CALL FOREIGN TRIAL数量 API CALL FOREIGN TRIAL费用 1549 3.3

Leatherface75 commented 3 years ago

That sounds strange have you done this first?

https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md

Also updating every 5 or 10 seconds maybe is enough for most people. Above exemple updates every second.

joe-sydney commented 3 years ago

That sounds strange have you done this first?

https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md

Also updating every 5 or 10 seconds maybe is enough for most people. Above exemple updates every second.

yeah, not sure what happened there. I received an email notification to check the details on the IoT Platform portform. Indeed, there was a $2+ charge. But today its been changed to zero.

I was triggering the tinytuya updateDPS commands via Node Red, every 15 seconds and 60 seconds for the two plugs, as I didn't really need constant streams of updates.

brunokelo commented 3 years ago

That sounds strange have you done this first? https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md Also updating every 5 or 10 seconds maybe is enough for most people. Above exemple updates every second.

yeah, not sure what happened there. I received an email notification to check the details on the IoT Platform portform. Indeed, there was a $2+ charge. But today its been changed to zero.

I was triggering the tinytuya updateDPS commands via Node Red, every 15 seconds and 60 seconds for the two plugs, as I didn't really need constant streams of updates.

can you explain how you did it on node red?

thanks

joe-sydney commented 3 years ago

It was using basically the same script (for each device) without infinite loop. Then I used Node-red to call that script at the required intervals.

I have since reverted back to localtuya as there is a fix there now which sends the updateDPS at regular intervals.

That sounds strange have you done this first? https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md Also updating every 5 or 10 seconds maybe is enough for most people. Above exemple updates every second.

yeah, not sure what happened there. I received an email notification to check the details on the IoT Platform portform. Indeed, there was a $2+ charge. But today its been changed to zero. I was triggering the tinytuya updateDPS commands via Node Red, every 15 seconds and 60 seconds for the two plugs, as I didn't really need constant streams of updates.

can you explain how you did it on node red?

thanks

brunokelo commented 3 years ago

It was using basically the same script (for each device) without infinite loop. Then I used Node-red to call that script at the required intervals.

I have since reverted back to localtuya as there is a fix there now which sends the updateDPS at regular intervals.

I understood the correction would be the file (init.py?) I've already applied it here, but I'm still having a very long delay in updating the consumption,

joe-sydney commented 3 years ago

There are two files to be replaced/updated: common.py and init.py. See the following:

https://github.com/rospogrigio/localtuya/pull/549/files

brunokelo commented 3 years ago

There are two files to be replaced/updated: common.py and init.py. See the following:

https://github.com/rospogrigio/localtuya/pull/549/files

I updated the 2 files, even so I'm having a delay of more than 15m to update the consumption :(

brunokelo commented 3 years ago

image exemple.

strange, when the smart life app opens the update is in real time.

dualmacops commented 1 year ago

The problem still persists, i haven't updated the frimware on most of my plugs for this reason.

LukasMendez commented 1 year ago

I also have the same issue still... Very annoying, as I just bought 8 Tuya Smart Plugs, but the refresh rate is so unreliable

jonathh21 commented 1 year ago

I've been investigating this issue after trying to work out what 'add electrity' is.

I wanted to have a device turn on ( a hover in my workshop) when I use my tablesaw but found the updates were hopelessly slow.

I am also investigating an issue where by the trigger seems to be an order of magnitude out. By maybe 100?

40w device triggers at 200w threshold. Homing in on the correct overreading by waiting to see a light turn on and off a new times.

I am new to this all and don't really know how to debug or hit the APIs.

jorgnyg commented 1 year ago

With Local Tuya you can set the scan interval manually. No need to open smart life app to get readings. image

gquach commented 1 year ago

Just making a note here that I'm experiencing the same issue with the Arlec Grid Connect smart plug. Reloading the Tuya integration manually fixes it temporarily, but then the energy consumption values remain frozen again after that...

calibra69 commented 1 year ago

Hi everybody, still same problem with my Konyks plugs. any news?

Shestoperd commented 10 months ago

Same problem with the Smart Plug +. Scan Interval is set to 20 seconds, but this does not affect the update frequency in any way.

TanYauRei commented 10 months ago

Hi there, got the same problem and also tried the workaround with the localtuya integration, to no avail. What did end up working for me was the tuya local custom integration! Might worth a try!

vigneshwaran0492 commented 3 months ago

@TanYauRei Thanks for the suggestion! I had the same issue where the power updates were very slow with my tuya plug and the localtuya didn't solve the issue for me. Switched to the tuya-local custom integration and the issue got resolved and now my power values get updated every 30 seconds.