DEV Community

adrian cockcroft
adrian cockcroft

Posted on

1

Measuring Energy Usage

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.

% pip3 install python-kasa
Enter fullscreen mode Exit fullscreen mode

I ordered a Kasa KP115 smart plug from Amazon for $22.99.

Smart pług

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>
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay