jasonacox/tuyapower

Missing Power Data

alexandergeiler opened this issue · 11 comments

Hi,

I used your python scripts to get data from an Tuya Smart Meter ZMAi-90.
see:
https://www.aliexpress.com/item/1005001992720359.html?spm=a2g0o.productlist.0.0.2fa2390aiu3hRi&algo_pvid=5cf56e5a-8bd7-4397-a57c-5a7a4deb7086&algo_expid=5cf56e5a-8bd7-4397-a57c-5a7a4deb7086-0&btsid=2100bb5116157546307118257ea49c&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_

I can find it
python tuyapower
TuyaPower (Tuya compatible smart plug scanner) [0.0.25] tinytuya [1.2.3]

Scanning on UDP ports 6666 and 6667 for devices (15 retries)...

FOUND Device [Valid payload]: 192.168.185.32
ID = bf3941cb7c91ced1a3mgyx, product = nzexeqam9qulajbf, Version = 3.3
Device Key required to poll for stats

Scan Complete! Found 1 devices.

But I can not get the data from it:

python plugpower.py bf3941cb7c91ced1a3mgyx 192.168.185.32 keyXXX 3.3 verbose=true
TuyaPower (Tuya Power Stats) [0.1.0] tinytuya [1.2.3]

Device bf3941cb7c91ced1a3mgyx at 192.168.185.32 key a5d40a4bf1c442d99b88522a5363faeb protocol 3.3:
Response Data: {u'Payload': u'', u'Err': u'900', u'Error': u'Invalid JSON Response from Device'}
Switch On: False
NOTE: Missing Power Data

{ "datetime": "2021-03-14T20:35:40Z", "switch": "False", "power": "0", "current": "0", "voltage": "0" }


Firmware
Hautpmodul: V1.1.71
MCU-Modul: V1.0.0

Could I somehow get the RAW JSON data?

Regards,
Alexander

Hi @alexandergeiler - all 3.3 devices will require that you have a valid device KEY. If you don't have that yet, you can get the key using the setup wizard in tinytuya (see here).

If you already have a valid device KEY, I recommend you turn on debugging and it will show you the raw payloads. Try this from inside python:

import tinytuya

tinytuya.set_debug(True) # Turn on Debug - for non-ANSI color use tinytuya.set_debug(True, False)

a = tinytuya.OutletDevice('bf3941cb7c91ced1a3mgyx', '192.168.185.32', DEVICEKEY)
a.set_version(3.3)
data =  a.status()
print(data)

I notice the device has 22 characters. There are some devices that require different command. TinyTuya which TuyaPower uses, should auto-detect device22 but it may not be working correctly. You can try this:

import tinytuya

tinytuya.set_debug(True) # Turn on Debug - for non-ANSI color use tinytuya.set_debug(True, False)

a = tinytuya.OutletDevice('bf3941cb7c91ced1a3mgyx', '192.168.185.32', DEVICEKEY, 'device22')
a.set_version(3.3)
a.set_dpsUsed({"1": None})  # This needs to be a valid datapoint on the device - 1 usually safe
data =  a.status()
print(data)

Hi,
I got the data with both commands:

python tuyatest.py
DEBUG:status() entry (dev_type is default)
DEBUG:building payload='{"t":"1615836781","devId":"bf3941cb7c91ced1a3mgyx","gwId                                                                                                                           ":"bf3941cb7c91ced1a3mgyx","uid":"bf3941cb7c91ced1a3mgyx"}'
DEBUG:payload generated='\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00                                                                                                                           \x88\x93\xc4\x06\xb9n\xf7\x07/\xaf\xc3\x1f{zR\xa3\xf6\xf7DF\x92\xa6p&\x087>\xaf\                                                                                                                           xe4\xf6\xec`\nPa\x9fWR>f\xff\x16\xcc\x8fe*\xdb\xe4\xa2\xf1PR\xf3\xfe\xe0v\xe1\xf                                                                                                                           2\x90\xb7\x1c\x08F\xe6\xe3Pa\x9fWR>f\xff\x16\xcc\x8fe*\xdb\xe4\xa2\x15\xbb{4}\x0                                                                                                                           4\xc8\x82\xdf\x87\x0c\xb2S\x8ca@\xbc\x0eb\x8c\xc6\xf9"j\xe2\x92y\\\xe3\xca\x88\x                                                                                                                           ee\x10\x0b9o[\xf92\xc5\xc1\xa8\x9al\xb6:h\x8d\xe0\xd8\x01\x00\x00\x00\xaaU'
DEBUG:received data='\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00,\x0                                                                                                                           0\x00\x00\x01\'E\xa2\xa5\x9e\xb7\x92\x84\xf5\xf7\xba~\xd9g"\x91\xd0jd\xfb\xc8\xc                                                                                                                           c1\xe0\xff\'t\x0b\x1d\x80\xdcf`zFh\x00\x00\xaaU'
DEBUG:raw unpacked message = TuyaMessage(seqno=0, cmd=10, retcode=1, payload='\'                                                                                                                           E\xa2\xa5\x9e\xb7\x92\x84\xf5\xf7\xba~\xd9g"\x91\xd0jd\xfb\xc8\xcc1\xe0\xff\'t\x                                                                                                                           0b\x1d\x80\xdcf', crc=1618626152)
DEBUG:decode payload='\'E\xa2\xa5\x9e\xb7\x92\x84\xf5\xf7\xba~\xd9g"\x91\xd0jd\x                                                                                                                           fb\xc8\xcc1\xe0\xff\'t\x0b\x1d\x80\xdcf'
DEBUG:decrypting='\'E\xa2\xa5\x9e\xb7\x92\x84\xf5\xf7\xba~\xd9g"\x91\xd0jd\xfb\x                                                                                                                           c8\xcc1\xe0\xff\'t\x0b\x1d\x80\xdcf'
DEBUG:decrypted 3.3 payload=u''
DEBUG:decoded results=u''
DEBUG:ERROR Invalid JSON Response from Device - 900 - payload: ""
DEBUG:status received data={u'Payload': u'', u'Err': u'900', u'Error': u'Invalid                                                                                                                            JSON Response from Device'}
{u'Payload': u'', u'Err': u'900', u'Error': u'Invalid JSON Response from Device'                                                                                                                           }

and with the second command I got the following:

python tuyatest2.py
DEBUG:status() entry (dev_type is device22)
DEBUG:building payload='{"devId":"bf3941cb7c91ced1a3mgyx","dps":{"1":null},"uid":"bf3941cb7c91ced1a3mgyx","t":"1615836829"}'
DEBUG:payload generated='\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\r\x00\x00\x00\x873.3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd4W\x88o\xe2\xf6L}\xb82\x8fq\x19=\'I\xbc\x0eb\x8c\xc6\xf9"j\xe2\x92y\\\xe3\xca\x88\xeeu\xdb\xc1\x9f\xfe\xfd\xd9\rG\x13\x9a4\xc7\xbd\x84o$#\xa2\xca)\xaeM\xf3]\xbdI\xf0\x97\xc0\xb3\xc8\xbc\x0eb\x8c\xc6\xf9"j\xe2\x92y\\\xe3\xca\x88\xee\xa6+\xc7\x8c\xf4\xd7\xb5\xef\x89\x01\x9fha\x1a\xce\x9cc\xa2\x19~\xa6\x02S\x11\r\x9cK]\xfcWF\x1f\'D\x07)\x00\x00\xaaU'
DEBUG:received data="\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\r\x00\x00\x00,\x00\x00\x00\x01\x96.\x8b\xd8\x87\xdcG\xc9\x86\xac\xd2\xae!\x1e\xe6\xa9\xf6;(\\\x00Sv\xfd//\xa4\xda-\xca\x8c\x16\xc7'\x0c\x83\x00\x00\xaaU"
DEBUG:raw unpacked message = TuyaMessage(seqno=0, cmd=13, retcode=1, payload='\x96.\x8b\xd8\x87\xdcG\xc9\x86\xac\xd2\xae!\x1e\xe6\xa9\xf6;(\\\x00Sv\xfd//\xa4\xda-\xca\x8c\x16', crc=3341225091L)
DEBUG:decode payload='\x96.\x8b\xd8\x87\xdcG\xc9\x86\xac\xd2\xae!\x1e\xe6\xa9\xf6;(\\\x00Sv\xfd//\xa4\xda-\xca\x8c\x16'
DEBUG:removing 3.3='\xa9\xf6;(\\\x00Sv\xfd//\xa4\xda-\xca\x8c\x16'
DEBUG:decrypting='\xa9\xf6;(\\\x00Sv\xfd//\xa4\xda-\xca\x8c\x16'
DEBUG:incomplete payload='\xa9\xf6;(\\\x00Sv\xfd//\xa4\xda-\xca\x8c\x16'
DEBUG:status received data=None
None


It is behaving like the DEVICEKEY is not valid.

What do you see with:

python -m tinytuya wizard

Hi,
I see the following:

python -m tinytuya wizard
TinyTuya Setup Wizard [1.2.3]


    Enter API Key from tuya.com: pr3hgx35af14buw8ursr
    Enter API Secret from tuya.com: xxx
    Enter any Device ID currently registered in Tuya App (used to pull full list                                                                                                  ): bf3941cb7c91ced1a3mgyx
    Enter Your Region (Options: us, eu, cn or in): eu

>> Configuration Data Saved to tinytuya.json
{
    "apiKey": "pr3hgx35af14buw8ursr",
    "apiSecret": xxx
    "apiDeviceID": "bf3941cb7c91ced1a3mgyx",
    "apiRegion": "eu"
}
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/pi/.local/lib/python2.7/site-packages/tinytuya/__main__.py", line                                                                                                   49, in <module>
    wizard.wizard(color)
  File "/home/pi/.local/lib/python2.7/site-packages/tinytuya/wizard.py", line 20                                                                                                  6, in wizard
    uid = response_dict['result']['uid']
KeyError: 'result'

I have copied the deviceID and API details from the Tuya web page at https://iot.tuya.com/
see picture:
device
app

From what I can tell, you are successful in getting an OAuth token based on the secret. However it is getting an empty result when trying to pull the device list based on the sample bf3941cb7c91ced1a3mgyx device ID you provided.

This is the area getting a no 'result' response:

    # Get UID from sample Device ID 
    uri = 'devices/%s' % DEVICEID
    response_dict = tuyaPlatform(REGION, KEY, SECRET, uri, token)
    uid = response_dict['result']['uid']

Can you confirm that you see your device ID bf3941cb7c91ced1a3mgyx in the iot.tuya.com portal?

Under your Test project you should see Device Management section and a "Details >" link. If you click on that and select the region ("eu") it should show your list of devices.

image

If this doesn't help, I'll upload additional debugging code to wizard.py to help troubleshoot (it is on my list anyway).

Possibly better... just got this: jasonacox/tinytuya#38

I updated the instructions based on this...

  1. Set up a Tuya Account:
    • Create a Tuya Developer account on iot.tuya.com and log in.
    • Click on "Cloud" icon -> Create a project (remember the Authorization Key: API ID and Secret for below)
    • Click on "Cloud" icon -> select your project -> Project Overview -> Linked Device -> Link devices by App Account (tab)
    • Click 'Add App Account' and it will display a QR code. Scan the QR code with the Smart Life app on your Phone (see step 1 above) by going to the "Me" tab in the Smart Life app and clicking on the QR code button [..] in the upper right hand corner of the app. When you scan the QR code, it will link all of the devices registered in your "Smart Life" app into your Tuya IoT project.
    • IMPORTANT Under "API Management" -> "API Products" and ensure the API groups have status "Subscribed": Smart Home Devices Management, Authorization and Smart Home Family Management (see screenshot here) - Make sure you authorize your Project to use these 3 API groups:
      • Click each of the API boxes
      • Click "Projects" tab
      • Click "New Authorization" button
      • Select your Project from the dropdown and click OK (see screenshot here)

@jasonacox , thank you for your hint. I haven´t authorizied my API!
I changed this already an hour ago, but it is still not working.

$ python plugpower.py bf3941cb7c91ced1a3mgyx 192.168.185.32 a5d40a4bf1c442d99b88522a5363faeb 3.3 verbose=true
TuyaPower (Tuya Power Stats) [0.0.25] tinytuya [1.2.3]

Device bf3941cb7c91ced1a3mgyx at 192.168.185.32 key a5d40a4bf1c442d99b88522a5363faeb protocol 3.3:
    Response Data: {u'Payload': u'', u'Err': u'900', u'Error': u'Invalid JSON Response from Device'}
    Switch On: False
    NOTE: Missing Power Data

{ "datetime": "2021-03-18T07:59:53Z", "switch": "False", "power": "-99", "current": "-99", "voltage": "-99" }

I added some more picture from the iot.tuya.com webpage.
I saw, that I am not able to test the API via the API explorer. May be that could explain the issue.
If I test the API via the DataCenter at China I am getting a "permission deny" error.
If I test the same API via the DataCenter at EU I see nothing... (Response is emtpy)

1 - API-Explorer - China
2 - API-Explorer - EU
3 - projects
4 - DeviceList
4a- Link devices
5 - DeviceDetails
6 - API-Products
7 - Authorization
8 -Smart Home Devices Management
9 - Smart Home Family Management

Thanks!

I think I may see the problem. Your device key is too long. It should be 16 characters, at least I have never seen anything longer than that.

Are you able to get information from the device using the SmartLife app?

If you run the wizard, it should create a devices.json file that has the ID and KEY:

python -m tinytuya wizard

examples devices.json

[
    {
        "name": "Living Room",
        "id": "01234567891234567890",
        "key": "0123456789abcdef"
    }
]

hi,

you are right, I was stupid. I used the Access Secret/Client Secret instead of the Device KEYs.
Sorry for that and thank you for you help!!!

python plugpower.py 3811261184cca89036b3 192.168.178.34 xxyyzz 3.3 verbose=true
TuyaPower (Tuya Power Stats) [0.1.0] tinytuya [1.2.3]

Device 3811261184cca89036b3 at 192.168.178.34 key xxyyzz protocol 3.3:
    Response Data: {u'devId': u'3811261184cca89036b3', u'dps': {u'1': True, u'18': 13, u'20': 2290, u'17': 53010, u'19': 0}}
    Switch On: True
    Power (W): 0.000000
    Current (mA): 13.000000
    Voltage (V): 229.000000
    Projected usage (kWh):  Day: 0.000000 Week: 0.000000  Month: 0.000000

{ "datetime": "2021-03-19T09:42:14Z", "switch": "True", "power": "0.0", "current": "13.0", "voltage": "229.0" }

solved