Tuya power consumption integration

the current Tuya component can only power on/off the device but doensn’t retrieve the power comsumption.
This integration here:

is able to retrieve those values, would be great to integrate it in the current component or just create a new one only to get those values.

Thank you

I got the value in to HA with MQTT

But problem is that the plug turns off randomly now when i fetch the value with powermonitor…


Same issue. Returns the values OK, but then turns off the plug. ;-(

I install it on a RPI and it´s still up n runnig
I fetch with a bashscript
python /home/pi/Downloads/powermonitor/plugpower.py > plug.log

Polling Device xxxxxx at 192.168.xxxxxx
Dictionary {u’devId’: u’xxxxxx ‘, u’dps’: {u’24’: 17521, u’25’: 1190, u’20’: 2290, u’21’: 1, u’22’: 635, u’23’: 30333, u’19’: 13, u’18’: 9, u’1’: True, u’9’: 0}}
Switch On: True

I run the script from Crontab every 3 min

I’m using it to monitor power consumption. works flawlessly.

I updated the firmware on the Tuya, and that seemed to have solved it. It now polls without shutting off the switch.

Ouch, spoke too soon. The ‘plugpower.py’ code will randomly shut the switch off. Not good, as it powers my fridge.

I’m running on an Intel NUC, Ubuntu Linux. I’ll poke around the API and see what may be causing it.

I have never seen the powermonitor script cause action on a device. What device are you using? The KEY that is used is a bogus value which should prevent state changes on the device and only allows the script to read state values.

I updated the pytuya library in jasonacox/powermonitor to the latest version and also added PLUGKEY as a user defined environmental value if you need to change it. You might want to pull the latest and try again.

export PLUGID='xxxxxxx' 
export PLUGIP="10.0.x.x" 
export PLUGKEY="0123456789abcde1"

Polling Device xxxxxxx at 10.0.x.x with key 0123456789abcde1
Dictionary {u'devId': u'xxxxxxx', u'dps': {u'1': True, u'2': 0, u'5': 12, u'4': 70, u'6': 1220}}
Switch On: True
Power (W): 1.200000
Current (mA): 70.000000
Voltage (V): 122.000000
Projected usage (kWh):  Day: 0.028800  Week: 0.201600  Month: 0.873600

or using docker:

$ docker run -e PLUGID='xxxxxxx' -e PLUGIP="10.0.x.x" -e PLUGKEY="0123456789abcde1" jasonacox/powermonitor
Polling Device xxxxxxx at 10.0.x,x with key 0123456789abcde1
Dictionary {'devId': 'xxxxxxx', 'dps': {'1': True, '2': 0, '4': 70, '5': 13, '6': 1211}}
Switch On: True
Power (W): 1.300000
Current (mA): 70.000000
Voltage (V): 121.100000
Projected usage (kWh):  Day: 0.031200  Week: 0.218400  Month: 0.946400

Just wondering if you had any further updates regarding this? I’m wondering if I should buy some sonoffs instead of waiting for a Tuya / HA solution.

Hi Jason,
Would there be any chance that you could give some details on how to integrate a Tuya switch (power monitoring) with Home assistant?

I’d really appreciate it.

Could this be added to PyPi, and then it can be integrated into Home Assistant?

Also, I’ve very little experience with Docker… Does Power monitor have to be run as a docker container? I’m a little confused as it not explicitly mentioned, especially if you are following ‘Build & Run > Option 2’.

Any clarification / pointers on getting this setup would be really appreciated.

Hi Phill, no, you do not need docker. The powermonitor script will run with just python and the pycrypto package. “Option 2” has you: 1) edit the script to add your DeviceID and IP address, 2) install the pycrypto package with pip install pycrypto. After that just run python plugpower.py. I’ll edit the README to better explain that.

I’ll investigate to see what it would take to integrate Tuya and HA.

1 Like



P.s. HA can automatically use anything that is in the PyPi directory. Although I’ve no idea what that involves.

I have a TopGreener TGWF15RM device.

I pulled the latest code, used docker this time, and made sure the KEY was a bogus value. I polled the device, got the correct power information, …and right after running the program the switch turned off

Polling Device 17778125dc4f22856c29 at with key 0123456789abcdef
Dictionary {‘devId’: ‘17778125dc4f22856c29’, ‘dps’: {‘1’: True, ‘2’: 0, ‘4’: 1027, ‘5’: 1726, ‘6’: 1210, ‘7’: 1, ‘8’: 1794, ‘9’: 11303, ‘10’: 48714, ‘11’: 449}}
Switch On: True
Power (W): 172.600000
Current (mA): 1027.000000
Voltage (V): 121.000000
Projected usage (kWh): Day: 4.142400 Week: 28.996800 Month: 125.652800

Hi Mark, thanks for the update. This is an interesting and concerning result. I would advise against using that smart switch at all, and here is why: In your example, you are sending a bogus “key” . Actions and commands against a smart switch require a valid key or you are at risk of being hacked. Based on the vulnerability you have uncovered with the TopGreener switch, a bad actor could potentially destroy whatever appliance is attached to that switch by sending rapid on-off sequences to the switch (think Stuxnet malware). Since the powermonitor code and tuya library is only polling for information instead of requesting action, the only other thing I can think of is that the switch’s processor and wifi logic is underpowered or buggy and asking for the power usage data with a bogus key is causing it to crash, subsequently dropping the control signal to the relay (causing it to turn off). Again, that vulnerability opens you up to attack if an intruder or malware enters your local network. I’ll add a note to my project about this switch. Thanks for doing the test!

I’ve had a good experience flashing my Tuya plugs using tuya convert to get ESPHome on them, and then integrating it into Home Assistant.
May be an option? You can measure daily energy use too

Can you please give example how do you get this data out?