rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.74k stars 533 forks source link

Cannot get voltage/current from wifi smart meter #569

Open Tinwelint78 opened 2 years ago

Tinwelint78 commented 2 years ago

The problem

I'm trying to get energy meter from a tuya wifi smart meter. I have succesfully added total energy in kWh, but I cannot select this sensor in the energy panel, so I was trying the "integral" method, as described in #536, but even if I see the current/voltage sensor values from Tuya IoT, I cannot see them from DP dump.

Environment

Steps to reproduce

Configuration configuration.yaml or config_flow

DP dump


INFO:localtuya:localtuya version 1.0.0
INFO:localtuya:Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
INFO:localtuya:Using pytuya version '8.1.0'
INFO:localtuya:Detecting list of available DPS of device bf304384e690f34a90yehl [192.168.1.151], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"bf304384e690f34a90yehl","devId":"bf304384e690f34a90yehl"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b'.k\x95\xde\xf6/\x92\x82;X\x8c\x98.\xe7\xa5D\xb7d\xdb\xad\xa5\x98\xf4\x8b\x14VR)\xb0w\xa7\xb0Z\xa4MA\xa7k\xb8\xe8\xcc\xa3\xcd\x05\xc6\xe3I\xce\x0b\xcbNmMB\xde\xb6\x00\xf2\xa4A\x96\xd5\x83\xe2'
DEBUG:localtuya.pytuya:decrypted result='{"dps":{"1":14186,"10":0,"16":true,"18":"111111111111"}}'
AVAILABLE DPS ARE [{'1': 14186, '10': 0, '16': True, '18': '111111111111'}]
INFO:localtuya:COMPLETE response from device bf304384e690f34a90yehl [192.168.1.151].

**** deviceInfo returned OK ****

TuyaDebug (Tuya DPs dump) [1.0.0]

Device bf304384e690f34a90yehl at 192.168.1.151 key 2c2cf93c84a64737 protocol 3.3 dev_type type_0a:
    DPS [1] VALUE [14186]
    DPS [10] VALUE [0]
    DPS [16] VALUE [True]
    DPS [18] VALUE [111111111111]

Provide Home Assistant taceback/logs

Additional information

Here is the dump from the "Get Device details" in Tuya IoT API explorer:

{
  "result": {
    "active_time": 1630757732,
    "biz_type": 18,
    "category": "zndb",
    "create_time": 1630757732,
    "icon": "smart/icon/ay1536897190133VVeLx/b9439bd86d7954663fe187f367c5a54b.jpg",
    "id": "bf304384e690f34a90yehl",
    "ip": "128.116.235.198",
    "lat": "xxxxxxxxxxxxxx",
    "local_key": "xxxxxxxxxxxxxxxx",
    "lon": "xxxxxxxxxxx",
    "model": "ZM-wi-Fi -电表",
    "name": "WiFi Smart Meter",
    "online": true,
    "owner_id": "xxxxxxxxxxx",
    "product_id": "nzexeqam9qulajbf",
    "product_name": "WiFi Smart Meter",
    "status": [
      {
        "code": "forward_energy_total",
        "value": 14168
      },
      {
        "code": "phase_a",
        "value": "{\"electricCurrent\":3.172,\"power\":0.45,\"voltage\":234.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,\"threshold\":\"150\"}]"
      },
      {
        "code": "meter_id",
        "value": "111111111111"
      }
    ],
    "sub": false,
    "time_zone": "+02:00",
    "uid": "xxxxxxxxxxxxxxxxx",
    "update_time": 1632207598,
    "uuid": "xxxxxxxxxxxxxxxx"
  },
  "success": true,
  "t": 1632208210743
}

Current and voltage values are there, how can I get them? Alternatively, How can I make Energy panel accept total energy sensor?

Tinwelint78 commented 2 years ago

Maybe this is being addressed by #473?

jeremysherriff commented 2 years ago

I have this working, for both LocalTuya and Xiaomi integration smart plugs. If it's still not working for you, let me know and I can share some config.

Tinwelint78 commented 2 years ago

I managed to use total energy by defining the sensor as "device_class:energy". I haven't been successfull in reading voltage/current meters though, so if you can share a config for doing this I'll be grateful!

jeremysherriff commented 2 years ago

I haven't been successfull in reading voltage/current meters though

It took me quite some time to work out what you mean by this, sorry. I didn't realise that the integration doesn't support your device fully!

I believe the values you are trying to get are not yet supported by this integration. The PR you linked (#473) or something similar is needed.

licheng5625 commented 2 years ago

another way is in the sensor.py add following

from homeassistant.components.sensor import DEVICE_CLASSES, DOMAIN, STATE_CLASS_MEASUREMENT, SensorEntity

class LocaltuyaSensor(LocalTuyaEntity, SensorEntity):

**@property
def state_class(self):
    return STATE_CLASS_MEASUREMENT**