jasonacox / tuyapower

Python module to read status and energy monitoring data from Tuya based WiFi smart devices. This includes state (on/off), current (mA), voltage (V), and power (wattage).
MIT License
140 stars 22 forks source link

Missing Power Data - again #33

Open alexandergeiler opened 1 year ago

alexandergeiler commented 1 year ago

Hi jasonacox,

I am getting again now power data (you helped me in the past already with the case https://github.com/jasonacox/tuyapower/issues/17

I have try to do the same steps again. But it is not working.


pi@pi-vz-be:~/test/tuyapower $ python plugpower.py 'bf3941cb7c91ced1a3mgyx' '192.168.185.75' 'xxx' '3.3'
TuyaPower (Tuya Power Stats) [0.2.0] tinytuya [1.12.11]

Device bf3941cb7c91ced1a3mgyx at 192.168.185.75 key xxx protocol 3.3:
    Response Data: {'dps': {'1': 6313, '10': 0, '16': True, '18': '111111111111'}}
    Switch On: 6313
    NOTE: Power data unavailable

{ "datetime": "2023-09-20T09:35:33Z", "switch": "6313", "power": "0", "current": "0", "voltage": "0" }

I have executed the 'python -m tinytuya wizard' command:

pi@pi-vz-be:~/test/tuyapower $ python -m tinytuya wizard
TinyTuya Setup Wizard [1.12.11]

    Existing settings:
        API Key=myKey
        Secret=mysecret
        DeviceID=bf3941cb7c91ced1a3mgyx
        Region=eu

    Use existing credentials (Y/n): Y

Download DP Name mappings? (Y/n): Y

Device Listing

[
    {
        "name": "WiFi Smart Meter - Garage",
        "id": "bf3941cb7c91ced1a3mgyx",
        "key": "xxx",
        "mac": "84:e3:42:70:81:7b",
        "uuid": "9e1d37b00ee11479",
        "sn": "1000158120009A",
        "category": "zndb",
        "product_name": "WiFi Smart Meter",
        "product_id": "nzexeqam9qulajbf",
        "biz_type": 0,
        "model": "ZM-wi-Fi -\u7535\u8868",
        "sub": false,
        "icon": "https://images.tuyaeu.com/smart/icon/ay1536897190133VVeLx/b9439bd86d7954663fe187f367c5a54b.jpg",
        "mapping": {
            "1": {
                "code": "forward_energy_total",
                "type": "Integer",
                "values": {
                    "unit": "kW\u00b7h",
                    "min": 0,
                    "max": 99999999,
                    "scale": 2,
                    "step": 1
                }
            },
            "6": {
                "code": "phase_a",
                "type": "Json",
                "raw_values": "{}",
                "values": {}
            },
            "10": {
                "code": "fault",
                "type": "Bitmap",
                "values": {
                    "label": [
                        "ov_cr",
                        "unbalance_alarm",
                        "ov_vol",
                        "undervoltage_alarm",
                        "miss_phase_alarm",
                        "outage_alarm",
                        "magnetism_alarm",
                        "terminal_alarm",
                        "cover_alarm",
                        "credit_alarm",
                        "no_balance_alarm",
                        "battery_alarm",
                        "meter_hardware_alarm"
                    ]
                }
            },
            "16": {
                "code": "switch",
                "type": "Boolean",
                "values": {}
            },
            "17": {
                "code": "alarm_set_2",
                "type": "Json",
                "raw_values": "{}",
                "values": {}
            },
            "18": {
                "code": "meter_id",
                "type": "String",
                "values": "{\"maxlen\":255}"
            }
        },
        "ip": "192.168.185.75",
        "version": "3.3"
    }
]

>> Saving list to devices.json
    1 registered devices saved

>> Saving raw TuyaPlatform response to tuya-raw.json

Poll local devices? (Y/n): Y

Scanning local network for Tuya devices...
    1 local devices discovered

Polling local devices...
    [WiFi Smart Meter - Garage] 192.168.185.75     - [Off] - DPS: {'1': 6314, '10': 0, '16': True, '18': '111111111111'}

>> Saving device snapshot data to snapshot.json

>> Saving IP addresses to devices.json
    1 device IP addresses found

Done.

pi@pi-vz-be:~/test/tuyapower $

file: tuya-raw.json

{
    "result": [
        {
            "active_time": 1695198459,
            "biz_type": 0,
            "category": "zndb",
            "create_time": 1614341183,
            "icon": "smart/icon/ay1536897190133VVeLx/b9439bd86d7954663fe187f367c5a54b.jpg",
            "id": "bf3941cb7c91ced1a3mgyx",
            "ip": "91.89.234.92",
            "lat": "49.3219",
            "local_key": "xxx",
            "lon": "8.5268",
            "model": "ZM-wi-Fi -\u7535\u8868",
            "name": "WiFi Smart Meter - Garage",
            "online": true,
            "owner_id": "166358995",
            "product_id": "nzexeqam9qulajbf",
            "product_name": "WiFi Smart Meter",
            "status": [
                {
                    "code": "forward_energy_total",
                    "value": 6313
                },
                {
                    "code": "phase_a",
                    "value": "{\"electricCurrent\":0.658,\"power\":0.15,\"voltage\":233.1}"
                },
                {
                    "code": "fault",
                    "value": 0
                },
                {
                    "code": "switch",
                    "value": true
                },
                {
                    "code": "alarm_set_2",
                    "value": "[{\"alarmCode\":\"overcurrent\",\"doAction\":false,\"threshold\":\"60\"},{\"alarmCode\":\"ammeter_overvoltage\",\"doAction\":false,\"threshold\":\"264\"},{\"alarmCode\":\"under_voltage\",\"doAction\":false,\
                },
                {
                    "code": "meter_id",
                    "value": "111111111111"
                }
            ],
            "sub": false,
            "time_zone": "+02:00",
            "uid": "eu1694874319369MBcdz",
            "update_time": 1695201243,
            "uuid": "9e1d37b00ee11479"
        }
    ],
    "success": true,
    "t": 1695202646458,
    "tid": "4f1e208e579911ee9a70760d0d3c40d3",
    "file": {
        "name": "tuya-raw.json",
        "description": "Full raw list of Tuya devices.",
        "account": "pr3hgx35af14buw8ursr",
        "date": "2023-09-20T11:37:26.479506",
        "tinytuya": "1.12.11"
    }
}

Any idea why I do not get the power data via plugpower.py? I can see the power data in the tuya-raw.json file...

alexandergeiler commented 1 year ago

add debug info

pi@pi-vz-be:~/tuyapower $ python debug-test-ag.py
DEBUG:TinyTuya [1.12.11]

DEBUG:Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
DEBUG:Using PyCrypto (3, 19, '0')
DEBUG:status() entry (dev_type is default)
DEBUG:final payload_dict for 'bf3941cb7c91ced1a3mgyx' ('v3.3'/'default'): {1: {'command': {'gwId': '', 'devId': '', 'uid': '', 't': ''}}, 7: {'command': {'devId': '', 'uid': '', 't': ''}}, 8: {mmand': {'gwId': '', 'devId': ''}}, 9: {'command': {'gwId': '', 'devId': ''}}, 10: {'command': {'gwId': '', 'devId': '', 'uid': '', 't': ''}}, 13: {'command': {'devId': '', 'uid': '', 't': ''}}6: {'command': {'devId': '', 'uid': '', 't': ''}}, 18: {'command': {'dpId': [18, 19, 20]}}, 64: {'command': {'reqType': '', 'data': {}}}}
DEBUG:building command 10 payload=b'{"gwId":"bf3941cb7c91ced1a3mgyx","devId":"bf3941cb7c91ced1a3mgyx","uid":"bf3941cb7c91ced1a3mgyx","t":"1695236032"}'
DEBUG:sending payload
DEBUG:payload encrypted=b'000055aa000000010000000a00000088b715f8535d75bae7996cedb17439baf51443a358ebc7d33986982ce952f13817d2d0977d28b9087342319769958b96e9fac12072f42cb61ab8ee3a0be8d0060821ddb80941d2c64426790c52259ef1443a358ebc7d33986982ce952f138179523d666494eee41fcf7e66cb57ebfcc752172c31949f2cf665af79809d8159df3a09d650000aa55'
DEBUG:received data=b'000055aa000000010000000a0000004c000000007392de80c50f1d37fab58e692c42142b68a77b2f1fe5aa562396587a629c565c016a3c8535929dbd2e5889548f5580bac2722cb206c722556e44c11341a1df7c86d790000aa55'
DEBUG:received message=TuyaMessage(seqno=1, cmd=10, retcode=0, payload=b's\x92\xde\x80\xc5\x0f\x1d7\xfa\xb5\x8ei,B\x14+h\xa7{/\x1f\xe5\xaaV#\x96Xzb\x9cV\\\x01j<\x855\x92\x9d\xbd.X\x89T\x8fU\x80a\xc2r,\xb2\x06\xc7"UnD\xc1\x13A\xa1\xdf|', crc=2262026617, crc_good=True, prefix=21930, iv=None)
DEBUG:raw unpacked message = TuyaMessage(seqno=1, cmd=10, retcode=0, payload=b's\x92\xde\x80\xc5\x0f\x1d7\xfa\xb5\x8ei,B\x14+h\xa7{/\x1f\xe5\xaaV#\x96Xzb\x9cV\\\x01j<\x855\x92\x9d\xbd.X\x89T\x8x80\xba\xc2r,\xb2\x06\xc7"UnD\xc1\x13A\xa1\xdf|', crc=2262026617, crc_good=True, prefix=21930, iv=None)
DEBUG:decode payload=b's\x92\xde\x80\xc5\x0f\x1d7\xfa\xb5\x8ei,B\x14+h\xa7{/\x1f\xe5\xaaV#\x96Xzb\x9cV\\\x01j<\x855\x92\x9d\xbd.X\x89T\x8fU\x80\xba\xc2r,\xb2\x06\xc7"UnD\xc1\x13A\xa1\xdf|'
DEBUG:decrypting=b's\x92\xde\x80\xc5\x0f\x1d7\xfa\xb5\x8ei,B\x14+h\xa7{/\x1f\xe5\xaaV#\x96Xzb\x9cV\\\x01j<\x855\x92\x9d\xbd.X\x89T\x8fU\x80\xba\xc2r,\xb2\x06\xc7"UnD\xc1\x13A\xa1\xdf|'
DEBUG:decrypted 3.x payload='{"dps":{"1":6585,"10":0,"16":true,"18":"111111111111"}}'
DEBUG:payload type = <class 'str'>
DEBUG:decoded results='{"dps":{"1":6585,"10":0,"16":true,"18":"111111111111"}}'
DEBUG:status() received data={'dps': {'1': 6585, '10': 0, '16': True, '18': '111111111111'}}
{'dps': {'1': 6585, '10': 0, '16': True, '18': '111111111111'}}
alexandergeiler commented 12 months ago

It seems the solution is in https://github.com/jasonacox/tinytuya/issues/55

pi@pi-vz-be:~/tuyapower $ python test2.py
input text = CRsAApUAAJY=
decoded = b'\t\x1b\x00\x02\x95\x00\x00\x96'
CRsAApUAAJY= converts to 0000100100011011000000000000001010010101000000000000000010010110
----- 3 values -----
V = 0000100100011011 = 233.1 V
I = 000000000000001010010101 = 2331 mA
P = 000000000000000010010110 = 150 W
pi@pi-vz-be:~/tuyapower $

But how can I get the payload via the plugpower.py?

jasonacox commented 12 months ago

Hi @alexandergeiler - good job on troubleshooting. Can you share the code you use for test2.py? Essentially you need to add that to plugpower.py to make it work. If you can think of a clean way to add this in for others using plugpower.py with this smart meter, please feel free to submit a Pull Request so we can give you credit. Otherwise, the test2.py code and/or any information on what you think we should do to edit plugpower.py would be helpful.

alexandergeiler commented 11 months ago

it´s your code :-) I just change the "text" to my value. I will have a look where I can add this in plugpower.py.

import base64

# Take one of the DPS values 
text = 'CV4AAPoAAC0='
field = text.encode('ascii')
print("input text = %s" % text)

# Decode base64
z =base64.b64decode(field)
print("decoded = %r" % z)

# Convert to binary - 64 bits
zbin = "".join(["{:08b}".format(x) for x in z])
print("%s converts to %s" % (text, zbin))

# Assume 2 int values are in the 64 bits
a = zbin[:32]
b = zbin[32:]
print("----- 2 values -----")
print("a = %s (%d)" % (a,int(a, 2)))
print("b = %s (%d)" % (b,int(b, 2)))

# Assume 4 int vlaues are in the 64 bits
a = zbin[:16]
b = zbin[16:32]
c = zbin[32:48]
d = zbin[48:64]
print("----- 4 values -----")
print("a = %s (%d)" % (a,int(a, 2)))
print("b = %s (%d)" % (b,int(b, 2)))
print("c = %s (%d)" % (c,int(c, 2)))
print("d = %s (%d)" % (d,int(d, 2)))