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
856 stars 206 forks source link

SMT006 Over-under voltage protector Energy meter - no power/voltage/current sensors, wrong energy sensor #495

Open slydiman opened 2 years ago

slydiman commented 2 years ago

Describe the bug The power, voltage, current, leak current sensors are missing at all!

The energy sensor is missing the attribute state_class: total_increasing. The attribute unit_of_measurement is wrong - kW·h instead of kWh. So it is impossible to add this sensor to the Energy panel.

Expected behavior Please add the power, voltage, current and leak current sensors.

The energy sensor must contain the attributes state_class: total_increasing and unit_of_measurement: kWh.

Screenshots

Home Assistant Version 2021.9.7

Device info :

{
      "active_time": 1632566806,
      "biz_type": 0,
      "category": "dlq",
      "create_time": 1632566806,
      "icon": "smart/device_icon/eu1594223482321eRimQ/bf179835e7cf55a9dbn32c163256777029615.png",
      "id": "***",
      "ip": "***",
      "lat": "***",
      "local_key": "***",
      "lon": "***",
      "model": "",
      "name": "Power Input",
      "online": true,
      "owner_id": "13863691",
      "product_id": "r9kg2g1uhhyicycb",
      "product_name": "",
      "status": [
        {
          "code": "total_forward_energy",
          "value": 220
        },
        {
          "code": "phase_a",
          "value": "CXQACcQAAWwAAA=="
        },
        {
          "code": "phase_b",
          "value": "AAAAAAAAAAAAAA=="
        },
        {
          "code": "phase_c",
          "value": "AAAAAAAAAAAAAA=="
        },
        {
          "code": "fault",
          "value": 0
        },
        {
          "code": "switch_prepayment",
          "value": false
        },
        {
          "code": "energy_reset",
          "value": ""
        },
        {
          "code": "balance_energy",
          "value": 0
        },
        {
          "code": "charge_energy",
          "value": 0
        },
        {
          "code": "switch",
          "value": true
        },
        {
          "code": "breaker_number",
          "value": "FSE-F723C52D0A****"
        }
      ],
      "sub": false,
      "time_zone": "+04:00",
      "uid": "***",
      "update_time": 1632583064,
      "uuid": "***"
 }

Device specifications:

2021-09-25 20:01:16 DEBUG (SyncWorker_4) [tuya iot] Request: method = GET, url = https://openapi.tuyaeu.com/v1.0/devices/bf179835e7cf55a9dbn32c/specifications, params = None, body = None, t = 1632585676088 
2021-09-25 20:01:16 DEBUG (SyncWorker_4) [tuya iot] Response: {
  "result": {
    "category": "dlq",
    "functions": [
      {
        "code": "switch_prepayment",
        "type": "Boolean",
        "values": "{}"
      },
      {
        "code": "energy_reset",
        "type": "Enum",
        "values": "{\"range\":[\"empty\"]}"
      },
      {
        "code": "charge_energy",
        "type": "Integer",
        "values": "{\"unit\":\"kW·h\",\"min\":0,\"max\":999999,\"scale\":2,\"step\":1}"
      },
      {
        "code": "switch",
        "type": "Boolean",
        "values": "{}"
      }
    ],
    "status": [
      {
        "code": "total_forward_energy",
        "type": "Integer",
        "values": "{\"unit\":\"kW·h\",\"min\":0,\"max\":99999999,\"scale\":2,\"step\":1}"
      },
      {
        "code": "phase_a",
        "type": "Raw",
        "values": "{}"
      },
      {
        "code": "phase_b",
        "type": "Raw",
        "values": "{}"
      },
      {
        "code": "phase_c",
        "type": "Raw",
        "values": "{}"
      },
      {
        "code": "fault",
        "type": "Bitmap",
        "values": "{\"label\":[\"short_circuit_alarm\",\"surge_alarm\",\"overload_alarm\",\"leakagecurr_alarm\",\"temp_dif_fault\",\"fire_alarm\",\"high_power_alarm\",\"self_test_alarm\",\"ov_cr\",\"unbalance_alarm\",\"ov_vol\",\"undervoltage_alarm\",\"miss_phase_alarm\",\"outage_alarm\",\"magnetism_alarm\",\"credit_alarm\",\"no_balance_alarm\"]}"
      },
      {
        "code": "switch_prepayment",
        "type": "Boolean",
        "values": "{}"
      },
      {
        "code": "energy_reset",
        "type": "Enum",
        "values": "{\"range\":[\"empty\"]}"
      },
      {
        "code": "balance_energy",
        "type": "Integer",
        "values": "{\"unit\":\"kW·h\",\"min\":0,\"max\":99999999,\"scale\":2,\"step\":1}"
      },
      {
        "code": "charge_energy",
        "type": "Integer",
        "values": "{\"unit\":\"kW·h\",\"min\":0,\"max\":999999,\"scale\":2,\"step\":1}"
      },
      {
        "code": "switch",
        "type": "Boolean",
        "values": "{}"
      },
      {
        "code": "breaker_number",
        "type": "String",
        "values": "{\"maxlen\":255}"
      }
    ]
  },
  "success": true,
  "t": 1632585676308
}

Additional context

slydiman commented 2 years ago

I have the following answer from Tuya Service & Support:

Phase_a parsing rules:

  1. A-phase voltage, current and power
  2. Big-endian mode, HEX format, 10 bytes in total
  3. unit accuracy: voltage, 2 bytes, unit 0.1V. Current, 3 bytes, 0.001a. Phase A active power, 3 bytes, unit 0.001kW
  4. Packet format

For example, 08 80 00 03 E8 00 27 10 indicates the A phase 217.6V, the A phase current 1.000A, and the A phase power 10.000 KW

In my case the following state

{
  "code": "phase_a",
  "value": "CXQACcQAAWwAAA=="
},

can be decoded as 09 74 00 09 c4 00 01 6c 00 00, which means 0x974 = 242.0V 0x9C4 = 2.5A 0x16C = 364W

So all necessary information is present. Please fix the Tuya v2 integration to parse this information correctly and add necessary sensors. Thanks!

skynetua commented 2 years ago

total_forward_energy has already been fixed in master branch, other things need to be added

WildeRNS commented 2 years ago

I have the following answer from Tuya Service & Support:

Phase_a parsing rules:

  1. A-phase voltage, current and power
  2. Big-endian mode, HEX format, 10 bytes in total
  3. unit accuracy: voltage, 2 bytes, unit 0.1V. Current, 3 bytes, 0.001a. Phase A active power, 3 bytes, unit 0.001kW
  4. Packet format

For example, 08 80 00 03 E8 00 27 10 indicates the A phase 217.6V, the A phase current 1.000A, and the A phase power 10.000 KW

In my case the following state

{
  "code": "phase_a",
  "value": "CXQACcQAAWwAAA=="
},

can be decoded as 09 74 00 09 c4 00 01 6c 00 00, which means 0x974 = 242.0V 0x9C4 = 2.5A 0x16C = 364W

So all necessary information is present. Please fix the Tuya v2 integration to parse this information correctly and add necessary sensors. Thanks!

I tried manually change sensor.py (in version 1.6) as yours PR, and all work fine. But new PR is needed, because version 1.6 is different.

slydiman commented 2 years ago

But new PR is needed, because version 1.6 is different.

I don't see any reason for a new PR. I have rebased the existing PR and there is no any conflict with 1.6.

Please add a comment to PR https://github.com/tuya/tuya-home-assistant/pull/498 that all work fine.

Hope it will be merged some day and I won't need to rebase it for years after each release.

zagnuts commented 2 years ago

Similar issue here. The power/energy information was showing in HA as separate entities when using the tuya2 custom integration, but on moving to the 'official' tuya2 integration those entities disappeared. Checking the logs I can see the information is there, but again it's not showing as either separate entities or as attributes for the main entity itself. Here's the info for the device in the HA log showing it is actually exposing info such as current and power etc:

{ "active_time": 1602806606, "biz_type": 18, "category": "cz", "create_time": 1592870560, "icon": "smart/icon/ay1536534747810ekKBj/1567489983a750d08c70e.png", "id": "655256142*********", "ip": "***", "lat": "***", "local_key": "***", "lon": "***", "model": "PC399HA", "name": "Gaming PC", "online": true, "owner_id": "5974179", "product_id": "4ooy6eeqo6hnlax1", "product_name": "Smart Plug with Energy Meter PC399HA", "status": [ { "code": "switch_1", "value": true }, { "code": "countdown_1", "value": 0 }, { "code": "add_ele", "value": 78 }, { "code": "cur_current", "value": 1350 }, { "code": "cur_power", "value": 1716 }, { "code": "cur_voltage", "value": 250 } ], "sub": false, "time_zone": "+10:00", "uid": "***", "update_time": 1634573195, "uuid": "65525614**************" },

[sorry about the formatting - github seems to be mucking up things when inserting code for me currently!]

slydiman commented 2 years ago

The power/energy information was showing in HA as separate entities when using the tuya2 custom integration, but on moving to the 'official' tuya2 integration those entities disappeared.

SMT006 is a new device. It is the breaker (over-under voltage protector) with energy meter. The category is "dlq". This device never worked with the previous integration or with this integration. So I have added the support myself.

Category of your device is "cz". It means socket. I have no such device and I don't know how it worked with the previous integration. I cannot help with it.

Please open a separate issue for your device.

zagnuts commented 2 years ago

OK - thanks for responding, and understood.

dankomick commented 2 years ago

I have the following answer from Tuya Service & Support:

Phase_a parsing rules:

  1. A-phase voltage, current and power
  2. Big-endian mode, HEX format, 10 bytes in total
  3. unit accuracy: voltage, 2 bytes, unit 0.1V. Current, 3 bytes, 0.001a. Phase A active power, 3 bytes, unit 0.001kW
  4. Packet format

For example, 08 80 00 03 E8 00 27 10 indicates the A phase 217.6V, the A phase current 1.000A, and the A phase power 10.000 KW

In my case the following state

{
  "code": "phase_a",
  "value": "CXQACcQAAWwAAA=="
},

can be decoded as 09 74 00 09 c4 00 01 6c 00 00, which means 0x974 = 242.0V 0x9C4 = 2.5A 0x16C = 364W

So all necessary information is present. Please fix the Tuya v2 integration to parse this information correctly and add necessary sensors. Thanks!

Hi, similar issues with a wifi smart meter switch, receiving: "code":"phase_a" "value":"CO0AAAAAAAA="

Please, how can I decode from "CO0AAAAAAAA=" to three separate HEX numbers (voltage, current and power), as you did with your value "CXQACcQAAWwAAA==" ?

Thanks in advance!

slydiman commented 2 years ago

base64 decode "CO0AAAAAAAA=" > 08ed 000000 000000 228.5V 0A 0W

dankomick commented 2 years ago

Yes!, no current through the switch now. I imagine you used a web decoder Base64 to Hex, so I just need to program the decoding rules into my node-red function to get the actual power, voltage and current.

Thanks a lot.

Chemita23 commented 4 months ago

If anyone need this in PHP:

<?php
    require( 'config.php' );

    $app_id = 'xxxxxxxxxxxx';

    $device_id = 'xxxxxxxxxxxxxx';

    $tuya = new \tuyapiphp\TuyaApi( $config );

    $token = $tuya->token->get_new( )->result->access_token;

    $tuya = $tuya->devices( $token )->get_status( $device_id );

$array = json_encode($tuya, JSON_PRETTY_PRINT);

function left($cadena, $count)
 {
  return substr($cadena, 0, $count);
}

function right($str, $length) 
{
  return substr($str, -$length);
}

$hex = bin2hex(base64_decode($tuya->result[1]->value));

echo "<br /><br />Online: ";
echo $tuya->result[10]->value;

echo "<br /><br />Energy kWh: ";
echo $tuya->result[0]->value;

echo "<br /><br />Leak Current mA: ";
echo $tuya->result[2]->value;
echo "<br /><br />Activado: ";
echo $tuya->result[3]->value;

echo "<br /><br />Amps A: ";
$amp = right(left ($hex, 10), 6);
echo hexdec($amp)/1000;
echo "<br /><br />Volts V: ";
$volt = left ($hex, 4);
echo hexdec($volt)/10;

echo "<br /><br />Power W: ";
$wat = right($hex, 6);
echo hexdec($wat)/1000;
echo "<br /><br />Temp C: ";
echo ($tuya->result[20]->value / 10);
?>