Tuya LOCAL with energy monitoring and without tuya-convert

I used android packet capture as described here.

Invalid config for [sensor.template]: [tuya_g4_voltage] is an invalid option for [sensor.template]. Check: sensor.template->tuya_g4_voltage. (See ?, line ?).
I keep getting this error!

https://paste.ubuntu.com/p/HgXbHK39Vs/
there is my code what am i doing wrong?

Paste the whole code you have for switch and sensor. I think you have an error in value.template. There should be the same as you have it as name on swihtch.

I got a 2 gang switch and I can’t get the second one to work. your yaml worked for one.

I trird to do this way the second gang dont work
could you help. thank you

I had the same issue. Got it working by switching to this custom component: https://github.com/fastcolors/localtuya-homeassistant

ok I use that one and I can’t get the switch to show up now
how do I config (dps)

node-red

node

I can get my tuya light switch to work in node-red
in a inject node
{“set”: true, “dps” : 2}
{“set”: false, “dps” : 2}
this is for switch 2
{“set”: true, “dps” : 1}
{“set”: true, “dps” : 1}
this is for switch 1
how an I but in in home assistant as a switch

I’m having some tuya smartlugs running non-esp82xx chip therefore cannot use tuya-convert and go for tasmota. Found this localtuya option and ended up here.

I’m trying to use https://github.com/rospogrigio/localtuya-homeassistant

However I’m unable to get DPS information as component seems to not load at all. What am I doing wrong? I’m running HA 0.111.4. I’ve cloned the repo and moved files to location I’m having for example hacs loaded

 /usr/share/hassio/homeassistant/custom_components/localtuya
 .
 ├── cover.py
 ├── __init__.py
 ├── light.py
 ├── manifest.json
 ├── pytuya
 │   └── __init__.py
 └── switch.py
 
 1 directory, 6 files

I see no custom_components.localtuya in home assistant logs after enabling debug logs and restarting HA

 logger:
    default: debug

Why the custom_component is not loading? Any ideas, common mistakes?

EDIT: Ok, managed to get it load. I guess by adding platform: localtuya to configuration.yml. Not sure actually.

Anyways here is the new issue. I’m getting null for all values reported my smartplug. Only thing that works because of this is on/off switch but I especially want the other information

2020-06-27 18:39:09 DEBUG (SyncWorker_7) [custom_components.localtuya.pytuya] json_payload=b'{"devId":"mydevid","uid":"myuid","t":"15933149","dps":{"1":null,"101":null,"102":null}}'

So whats up?

1 Like

Loving this integration https://github.com/rospogrigio/localtuya-homeassistant
Thanks for posting and to all of the contributors.

After getting three of these devices working with this method I installed the
https://www.home-assistant.io/integrations/integration/
to get the kWh totalling up and then add
https://www.home-assistant.io/integrations/utility_meter/
to get daily totals.
Selection_382
Great work.

5 Likes

Same here, getting null dps results, only switch on/off is working.
Any help?

I managed to get the keys and ids from all in SmartLife coupled TUYA Plugs (BeLife)

I have a seperate RPi3 which I used because I didn’t like to use the Pi4 with HA. I used a excisting raspberry buster lite image on it which did not have cli installed. I used this link: here. It is in Italian but used google translate. This also didn’t work. But at the end it said to use the cli link procedure. Which is descibed here. This worked

Now: implement in HA. Next step.

Same here: " dps":{“1”:null,“101”:null,“102”:null}}’

i’m getting the same null dps results, and only switch on/off is working. anyone else is able to read the power, current and voltage reading using localtuya?

For me things work normally. Which sockets do you have?

I have BeLife sockets. https://www.bol.com/nl/p/belife-smart-plug-1-stuk-slimme-stekker-met-energiemeter-google-home-amazon-alexa-compatible-smart-home/9200000123431593/?s2a=#productTitle

I’m getting the errors below:

Update for switch.wm 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.7/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 466, in async_device_update
self.update # type: ignore
File “/usr/local/lib/python3.7/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 .

Update: 16th July 2020

Finally, I got it working via the NodeRed method instead of localtuya. I’m using the Tuya integration to control the On/Off switch, then create MQTT sensors to capture the power, current and voltage values.

Below is the sensor setting in configuration.yaml:

  • platform: mqtt
    name: “WM_Power”
    state_topic: “tuya/tuya_wm/status”
    unit_of_measurement: W
    value_template: “{{ value_json.attributes.power }}”

  • platform: mqtt
    name: “WM_Current”
    state_topic: “tuya/tuya_wm/status”
    unit_of_measurement: mA
    value_template: “{{ value_json.attributes.current }}”

  • platform: mqtt
    name: “WM_Voltage”
    state_topic: “tuya/tuya_wm/status”
    unit_of_measurement: V
    value_template: “{{ value_json.attributes.voltage }}”

  • platform: mqtt
    name: “AF_Power”
    state_topic: “tuya/tuya_af/status”
    unit_of_measurement: W
    value_template: “{{ value_json.attributes.power }}”

  • platform: mqtt
    name: “AF_Current”
    state_topic: “tuya/tuya_af/status”
    unit_of_measurement: mA
    value_template: “{{ value_json.attributes.current }}”

  • platform: mqtt
    name: “AF_Voltage”
    state_topic: “tuya/tuya_af/status”
    unit_of_measurement: V
    value_template: “{{ value_json.attributes.voltage }}”

Below is how my NodeRed flow like:

i got the same dps results.
Did you solve this already?

Yes, but not using localtuya.

I use nodered to publish the status values to mqtt. And setup sensor using mqtt, using the 1st method in this thread. However, I made some changes to the code in nodered function and the value template when setup the sensors

1 Like

Hello!

What wrong with our configs?
This is my “dps”:{“22”:606,“23”:26868,“1”:true,“24”:15119,“25”:1376,“26”:0,“17”:2,“18”:34,“19”:35,“9”:0,“20”:2240,“21”:1}
This is my conf:

    host: 192.168.1.65
    local_key: xxx
    device_id: xxx
    name: tuya_g5
    friendly_name: tuya_g5
    protocol_version: 3.3
    current: 18
    current_consumption: 19
    voltage: 20
    switches:
      sw01:
        name: tuya_g5
        friendly_name: tuya_g5
        id: 1

sensor:

- platform: template
  sensors:
    tuya_g5_voltage:
      friendly_name: "G5 voltage"
      unit_of_measurement: 'V'
      value_template: "{{ states.switch.tuya_g5.attributes.voltage }}"
      
    tuya_g5_current:
      friendly_name: "G5 current"
      unit_of_measurement: 'mA'
      value_template: "{{ states.switch.tuya_g5.attributes.current }}"
      
    tuya_g5_current_consumption:
      friendly_name: "G5 current consumption"
      unit_of_measurement: 'W'
      value_template: "{{ states.switch.tuya_g5.attributes.current_consumption }}"     

This is my log:

2020-07-22 21:55:00 DEBUG (SyncWorker_6) [custom_components.localtuya.pytuya] status() entry (dev_type is device22)
2020-07-22 21:55:00 DEBUG (SyncWorker_6) [custom_components.localtuya.pytuya] json_payload=b'{"devId":"xxx","uid":"xxx","t":"1595451300","dps":{"1":null,"101":null,"102":null}}'
2020-07-22 21:55:00 DEBUG (SyncWorker_6) [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\x07\x1c\x00\x00\x00\x01\xc1<H\t\x1c\xb8\xa0\xb7\x07\xe4LE\xe5\x98\xfa+>I\x16\xcduEJ^~m\xfaw~\xc0\x1a8\x1d\xe43\x99\xe8\x0f8\xd4x\x83\x01\xfd\xfc\xad\xe2\xc7\xdc\x86\x03g\x00\x00\xaaU'
2020-07-22 21:55:00 DEBUG (SyncWorker_6) [custom_components.localtuya.pytuya] result=b'\xc1<H\t\x1c\xb8\xa0\xb7\x07\xe4LE\xe5\x98\xfa+>I\x16\xcduEJ^~m\xfaw~\xc0\x1a8\x1d\xe43\x99\xe8\x0f8\xd4x\x83\x01\xfd\xfc\xad\xe2\xc7'
2020-07-22 21:55:00 DEBUG (SyncWorker_6) [custom_components.localtuya.pytuya] decrypted result='{"dps":{"1":true},"t":1595451299}'

Why we all don’t have current_consumption???

thanks! just got able to turn on and off with your template!

My DPS is not showing power consumption. Why is that?
In Smartlife app, the info is available…

{“dps”:{“1”:true},“t”:1595687103}’