Python module to read status and energy monitoring data from Tuya based WiFi smart devices. This includes state (on/off), current (mA), voltage (V), and power (wattage).
MIT License
This python module will poll WiFi Tuya compatible Smart Plugs/Switches/Lights for state (on/off), current (mA), voltage (V), and power (wattage).
This module uses the python tinytuya or pytuya library to poll Tuya compatible Smart Plugs, Switches and Lights for state and power data that can be used for point in time monitoring or stored for trending. There are two test scripts here. The plugpower.py script responds with a human readable output of state (on/off), current (mA), voltage (V), and power (W). The plugjson.py script responds with JSON containing the same but adds a timestamp for convenient time series processing.
Tested on RaspberryPi, Linux, Windows 10 and MacOS. Install pip and the following python libraries if you haven't already.
TuyaPower has been updated to use tinytuya
, a fork of pytuya
that adds support for device IDs of 20 and 22 characters (pytuya only supports 20 character IDs). Install tinytuya
to take advantage of that feature.
# Install required libraries
sudo apt-get install python-crypto python-pip # for RPi, Linux
python3 -m pip install pycryptodome # or pycrypto, pyaes, Crypto
python3 -m pip install tinytuya # or pytuya
python3 -m pip install tuyapower # Pull this tuyapower module from PyPi
Pulling data from Tuya devices on your network requires that you have the Device IP, ID, VERSION and KEY (for 3.3 devices). The tuyapower
and tinytuya
modules include a scanner function to find Smart Plugs on your network. This will scan the network and identify Device's IP, ID and VERSION. It will not be able to get the local KEY. Since newer 3.3 devices will require the KEY, the following steps will help you determine the KEYs for your devices:
python3 -m tuyapower
From iot.tuya.com
From your Local Workstation
python3 -m tinytuya wizard
# If you are using windows command prompt w/o color use:
python -m tinytuya wizard -nocolor
devices.json
. TinyTuya will use this file to provide additional details to scan results from tinytuya.scanDevices()
or when running python3 -m tinytuya
to scan your local network.Notes:
npm i @tuyapi/cli -g
and run tuya-cli wizard
(on, w, mA, V, err) = tuyapower.deviceInfo(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
rawData = tuyapower.deviceRaw(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
tuyapower.devicePrint(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
dataJSON = tuyapower.deviceJSON(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
verbose = False
devices = tuyapower.deviceScan(verbose)
Note: If error occurs, on will be set to false, w, mA and V will be set to 0.
You can import the tuyapower module into your own python projects and use the deviceInfo(), deviceJSON(), deviceScan() and devicePrint() functions to access data on your Tuya devices. Here are some examples:
# Poll a Single Devices
import tuyapower
PLUGID = '01234567891234567890'
PLUGIP = '10.0.1.99'
PLUGKEY = '0123456789abcdef'
PLUGVERS = '3.1'
(on, w, mA, V, err) = tuyapower.deviceInfo(PLUGID,PLUGIP,PLUGKEY,PLUGVERS)
tuyapower.deviceJSON(PLUGID,PLUGIP,PLUGKEY,PLUGVERS)
'{ "datetime": "2019-10-13T03:58:57Z", "switch": "True", "power": "1.2", "current": "70.0", "voltage": "122.1", "response": "OK" }'
tuyapower.devicePrint(PLUGID,PLUGIP,PLUGKEY,PLUGVERS)
TuyaPower (Tuya Power Stats)
Device 03200160dc4f2216ff61 at 10.0.1.5 key 0123456789abcdef protocol 3.1:
Switch On: True
Power (W): 1.200000
Current (mA): 70.000000
Voltage (V): 122.100000
Projected usage (kWh): Day: 0.028800 Week: 0.201600 Month: 0.873600
# Scan Network for All Devices
# To see output on stdout set verbose True
tuyapower.deviceScan(True)
TuyaPower (Tuya compatible smart plug scanner) [0.0.16]
Scanning on UDP ports 6666 and 6667 for devices...
FOUND Device [Valid payload]: 10.0.1.100
ID = 01234567891234567890, Key = 0123456789abcdef, Version = 3.1
Stats: on=True, W=6.0, mA=54.0, V=121.1 [OK]
FOUND Device [Valid payload]: 10.0.1.200
ID = 01234567891234567891, Key = 0123456789abcdea, Version = 3.1
Stats: on=True, W=-99, mA=-99, V=-99 [Power data unavailable]
FOUND Device [Valid payload]: 10.0.1.222
ID = 01234567891234567893, productKey = 0123456789abcdea, Version = 3.3
Device Key required to poll for stats
Scan Complete! Found 3 devices.
# Scan the network and unpack the response
devices = tuyapower.deviceScan()
for ip in devices:
id = devices[ip]['gwId']
key = devices[ip]['productKey']
vers = devices[ip]['version']
(on, w, mA, V, err) = deviceInfo(id, ip, key, vers)
print("Device at %s: ID %s, state=%s, W=%s, mA=%s, V=%s [%s]"%(ip,id,on,w,mA,V,err))
The function tuyapower.scan()
will listen to your local network and identify Tuya devices broadcasting their IP, Device ID, Product Key (not the Local KEY) and protocol Version. It will print the list of devices and for 3.1 protocol devices that don't require local KEY, it will display their energy stats. This can help you get a list of compatible devices on your network. The tuyapower.deviceScan()
function returns all found devices and their stats via a dictionary result.
You can run the scanner from the command line using this:
python3 -m tuyapower
By default, the scan functions will retry 15 times to find new devices. If you are not seeing all your devices, you can increase max_retries by passing an optional arguments (ex. 50 retries):
# command line
python3 -m tuyapower 50
# invoke verbose interactive scan
tuyapower.scan(50)
# return payload of devices
devices = tuyapower.deviceScan(false, 50)
A docker container version of the tuyapower library is available on DockerHub and can be used to grab power data without installing the python libraries.
# Friendly Output
# Run tuyapower container - replace with device ID, IP and VERS
docker run -e PLUGID="01234567891234567890" \
-e PLUGIP="10.0.1.x" \
-e PLUGKEY="0123456789abcdef" \
-e PLUGVERS="3.3" \
jasonacox/tuyapower
# JSON Output
# Run tuyapower container - replace with device ID, IP and VERS
docker run -e PLUGID="01234567891234567890" \
-e PLUGIP="10.0.1.x" \
-e PLUGKEY="0123456789abcdef" \
-e PLUGVERS="3.3" \
-e PLUGJSON="yes" \
jasonacox/tuyapower