fffonion / tplink-plug-exporter

TP-Link Smart Plug Prometheus Exporter
BSD 2-Clause "Simplified" License
122 stars 20 forks source link

Support for HS300 Power Strip #8

Closed tideline3d closed 3 years ago

tideline3d commented 4 years ago

Loving this exporter, but I'm targeting it against my HS300 Power Strip and it appears to only be pulling one plugs power wattage. Is there a way to make it pull either each one individually (like its 6 HS110s) or just pull an aggregate for the whole strip?

fffonion commented 3 years ago

Hi @tideline3d I don't have a HS300 on my side to test, but I read that it uses same kasa protocol and probably have an extra rpc function to pull power consumption for whole strip. Any dump will be helpful like using tools from https://github.com/softScheck/tplink-smartplug.

tideline3d commented 3 years ago

I somehow missed this notification, will get you some info soon.

tideline3d commented 3 years ago

Here's the output of a couple of the commands from that tool. Let me know if you need something else?

-c energy output

"emeter": { "get_realtime": { "voltage_mv": 121487, "current_ma": 785, "power_mw": 92388, "total_wh": 16769, "err_code": 0 } }

-c info output

"system": { "get_sysinfo": { "sw_ver": "1.0.19 Build 200224 Rel.090814", "hw_ver": "1.0", "model": "HS300(US)", "deviceId": "80065BD45F0CE05BE7807BA186CCB7871B25D4B2", "oemId": "5C9E6254BEBAED63B2B6102966D24C17", "hwId": "34C41AA028022D0CCEA5E678E8547C54", "rssi": -57, "longitude_i": 0, "latitude_i": -778794, "alias": "TP-LINK_Power Strip_0786", "status": "new", "mic_type": "IOT.SMARTPLUGSWITCH", "feature": "TIM:ENE", "mac": "D8:0D:17:D8:07:86", "updating": 0, "led_off": 0, "children": [ { "id": "80065BD45F0CE05BE7807BA186CCB7871B25D4B200", "state": 1, "alias": "E3-Green", "on_time": 843976, "next_action": { "type": -1 } }, { "id": "80065BD45F0CE05BE7807BA186CCB7871B25D4B201", "state": 1, "alias": "RPi-Red", "on_time": 822261, "next_action": { "type": -1 } }, { "id": "80065BD45F0CE05BE7807BA186CCB7871B25D4B202", "state": 0, "alias": "E3-Blue", "on_time": 0, "next_action": { "type": -1 } }, { "id": "80065BD45F0CE05BE7807BA186CCB7871B25D4B203", "state": 0, "alias": "Air Filter", "on_time": 0, "next_action": { "type": -1 } }, { "id": "80065BD45F0CE05BE7807BA186CCB7871B25D4B204", "state": 1, "alias": "RPi-Blue", "on_time": 1380538, "next_action": { "type": -1 } }, { "id": "80065BD45F0CE05BE7807BA186CCB7871B25D4B205", "state": 1, "alias": "E3-Red", "on_time": 1380537, "next_action": { "type": -1 } } ], "child_num": 6, "err_code": 0 } } }

fffonion commented 3 years ago

@tideline3d Thanks! With that I guess we can at least have the children status. I'm wondering if HS300 support per socket energy meter function. Are you able to view that using the Kasa app?

tideline3d commented 3 years ago

Yes, I can see individual power on the app. I use this plugin for OctoPrint that will pull energy from an individual plug for its statistics. But would much rather have all that flowing into Prometheus.

Here's the plugin code that does it, forgive me for not really knowing python well enough to point you any further :)

https://github.com/jneilliii/OctoPrint-TPLinkSmartplug/blob/master/octoprint_tplinksmartplug/__init__.py

fffonion commented 3 years ago

@tideline3d nvm, I find a similar one https://github.com/python-kasa/python-kasa/blob/master/kasa/smartstrip.py which appears to be more readable. I will draft a PR now.

fffonion commented 3 years ago

@tideline3d wanna try https://github.com/fffonion/tplink-plug-exporter/pull/9 😄 ? I attached a linux-amd64 build below as well: tplink-plug-exporter.gz

tideline3d commented 3 years ago

@fffonion I threw it on DockerHub so I could test it easier (for me at least). It seems to work, but a few oddities. Here's the output that seems to be working!

Screen Shot 2020-11-30 at 2 46 30 PM

Potential Issues I found:

1 - Duplicate plug names throw an error. I assume since they were just a string, they shouldn't be used for anything? Here I had two of my plugs labeled 'Empty'. Changing one to 'Empty2' got around this.

image

2 - I'm getting spotty results from the scrape. It seems to scrape fine, but after a few tries I'll get empty results. My scrape interval is ambitious at 5s. Though I can cause this reliably by just refreshing a browser. Is that a 'feature' of the Kasa device just not liking that much traffic?

Screen Shot 2020-11-30 at 3 10 54 PM

3 - The 'TPLink _PowerStrip' item appears to be the aggregate, but that causes totals in your default dashboard to get weird. Not sure if this will cause problems later. I'd personally be fine ignoring the total from the device and only presenting the 6 plugs, ignoring the parent. But not sure if that works for all?

Screen Shot 2020-11-30 at 3 16 49 PM
tideline3d commented 3 years ago

Occasionally I'll get a partial response when hitting the scrape endpoint as well.

Screen Shot 2020-11-30 at 3 29 06 PM

Instead of

image

fffonion commented 3 years ago

@tideline3d Woohoo it works! Thanks for taking time testing this!

  1. Yeah that's an issue we need to address. I'll probably add the child_id as a label to child plugs. Since for HS1xx the IP address is also attached as label automatically, so they are not affected before.
  2. It appears from my test that the TCP server on those plugs are single-threaded, if the previous scrape didn't end, then your next scrape will hang. It's a good idea to consult "Targets" page of your prometheus, and see how long does prometheus finish a scrape for one target, then use a scrape interval longer than the single scrape. But it could also be some bug with exporter, I'll log out the err in next commit.
  3. That's a good point, I was thinking this yesterday as well. ~It probably makes sense to use a different metrics for the total if it's a powerstrip.~ Since we now have child_id label on child plugs, we can just exclude them children and only use the powerstrip for computing total. I will update the dashboard.
fffonion commented 3 years ago

I have made some improvement to the exporter. Now every metrics has an id label, when calculating total, we should use sum(kasa_power_load{id=~".{40}") to exclude child plugs.

tideline3d commented 3 years ago

I'll try this later today, but I'm not sure using the strip's power usage is actually accurate? Most of the time, mine doesn't add up to the sum of its children. However I'm running 3D printers here which fluctuate power ALOT in turning on/off the heaters, so its possible that its just an artifact of the really quick power spikes they generate?

An example from your dashboard. Note the orange is never the sum of the other items, its only as large as one of them at a given time? This is all from the original PR, not the update. Curious if that update approach is actually accurate given the data here? Another option might be to just ignore the parent and treat the strip as 6 HS110 plugs?

Screen Shot 2020-12-01 at 10 16 54 AM

And another example from power total dashboard element. I would expect the purple line here to be a sum of all the other ones (and then as it stands, green being an aggregation of both of those)

Screen Shot 2020-12-01 at 10 19 46 AM
fffonion commented 3 years ago

Hmmmm that's interesting, I'd double check does the power number adds up in the Kasa app? If it does, then there must be either the strip or the child plugs has wrong number: could you share on which one doesn't match to that exports to prometheus?

fffonion commented 3 years ago

@tideline3d I merged the PR and it closes this issue. Feel free to reopen or create a new issue if the feature is not working.