Measuring Energy Usage
Published: 2022-10-31 23:17:27

Decided to figure out how to measure the energy used by a desktop computer and see if I can figure out a way to identify different workloads.

I have a Mac Studio M1 to run the workload, and an old MacBook laptop to run data collection on, so that it doesn't add to the workload.

First thing we need is a power monitoring plug that has an API. The TP-Link Kasa platform seems like a good place to start. It has a python based API available [on GitHub](https://github.com/python-kasa/python-kasa).

```
% pip3 install python-kasa
```

I ordered a [Kasa KP115 smart plug from Amazon](https://www.amazon.com/gp/product/B08LN3C7WK) for $22.99.

![Smart pług](https://m.media-amazon.com/images/I/61+Yr4-FRXL._AC_SL1500_.jpg)

Setup the plug using the mobile app, then:
```
% kasa
No host name given, trying discovery..
Discovering devices on 255.255.255.255 for 3 seconds
== PowerMeter - KP115(US) ==
	Host: 10.0.0.46
	Device state: ON

	== Generic information ==
	Time:         2022-10-31 15:21:56 (tz: {'index': 6, 'err_code': 0}
	Hardware:     1.0
	Software:     1.0.18 Build 210910 Rel.141202
	MAC (rssi):   10:27:F5:9C:37:12 (-49)
	Location:     {'latitude': 36.xxx, 'longitude': -121.xxx}

	== Device specific information ==
	LED state: True
	On since: 2022-10-31 12:35:15

	== Current State ==
	<EmeterStatus power=3.456 voltage=122.48 current=0.049 total=0.002>

	== Modules ==
	+ <Module Schedule (schedule) for 10.0.0.46>
	+ <Module Usage (schedule) for 10.0.0.46>
	+ <Module Antitheft (anti_theft) for 10.0.0.46>
	+ <Module Time (time) for 10.0.0.46>
	+ <Module Cloud (cnCloud) for 10.0.0.46>
	+ <Module Emeter (emeter) for 10.0.0.46>
```

Next - you have to specify host, or it will crash, and specifying type saves it an extra API call, and you get a single result

```
% kasa --host 10.0.0.46 --type plug emeter 
== Emeter ==
Current: 0.049 A
Voltage: 121.715 V
Power: 3.628 W
Total consumption: 0.002 kWh
Today: 0.002 kWh
This month: 0.002 kWh

```

Using a csh loop to get a 1 second trace of the data it's clear that the data changes about every 4 seconds.
```
% while 1
while? kasa --host 10.0.0.46 --type plug | egrep '(Time:|EmeterStatus)'
while? sleep 1
while? end
	Time:         2022-10-31 16:09:34 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=0.0 voltage=122.49 current=0.0 total=0.004>
	Time:         2022-10-31 16:09:35 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=0.0 voltage=122.49 current=0.06 total=0.004>
	Time:         2022-10-31 16:09:36 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=0.0 voltage=122.36 current=0.06 total=0.004>
	Time:         2022-10-31 16:09:38 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.68 voltage=122.36 current=0.06 total=0.004>
	Time:         2022-10-31 16:09:39 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.68 voltage=122.36 current=0.06 total=0.004>
	Time:         2022-10-31 16:09:40 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.68 voltage=122.36 current=0.05 total=0.004>
	Time:         2022-10-31 16:09:41 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.68 voltage=122.258 current=0.05 total=0.004>
	Time:         2022-10-31 16:09:42 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.253 voltage=122.258 current=0.05 total=0.004>
	Time:         2022-10-31 16:09:43 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.253 voltage=122.258 current=0.05 total=0.004>
	Time:         2022-10-31 16:09:45 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.253 voltage=122.258 current=0.049 total=0.004>
	Time:         2022-10-31 16:09:46 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.253 voltage=122.382 current=0.049 total=0.004>
	Time:         2022-10-31 16:09:47 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.253 voltage=122.382 current=0.049 total=0.004>
	Time:         2022-10-31 16:09:48 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.675 voltage=122.382 current=0.049 total=0.004>
	Time:         2022-10-31 16:09:49 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.675 voltage=122.382 current=0.012 total=0.004>
	Time:         2022-10-31 16:09:50 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.675 voltage=122.446 current=0.012 total=0.004>
	Time:         2022-10-31 16:09:52 (tz: {'index': 6, 'err_code': 0}
	<EmeterStatus power=3.675 voltage=122.446 current=0.012 total=0.004>
```

The output formatting is defined in cli.py

```
kasa --host 10.0.0.46 --type plug | awk '/Time:|EmeterStatus/{printf("%s %s ", $2, $3)}'; echo
2022-10-31 16:23:11 power=1.017 voltage=122.608 
```

That's all I have time to do for now, to be continued...