tinytuya icon indicating copy to clipboard operation
tinytuya copied to clipboard

Is it possible to have statistical data too?

Open mauriziozito01 opened this issue 3 years ago • 5 comments

I would also need to have the total electricity consumed during the day. I believe the API returns this data.

thanks

Maurizio

mauriziozito01 avatar Nov 29 '20 13:11 mauriziozito01

It depends on your switch. If it is a 3.3 version plug and returns DPS index 17, that is kWh (see link here) which means the switch is keeping track of the power usage over time. However, in my experience, most Tuya outlets with energy monitoring do not have this memory and behave more like the basic switch type (here). In that case, the kWh value you see from the SmartLife app is coming from the TuyaCloud where they are computing the kwh for you (kWh = kW * t):

If your load is constant (wattage doesn't vary much during the day), you can estimate kWh by multiplying the wattage by 24 and dividing by 1000. If the wattage varies, you have to make multiple samples of the wattage over time. If you were to plot all of those samples onto the graph, over time, basically what you want is the area under the graph (the integral):

image

If you measure in a sensible, constant time interval, just adding them up and multiplying the result with that interval will give you your total energy reading. Something like this would do:

 ### "Powers" is a list of your power measurements in Watts
 ### "T" contains the sample interval in seconds

total_power = sum(powers) / 1000 * T / 3600

jasonacox avatar Nov 29 '20 17:11 jasonacox

Great answer! And I like to see an integral 😄.

FYI, I'm working to take samples on a regular basis (as much as possible), then use the HTTP API of InfluxdB to store them. It has a built in integral function to collect the data afterwards https://docs.influxdata.com/influxdb/v1.8/query_language/functions/#integral

Thanks!

arrmo avatar Nov 29 '20 17:11 arrmo

Nice! @arrmo would you be willing to share your code? It would be good to add a script for energy monitoring to the examples folder. Feel free to submit a pull request as a contributor if you would like.

jasonacox avatar Nov 29 '20 22:11 jasonacox

Absolutely! Will do - just let me get it working first, but happy to share.

arrmo avatar Nov 30 '20 01:11 arrmo

As promised, my code - let me know what you think. From here, the data is in influxdb ... can use Chronograf, Grafana, or SQL queries. And this works for Tuya, also Kasa.

eMeter.zip

arrmo avatar Dec 03 '20 02:12 arrmo