Git Product home page Git Product logo

powermonitor's Introduction

PowerMonitor (Tuya Power Stats)

Monitor power usage through WiFi Smart Plug

NOTICE: This project has been replaced by a PyPi module, tuyapower, to simplify adding this capability to your python projects.

Description

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

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 scripts here. The powerplug.py script responds with a human redable output of state (on/off), current (mA), voltage (V), and power (W). The powerjson.py script responds with JSON containing the same but adds a timestamp for convient time series processing.

REQUIRED: IP address and Device ID of smart plug.

Preparation

  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. 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. Again 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.

  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 here https://github.com/clach04/python-tuya/wiki or follow these instructions: https://github.com/jasonacox/tuyapower#tuya-device-preparation

Setup: Option 1 - Docker

Build a docker container using Dockerfile or get it at Docker Hub: https://hub.docker.com/r/jasonacox/powermonitor

# build powermonitor container
docker build -t powermonitor .

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

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

Setup: Option 2 - Manually (Tested on RaspberryPi):

The script does not need docker but it does require the pytuya and pycrypto python library. Follow these steps to set it up and run the script:

  1. Install pip and python libraries if you haven't already:
 sudo apt-get install python-crypto python-pip		
 pip install pycrypto	# or pycryptodome
 pip install pytuya
 pip install Crypto		# some systems will need this
 pip install pyaes		# some systems will need this
 
  1. Run the python script:
 #Devices with older firmware (1.0.4 and below)
 python plugpower.py {DEVICEID} {DEVICEIP} {DEVICEKEY [optional]} {DEVICEVERS [optional]}

eg:

 #Devices with older firmware (1.0.4 and below)
 python plugpower.py 01234567890 10.0.1.99 0123456789abcdef
 #Devices with newer firmware (1.0.5 and above)
 python plugpower.py 01234567890 10.0.1.99 0123456789abcdef 3.3

JSON Output Script

The plugjson.py script works the same as plugpower.py but produces the data in JSON output with a datetime stamp. This makes it easier to feed into other systems for recording, alerting or graphing.

Example Output

Docker

$ docker run -e PLUGID='01234567891234567890' -e PLUGIP="10.0.1.99" -e PLUGKEY="0123456789abcdef" jasonacox/powermonitor

Polling Device 01234567891234567890 at 10.0.1.99 with key 0123456789abcde1
Dictionary {'devId': '01234567891234567890', 'dps': {'1': True, '2': 0, '4': 69, '5': 12, '6': 1181}}
Switch On: True
Power (W): 1.200000
Current (mA): 69.000000
Voltage (V): 118.100000
Projected usage (kWh):  Day: 0.028800  Week: 0.201600  Month: 0.873600

Command Line / Bash Script

$ bash test.sh 01234567890 10.0.1.99 0123456789abcdef 3.1
JSON Output - plugjson.py:
{ "datetime": "2019-08-31T07:20:59Z", "switch": "True", "power": "1.2", "current": "70.0", "voltage": "122.1" }

TEXT Output - plugpower.py:
Polling Device 01234567891234567890 at 10.0.1.99 with key 0123456789abcde1
Dictionary {u'devId': u'01234567891234567890', u'dps': {u'1': True, u'2': 0, u'5': 13, u'4': 70, u'6': 1220}}
Switch On: True
Power (W): 1.300000
Current (mA): 70.000000
Voltage (V): 122.000000
Projected usage (kWh):  Day: 0.031200  Week: 0.218400  Month: 0.946400

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

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

powermonitor's People

Contributors

codeclinic avatar dotmaster avatar jasonacox avatar xinyic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

powermonitor's Issues

Turns off switch?

Some users report some plugs turns off randomly after using plugpower.py to poll for power data. See: https://community.home-assistant.io/t/tuya-power-consumption-integration/106839/2

I have not been able to reproduce this. In order to change state on a device you need to send a valid KEY. The key in the plugpower.py script is a random/bogus key to prevent any state change on the device.

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. If you are seeing this behavior, I recommend pulling the latest and trying again. Please let me know your result.

did not work on Gosung SP1 v2.4

after the first run with docker i got this messages:

9b5ca1b7ed17: Pull complete Digest: sha256:a9c34fdb540a2d042197028cf3886123b7cc5e517359f4f6844b7275d78e122c Status: Downloaded newer image for jasonacox/powermonitor:latest ERROR:pytuya:Unexpected status() payload=b'3.3\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x01\x19\xf9\xa2\x17%\xe6\xfek\xe8v-\x11\x86\x04@\xb5K\x866\x19\x97Oce\x8b+x\x88\xd6\x187\x8cI\xa7\x15(\x86/\xd0L\xc54\xf8\x92N+#z\xdf2V\xfea\xb4\x82n\x9f"\xea%\xc4\xd6\x89\x1din8\xfb\x8d\x0b\x92G\x93\xb3\xadq\x1f\xa3\x11l' ERROR:pytuya:Unexpected status() payload=b'3.3\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x01\x19\xf9\xa2\x17%\xe6\xfek\xe8v-\x11\x86\x04@\xb5K\x866\x19\x97Oce\x8b+x\x88\xd6\x187\x8c>(\x97\xf7\xbb1\x9d\x81\xb8\x96\xafb\xc7[\xec\xca3\x1a\xd2\xadN\xbe\xbf\x82\xf5\xfc-\xc4\x1a\xa1\xc1\x1fin8\xfb\x8d\x0b\x92G\x93\xb3\xadq\x1f\xa3\x11l' Polling Device 60020372840d8e6fa9d2 at 192.168.2.38 Dictionary b'3.3\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x01\x19\xf9\xa2\x17%\xe6\xfek\xe8v-\x11\x86\x04@\xb5K\x866\x19\x97Oce\x8b+x\x88\xd6\x187\x8cI\xa7\x15(\x86/\xd0L\xc54\xf8\x92N+#z\xdf2V\xfea\xb4\x82n\x9f"\xea%\xc4\xd6\x89\x1din8\xfb\x8d\x0b\x92G\x93\xb3\xadq\x1f\xa3\x11l' Dictionary b'3.3\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x01\x19\xf9\xa2\x17%\xe6\xfek\xe8v-\x11\x86\x04@\xb5K\x866\x19\x97Oce\x8b+x\x88\xd6\x187\x8c>(\x97\xf7\xbb1\x9d\x81\xb8\x96\xafb\xc7[\xec\xca3\x1a\xd2\xadN\xbe\xbf\x82\xf5\xfc-\xc4\x1a\xa1\xc1\x1fin8\xfb\x8d\x0b\x92G\x93\xb3\xadq\x1f\xa3\x11l' ERROR: No response from plug 60020372840d8e6fa9d2 [192.168.2.38].

after that, i got no message from the script. testet on a synology docker and a raspi4 in python.
greetz Stefan
Gosung SP1 v2.4 arendst/Tasmota#5323

Doesn't work with these Tuya smart sockets

Both docker and native options didn't yield any information for me for this product:
https://www.aliexpress.com/item/32975898978.html?spm=a2g0s.9042311.0.0.27424c4dtPBf8A

My output is as follows:

PS C:\Users\...\Desktop\powermonitor-master> docker run -t -e PLUGID='44563025d8f15bc2f8b6' -e PLUGIP="<Device's Local IP Address>" jasonacox/powermonitor
Polling Device 44563025d8f15bc2f8b6 at <Device's Local IP Address> with key 0123456789abcdef
Unexpected status() payload=b''
Dictionary b''
Unexpected status() payload=b''
Dictionary b''
Unexpected status() payload=b''
Dictionary b''
ERROR: No response from plug 44563025d8f15bc2f8b6 [<Device's Local IP Address>].

I tried with my Global IP Address as well just to make sure I did not misunderstand and that's what the script expects:

PS C:\Users\...\Desktop\powermonitor-master> docker run -t -e PLUGID='44563025d8f15bc2f8b6' -e PLUGIP="<My Global IP Address>" jasonacox/powermonitor
Polling Device 44563025d8f15bc2f8b6 at <My GlobalIP Address> with key 0123456789abcdef
ERROR: No response from plug 44563025d8f15bc2f8b6 [<My Global IP Address>].

Can it be integrated in Home Assistant?

Perhaps dockerized and/or as an alternative to the current Home Assistant Tuya integration: https://www.home-assistant.io/components/tuya/

This integration works good, obviously, but it misses the power metering feature and I believe your project can improve this integration a lot!

I bought some of these plugs and after discovering the firmware is locked and can't be tuya-flashed, I tried to use the integration above thinking that it would have worked anyway even if without MQTT.
Then I saw that also the power metering wasn't there.

Thank you

PyPi Release Version

Hi Jason,

I'm thinking that this library would be a lot more useful if people could find it on PyPi as this would make it easier to install but also mean it could be used directly in Home assistant and probably other system without the need for the user to install/setup.

I'm just glancing at the process and I've already noticed that it would require removing all "print" statements and removing the dockerfile. So, I'm just wondering if you have any thoughts on the best way to proceed. The last thing I want to do is just fork this and create a separate PyPi version without seeing if you have any preferences/ideas on the best way to go.

Docker Raspberry4

No work over docker in RPi4
Status: Downloaded newer image for jasonacox/powermonitor:latest standard_init_linux.go:211: exec user process caused "exec format error"
`docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46
Built: Wed Sep 16 17:07:02 2020
OS/Arch: linux/arm
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46
Built: Wed Sep 16 17:00:52 2020
OS/Arch: linux/arm
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683`

Request KWh reading

Thanks. Work great. Is it possible to add KWh? It will be great to capture total power used over time. Thanks.

unexpected status()

I have a plug which is joined to my tuya app on my iPhone and the device is being seen in Home Assistant. I wish to monitor power as well.

I grabbed the device ID and IP address and tried to run this script but am getting an error:

Polling Device ebf8372483a88daab4ckmy at 10.211.2.137 with key 0123456789abcde1 and protocol version 3.1
Unexpected status() payload=b'\xf1b\xe6\x13^\xcdc\xc3\x8f\xf4\xab\xfa\tB\xc7\xbb\xefxU\xd9g.,\xd2\xc6\xd8\xfc\xb3@A\x17%'
Dictionary b'\xf1b\xe6\x13^\xcdc\xc3\x8f\xf4\xab\xfa\tB\xc7\xbb\xefxU\xd9g.,\xd2\xc6\xd8\xfc\xb3@A\x17%'
Unexpected status() payload=b'\xf1b\xe6\x13^\xcdc\xc3\x8f\xf4\xab\xfa\tB\xc7\xbb\xefxU\xd9g.,\xd2\xc6\xd8\xfc\xb3@A\x17%'
Dictionary b'\xf1b\xe6\x13^\xcdc\xc3\x8f\xf4\xab\xfa\tB\xc7\xbb\xefxU\xd9g.,\xd2\xc6\xd8\xfc\xb3@A\x17%'

Please advise.

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.