Giter VIP home page Giter VIP logo

tuyapower's Introduction

TuyaPower - Python Module

This python module will poll WiFi Tuya campatible Smart Plugs for state (on/off), current (mA), voltage (V), and power (wattage).

Description

This project is based on the python pytuya library to poll Tuya campatible Smart Plugs 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 test.py script responds with a human redable output of state (on/off), current (mA), voltage (V), and power (W). The test-json.py script responds with JSON containing the same but adds a timestamp for convient time series processing.

Preparation

The tuyapower module includes a scanner function deviceScan() to find Smart Plugs on your network. However, it may or may not be able to detect all of them. You can use the following to manually identify the required IP address and Device ID of the smart plug:

  1. Download the Smart Life - Smart Living app for iPhone or Android. Pair with your smart plug (this is important as you cannot monitor a plug that has not been paired).

  2. Device ID - Inside the app, select the plug you wish to monitor, select the 3 dots(Jinvoo) or the edit/pencil icon(Tuya & SmartLife) in the top right and then "Device Info". The page should display "Device ID" which the script will use to poll the plug. It's also worth noting the MAC address of the device as it can come in handy in step 3.

  3. IP Address - If your router displays a list of all the devices that are connected to it, you can search for the MAC address of the device. This is often the quickest way to locate your device IP.

    Alternatively, you will need to manually determine what IP address your network assigned to the Smart Plug - this is more difficult but it looks like arp-scan can help identify devices on your network. WiFi Routers often have a list of devices connected as well. Look for devices with a name like "ESP_xxxxxx". Many modern routers allow you to set the hostname of connected devices to something more memorable, once you have located it.

  4. Firmware Version - Devices with newer firmware (1.0.5 and above) are typically using a different protocol (3.3). These devices need to be communicated with using encryption and the resultant data is packaged slightly differently. It's a good idea therefore to check the Firmware version of the device(s) too. In the Tuya/SmartLife/Jinvoo app there will be a device option "Check for Firmware Upgrade" or similar. Open this option and take note of the Wi-Fi Module & MCU Module numbers. These are usually the same.

    • Firmware 1.0.4 and lower: DEVICEVERS = 3.1
    • Firmware 1.0.5 and above: DEVICEVERS = 3.3
  5. Device Key - If your device is running Firmware 1.0.5 or above, you will need to obtain the Device Key. This is used to connect with the device decrypt the power consumption data. For details on how to do this, see point 2: https://github.com/clach04/python-tuya/wiki

Setup

Tested on RaspberryPi, Linux, Windows 10 and MacOS. Install pip and python libraries if you haven't already:

# Install required libraries
 sudo apt-get install python-crypto python-pip		# for RPi, Linux
 pip install pycrypto
 pip install pytuya
 # pip install Crypto		
 # pip install pyaes		
 pip install tuyapower  # Pull this tuyapower module from PyPi

For Windows 10 users or if you get errors related to Crypto, try installing the pycryptodome module:

pip install pycryptodome

Scan Tool

The function tuyapower.scan() will listen to your local network and identify Tuya devices broadcasting their IP, Device ID, Key and Version and will print that and their stats to stdout. 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 dictionary result).

You can run the scanner from the command line using this:

python -m tuyapower

Exmaple Usage

# 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))

Setup: Optional - Docker

Tested on Linux and MacOS. Build a docker container using Dockerfile

# build tuyapower container
docker build -t tuyapower .

# Devices with older firmware (1.0.4 and below)
# run tuyapower container - replace with device ID and IP 
docker run -e PLUGID='01234567891234567890' -e PLUGIP="10.0.1.x" -e PLUGKEY="0123456789abcdef" tuyapower

# Devices with newer firmware (1.0.5 and above)
# run tuyapower container - replace with device ID and IP 
docker run -e PLUGID='01234567891234567890' -e PLUGIP="10.0.1.x" -e PLUGKEY="0123456789abcdef" -e PLUGINVERS="3.3" tuyapower

Please note, these smart plugs and this script do not hold power usage data in memory so the "Projected usage" reported is an estimate based on current power readings and assumed steady state over time.

Example Products

Acknowledgements

Contributers

  • Jason A. Cox (jasonacox)
  • Phill Healey (codeclinic) - Integration for firmwares (1.0.5+) / protocol v3.3 & commandline arguments.

tuyapower's People

Contributors

basnijholt avatar jasonacox avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.