Tuya switches with energy monitoring support

The number you get is the first number from your device-id
The bash-script is ereasing all others characters then the real result
And if you dont get the correct result, all the rest will be fu*ked up

You have to find out why you cant connect to your plug with plugpower.py
Are you using python or python3 ?
Can you ping the plug from the terminal you try the script from ?

From start the real result is

pi@raspberrypi:~/Downloads/powermonitor $ python plugpower.py
Polling Device 46268374cXXXXXXXX at 192.168.0.XXX
Dictionary {u'devId': u'46268374cXXXXXXX', u'dps': {u'24': 17521, u'25': 1190, u'20': 2312, u'21': 1, u'22': 635, u'23': 30333, u'19': 0, u'18': 0, u'1': False, u'9': 0}}
Switch On: False

First line in bash-script is deleting first row Polling Device 46268374cXXXXXXXX at 192.168.0.XXX
echo -e “$(sed ‘1d’ /home/pi/script/power/plug.log)\n” > plug.log

Next line is deleting 139 first characters Dictionary {u’devId’: u’46268374cXXXX’, u’dps’: {u’24’: 17521, u’25’: 1190, u’20’: 2297, u’21’: 1, u’22’: 635, u’23’: 30
333, u’19’:
sed ‘s/^.{139}//’ plug.log > plug1.log

Next line is break all numbers to rows
0
18
0
1
9
0
grep -o -E ‘[0-9]+’ plug1.log

Next line is cut out the single row with the real result
0
sed -n ‘1p’ plug.log

Next line is a math-calculator to multipli the result with 0,1
perl -pe ‘s/\b(\d+.)?\d+\b/$&*0.1/ge’ plug1.log

if i change the ip on my plug and try polling, it will result 4626837.4 and its the first number from device-id multipli with 0.1 ( 46268374 *0,1 = 4626837.4 )

Thanks for the response, although to be honest, I’m not really sure whats going on there or why I’m not getting the results. Either-way, I’ve found a much simpler & more useful solution that also makes it easy to get power usage and on/off status for multiple tuya devices.

I’ll post a walk-through over the next day or two and post it here.

Thanks for the assistance.

Yes, please post a walkthrough! This will be very useful for automation and stuff like that.

1 Like

any chance you will be posting the simpler method. Tuya convert isnt working for me

@teitelbot @chilman4081 Soon. I was just in the middle of writing a python library that can be really easily used inside HA. But, my system died and I’m currently rebuilding my whole Home Assistant setup. The Tuya library should be ready, I just need to test it fully and then write a post detailing it. If you can wait a weeks or so, I’ll hopefully have it online.

2 Likes

@teitelbot @chilman4081 The python package is finished and uploaded to pypi (tuyapower2mqtt) and I’ll have a walkthrough & call for testers online within the next 24hrs max.

3 Likes

Does it matter what firmware is currently on the device?

@Micke_Hedqvist Having extended the script by Jason Wilcox and contributed to the project, I learned that your script and his doesn’t work with the newer firmware versions which have encryption and a differently formatted output.

I deciphered this data and Jason pulled my pr’s in to his script. I’ve since created a newer script that can be installed via pip. It also reports the Tuya data via MQTT so that it’s easily grabbed by Home Assistant. You can find my package on pypi as TuyaPower2MQTT (pip install tuyapower2mqtt). I’ll be posting a walkthrough and call for testers in the next few hours.

1 Like

@teitelbot Not with my package. It works with both the older format (firmware 1.0.4 and below) as well as newer version 3.3 devices( firmware 1.0.5 and above).

@Malaga82 It looks like you probably have a device with the newer firmware. These require some updated code over that with @Micke_Hedqvist suggested. You also need to provide the key with these newer devices, otherwise they will just close the connection and not report any data. I’ll be posting a walkthrough and call for testers for my new python package that can handle both new and old devices, and then reports the stats via MQTT.

Here’s the link to the walkthrough for bookmarking. I’ll have the details filled in the next 24hrs, but feel free to take a look at the beginnings of a guide: https://community.home-assistant.io/t/tuya-switches-w-power-usage-data-no-tuyaconvert-needed

1 Like

This is my way yo use this. I’m using docker image for this and extract the data.
my linux script:

#!/bin/bash

cd /home/ha/Power-monitor
docker run --rm --name test-pistik -e PLUGID='XXXXXXXXXXXXXXXX' -e PLUGIP="192.168.1.102" -e PLUGKEY="0123456789abcdef" jasonacox/powermonitor > plug.log
mosquitto_pub -h 192.168.1.18 -t devices/tuya-test/current -m "$(grep Current plug.log | cut -d ':' -f 2 | tr -d " " | awk '{printf("%.2f\n", $1)}')"
mosquitto_pub -h 192.168.1.18 -t devices/tuya-test/power -m "$(grep Power plug.log | cut -d ':' -f 2 | tr -d " " | awk '{printf("%.2f\n", $1)}')"
mosquitto_pub -h 192.168.1.18 -t devices/tuya-test/voltage -m "$(grep Voltage plug.log | cut -d ':' -f 2 | tr -d " " | awk '{printf("%.2f\n", $1)}')"


sensors.yml

  - platform: mqtt
    name: "Test-Pistik power"
    state_topic: "devices/tuya-test/power"
    unit_of_measurement: W
  - platform: mqtt
    name: "Test-Pistik current"
    state_topic: "devices/tuya-test/current"
    unit_of_measurement: W    
  - platform: mqtt
    name: "Test-Pistik voltage"
    state_topic: "devices/tuya-test/voltage"
    unit_of_measurement: W    

Is it feasible to use the data from these plugs to get the total consumption over a period of time accurately ?

I am just wondering since you can’t poll it every second it’s hard to use it as a total energy consumption monitor , perhaps there is a better alternative for that purpose like the tuya API ?

Can you please share how your automations look? I want to make that when power consuption goes above 100w, it does something (turns switch) and when process is finished (power 0w for more than 5s) it will send notification (notifications in node red already works for me). Can I do this without mqtt just in node red?

sorry i used Node-Red, refer previous posts in this thread

I know that you are using node red. I would like the same. But on your images, output is msg.payload, as on https://flows.nodered.org/node/node-red-contrib-tuya-smart My question is how to make automation as output

node red will publish the values into MQTT , then setup a MQTT sensor with HA

within the actual nodered diagram the plug1 is the MQTT publisher , the msg.payload and set,true are for debugging

I get all the data in one sensor entity that is useless for automations…

refer my HA Sensor setup , you need to extract the individual attributes (data).

Local Control???

Old post, but I thought I’d mention in case someone else stumbled upon this as I did.
I got a TUYA based power meter to work now (ARLEC GRID brand, July 2022). I had to manually enable the extra sensors in Home Assistant for the device, just done in the GUI, nothing overly technical, but its working a charm now!