Tuya LOCAL with energy monitoring and without tuya-convert

Full log

2020-07-25 11:34:05 DEBUG (SyncWorker_0) [custom_components.localtuya.pytuya] status() entry (dev_type is device22)
2020-07-25 11:34:05 DEBUG (SyncWorker_0) [custom_components.localtuya.pytuya] json_payload=b'{"devId":"12312312123","uid":"123123123123","t":"1595687645","dps":{"1":null,"101":null,"102":null}}'
2020-07-25 11:34:05 DEBUG (SyncWorker_0) [custom_components.localtuya.pytuya] status received data=b'\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00K\x00\x00\x00\x003.3\x00\x00\x00\x00\x00\x00\x00;\x00\x00\x00\x01\xd1H\xf3\xf65\xff\xfb7\x0f\x86\xa0\xab\xc8\xac\xaa\xef\xfa\r\tgb\xb5\xa7\x85\x1d}%\xfd\x12Z\xe6b\x1cd^\xaeJ\x8bMJ\xb62\x85\x81\x18]\x0e\xbaL\xca\xeaq\x00\x00\xaaU'
2020-07-25 11:34:05 DEBUG (SyncWorker_0) [custom_components.localtuya.pytuya] result=b'\xd1H\xf3\xf65\xff\xfb7\x0f\x86\xa0\xab\xc8\xac\xaa\xef\xfa\r\tgb\xb5\xa7\x85\x1d}%\xfd\x12Z\xe6b\x1cd^\xaeJ\x8bMJ\xb62\x85\x81\x18]\x0e\xba'
2020-07-25 11:34:05 DEBUG (SyncWorker_0) [custom_components.localtuya.pytuya] decrypted result='{"dps":{"1":true},"t":1595687644}'

Hi!
Where did you get this dps from??

Hi!
Android Tuya app log.

Same problem - " dps":{“1”:null,“101”:null,“102”:null}}’
Meters not showing any values

Android app log:
{“1”:true,“17”:18,“18”:0,“19”:0,“20”:2236,“21”:1,“22”:617,“23”:27108,“24”:15461,“25”:1348,“26”:0,“9”:0},"

heres my code:

  - platform: localtuya
    host: 10.255.0.24
    local_key: x
    device_id: x
    name: tuya_plug1
    friendly_name: plug_1
    protocol_version: 3.3
    current: 18
    current_consumption: 19
    voltage: 20
  - platform: template
    sensors:
      tuya_plug1_voltage:
        friendly_name: "G5 voltage"
        unit_of_measurement: 'V'
        value_template: "{{ states.switch.tuya_plug1.attributes.voltage }}"
      
      tuya_plug1_current:
        friendly_name: "G5 current"
        unit_of_measurement: 'mA'
        value_template: "{{ states.switch.tuya_plug1.attributes.current }}"
      
      tuya_plug1_current_consumption:
        friendly_name: "G5 current consumption"
        unit_of_measurement: 'W'
        value_template: "{{ states.switch.tuya_plug1.attributes.current_consumption }}" 

can you check if this log was a local communication (LAN) or it was a response from the cloud? if it was local, can you share de request log? maybe we can compare with the actual python request… so we can find the difference.

{“1”:true,“17”:18,“18”:0,“19”:0,“20”:2236,“21”:1,“22”:617,“23”:27108,“24”:15461,“25”:1348,“26”:0,“9”:0},"

I have this line from adroid app \shared_prefs\preferences_global_keyes file.
I don’t know how to check whats this response come from - cloud or lan
How can I extract the request log ?

Thanks

I have also these errors in log:

Szczegóły loga (ERROR)
Logger: homeassistant.helpers.entity
Source: custom_components/localtuya/switch.py:147
First occurred: 11:39:01 (14 occurrences)
Last logged: 14:01:30

Update for switch.tuya_plug1 fails
Traceback (most recent call last):
  File "/config/custom_components/localtuya/switch.py", line 139, in __get_status
    status = self._device.status()
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 349, in status
    result = cipher.decrypt(result, False)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 87, in decrypt
    raw = cipher.decrypt(enc)
  File "/usr/local/lib/python3.8/site-packages/Crypto/Cipher/_mode_ecb.py", line 195, in decrypt
    raise ValueError("Data must be aligned to block boundary in ECB mode")
ValueError: Data must be aligned to block boundary in ECB mode

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 272, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 465, in async_device_update
    await self.hass.async_add_executor_job(
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/localtuya/switch.py", line 234, in update
    self._status = self._device.status()
  File "/config/custom_components/localtuya/switch.py", line 170, in status
    self._cached_status = self.__get_status()
  File "/config/custom_components/localtuya/switch.py", line 147, in __get_status
    raise ConnectionError("Failed to update status .")
ConnectionError: Failed to update status .

i’m having the same error, not able to get the power reading using localtuya. I’m using the NodeRed method :slight_smile:

I’m using the NodeRed method too. I can’t found solutions for localtuya

thanks! will try this one… I believe it has to do with the protocol… if node red work here, will try to find the differences…

nodered is working for me!!!
very, very, very important TIP: the devices will only send the energy consumption IF THERE IS CONSUMPTION! I was testing without appliances… in this case, it only sends “1” status…

2020-07-26 16:54:19 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_3/status: b'{"attributes":{"power":"3.3 W","current":"0 mA","voltage":"0 V"}}'
2020-07-26 16:54:24 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_3/status: b'{"attributes":{"power":"3.7 W","current":"31 mA","voltage":"0 V"}}'
2020-07-26 16:54:36 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"0 W","current":"26 mA","voltage":"213.3 V"}}'
2020-07-26 16:54:38 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"0 W","current":"23 mA","voltage":"0 V"}}'
2020-07-26 16:54:43 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.4 W","current":"22 mA","voltage":"0 V"}}'
2020-07-26 16:54:47 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.7 W","current":"21 mA","voltage":"212.9 V"}}'
2020-07-26 16:54:52 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.3 W","current":"23 mA","voltage":"0 V"}}'
2020-07-26 16:54:54 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.6 W","current":"22 mA","voltage":"0 V"}}'
2020-07-26 16:54:56 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_3/status: b'{"attributes":{"power":"3.2 W","current":"29 mA","voltage":"214.9 V"}}'
2020-07-26 16:55:03 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.3 W","current":"23 mA","voltage":"210.9 V"}}'
2020-07-26 16:55:03 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_3/status: b'{"attributes":{"power":"3.6 W","current":"31 mA","voltage":"0 V"}}'
2020-07-26 16:55:05 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.6 W","current":"22 mA","voltage":"0 V"}}'
2020-07-26 16:55:27 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"0 W","current":"25 mA","voltage":"211.6 V"}}'
2020-07-26 16:55:31 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.3 W","current":"22 mA","voltage":"210.9 V"}}'
2020-07-26 16:55:33 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.6 W","current":"0 mA","voltage":"0 V"}}'
2020-07-26 16:55:35 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.7 W","current":"27 mA","voltage":"0 V"}}'
2020-07-26 16:55:38 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tuya/switch_1/status: b'{"attributes":{"power":"2.6 W","current":"24 mA","voltage":"0 V"}}'

I got my switches to work by using Node-Red they are in sync with the app, home assistant
and pressing the button on the switch. They are only seconds apart. Node-Red has to be running all the time for it to work.

Hi,
I tried the custom component of @rospogrigio and the switch (Teckin SP22 model) is recognized locally and I can manage it.

I have the problem that sensor data (V, mA, W) is not detected by Home Assistant.

This is my string that I extrapolated from SmartLife + Android + Root:

{ “1”: true, “17”: 3, “18”: 180, “19”: 205, “20”: 2368, “21”: 1, “22”: 606, “23”: 31937, " 24 “: 18345,” 25 “: 1136,” 26 “: 0,” 9 ": 0}

This is the code present in the packages / localtuya.yaml file:

switch:

  • platform: localtuya
    host: 192.168.1.xxx
    local_key: a1111b222cd3e44
    device_id:aa11bcde5566778899f1gg
    name: presa_pc_studio_localtuya
    friendly_name: “presa_pc_studio_localtuya”
    icon: mdi:power-socket-de
    protocol_version: 3.3
    switches:
    sw01:
    name: smartplug1
    friendly_name: Smart Plug 1
    id: 1
    current: 18
    current_consumption: 19
    voltage: 20

sensor:

  • platform: template
    sensors:
    smartplug1_voltage:
    friendly_name: “Smart Plug 1 voltage”
    unit_of_measurement: ‘V’
    value_template: “{{ states.switch.smartplug1.attributes.voltage }}”

    smartplug1_current:
    friendly_name: “Smart Plug 1 current”
    unit_of_measurement: ‘mA’
    value_template: “{{ states.switch.smartplug1.attributes.current }}”

    smartplug1_current_consumption:
    friendly_name: “Smart Plug 1 current consumption”
    unit_of_measurement: ‘W’
    value_template: “{{ states.switch.smartplug1.attributes.current_consumption }}”

Where am I wrong? The LOG does not report errors

1 Like

I think the localtuya method is broken. Try NodeRed method

Hi,
having never used node-red, after installing it is it difficult to configure it to detect the socket data?
I should follow guide 2. Node red configuration (2.2 Test node
Put settings into the tuya-local node in the simple testing flow from the previous post and test if you get the connection)

1 Like

Hi,
I installed nodered and reported the code that I found in step 3, I entered the values of my sockets in switch1 and switch2, I reported id and ip in the tuya device set, I inserted the switches and sensors in configuration.yaml, but the values are not reported and the socket status is off even if it is on.

Do I have to enter values manually also in the mqtt device set?

By doing the test of post 2.2, instead, under “msg” the values of 18 and 19 were reported.

What can I check?

1 Like

Hi @szkari,
I have six Bakibo smart plugs with power metering and original firmware and localtuya is working flawlessy in my configuration.

I’ve installed @rospogrigio 's repo on GitHub
[https://github.com/rospogrigio/localtuya-homeassistant]

I’m not completely sure if it is his repo but his nickname is so unique…

@SmartM-ui, who is a friend of mine, was unable to do the same with his Teckin SP22.
I wonder if this issue is hardware related or the @rospogrigio 's release needs an update …

What I am sure of is that localtuya works … only under certain conditions …

1 Like

I can confirm that with SP22 does not works this method, i could debug the problem:

  1. The socket is connected successfully
  2. The command to get status is sent successfully
  3. The response of the plug cannot be handled by pytuya package and falls in a exception

There is a mismatch of payload response of the plug, pytuya package cannot handle the response and the general purposes log message is “response is unvalid”.

Any idea? another method is trying to flash Tasmota firm…

1 Like

Hi @raimangsxr ,
have you tried node-red?
If so, could you tell me the procedure?
I have an error on function “df5da0e3.1e9ba8”: “TypeError: Cannot use ‘in’ operator to search for ‘1’ in undefined”
I also have the Teckin SP22
Thanks