[RELEASE] TP-Link/Kasa Plug, Switch, and Bulb integration

I have a question. This is probably me being dumb. I do not see a link to any code to install this app? There are 600 comments on this thread...

All I am wanting to do is something simple. We have an air purifier connected to one of the outlets in the media cabinet, which are all on an HS300 (for the power monitoring integration of Sense energy monitor). I just want the plug that has the air purifier to turn off when the TV is turned on and then turn back on 10 seconds later (the air purifier requires a button press to come back on).

I think you're right. The convention is most Dev's will update the first post with updates to their features and a link to their code, which I assume how I accessed this some time ago. I'm pretty sure @djgutheinz is setup in package manager / explorer if you want to install those and then access the drivers / app from there. Otherwise I'm sure @djgutheinz will get back to you soon. Let me know if you need a hand with the package manager option.

I'm not familiar with Package Manager. This is literally the first app I'm trying to install. Is that one of the build in apps?

No it's not, give me a moment and I'll find the links for you

I think I managed to find the code. Is there an advantage to using Package Manager over installing the app and the driver manually? I assume it'll do things like notify me if there is an update?

Was typing the instructions below and just noticed your post. Essentially you are right, it's a more convenient method for the user and developer, with added benefits like reminders when an update available. It's also good for apps with a number of drivers.

Package Manager is built by one of the members of the HE Community, @dman2306. The thread is here and the code and installation instructions are here. This app was developed to make it more convenient to install apps such as the kasa integration.

Once installed I think you may be able to lookup the kasa app and install it, otherwise (or in addition to) lookup package explorer on this forum.


Got it working. There's about a 15 second delay for the air purifier to turn off, but that's because I'm letting Sense turn off the plug using IFTTT instead of polling the HS300 for when the TV turns on. The Sense is already polling it every 2 seconds asking for energy data. Though, I might try polling the TV and just see if it impacts Sense at all. I would prefer to keep everything as local as possible. Just made a simple rule in RM to turn the switch on if it's turned off and delayed the action by 10 seconds.

EDIT: At least I hope it showing pollFreq as 0 means it's not polling the plug.

1 Like

Great to hear you got it working and have already moved on to playing with some rules.

The specifics on using it I might leave for @djgutheinz to comment on. My use is relatively simple, mostly just monitoring my energy usage and used in conjunction with a laundry monitor app to alert me when the washing is done.

Yes, my integrations are in Package Manager as Kasa under control category.

Yes, pollFreq of 0 means it is not polling. (I may change that interface in the next version (using a drop-down) to be clearer.)

Does Sense connect to the HS300? How often does it poll for power and energy or does it let Kasa the cloud do that response?

Recommendation to turn off Air Purifier. I recommend turning the EM function and quick polling ON for only the TV set and off for everything else (unless you need the data for some reason). Then, I would set up a rule using a threshold of 20 watts to turn the air purifier off then with a 10 second delay turn it back on. On my Samsung TV, I get the following log from HS300 power-on command to various power reports. Essentially, in 5 seconds from power on the device it will report over 20watts (your TV may differ - use text logging with quick poll set to see yours.)

[dev:3942]( 04:06:20.911 am [info]( 5.2.1 powerPollResponse: power = 98

[dev:3942]( 04:06:15.664 am [info]( 5.2.1 powerPollResponse: power = 37

[dev:3942]( 04:06:10.453 am [info]( 5.2.1 powerPollResponse: power = 30

[dev:3942]( 04:06:05.533 am [info]( 5.2.1 commandResponse: switch: on

Concern. Your post highlights something I had placed in my don't worry bin. When connected to the cloud, the Kasa devices send a status message to the cloud periodically and when a change occurs. This may account for some of the errors I get on the HS300 since one WiFi interface is servicing six devices. Nothing for you to do. Just a note as we go forward.

PS - sorry for all the posts here, but I have open discussions on issues, improvements, and updates. The integration has evolved from a cloud-based to local wifi using UDP. The next version will use the (Alpha) rawSocket implementation on Hubitat to reduce overhead when quickPolling. Maybe I will Deprecate this thread and have it closed on the next release and start a new thread.

1 Like

I'm pretty sure it sends out a multicast to all the plugs on the network every 2 seconds. I don't know the specifics though of what it sends to the plugs or what the plugs send back. I've made a post on the Sense forum asking for some of that information. It does not get the data from the cloud, that would be way to slow for what Sense is using the data for. It allows Sense to collect ground truth for the device plugged in that they can feed into their ML algorithm.

Reading the Sense technical descriptions, they do not get anything directly from the TP-Link devices. Reading their docs, it looks like they determine the electrical signature of different devices and then you can name the device and use the data. I hope this is correct, or it is yet another communicator with TP-Link.

PS - I talk directly to the TP-Link devices and the HS300 begins getting a lot of error below 5 second refresh window.

In general, yes that's how Sense works. But not everything can be detected easily by the electrical signature, and this includes pretty much anything that runs of DC power, which is where the smart plug integrations come into play. It's not to control the device plugged in, but to capture it's energy usage. For example, my desktop computer and my network rack are each plugged into a HS110.

Here's a post I found from a Sense employee talking about how they get the information from the plugs. I had thought they used multicast, but apparently that's not the case.

TPLink has two ways to fetch the current wattage: TCP, UDP unicast and UDP broadcast. UDP broadcast is the best choice for sense because:

  1. We only care about the wattage when the query is sent, and we don’t backfill from the smart plugs, so a slower protocol like TCP would not help us. Yes, TCP is more reliable, but only because it tries retransmitting unacknowledged packets. By the time those retransmissions have been done, Sense doesn’t care about the data any more.
  2. Broadcast UDP (which is distinct from multicast, which TPLink does not use) is great, because it means that Sense can send a single broadcast wattage query to the whole network and receive unicast responses from each plug, all in a single packet. This means that the whole network’s load is basically as minimal as possible.

However the post didn't mention how often this happens.

EDIT: Further down that post on the Sense forum, a user posted

Sense sends a broadcast message every 2 seconds to port 9999 with a ‘{“emeter”: {“get_realtime”: {}}, “system”: {“get_sysinfo”: {}}}’ command, and then all the HS110’s respond with UDP packets with the data:

1 Like

If this is really true, he command string they are sending is over-qualified. The "“system”: {“get_sysinfo”: {}}" is not required and is actually slowing down the response.

I separate the commands, but the same commands I use for the HS-110. Curious as to what they use for the HS300 (requires a childId context in my code).

Also, can I get a link to the post????

Sure. This is the post I found.

You can't create an account on the forum unless you have a Sense, but if you're really curious and have any questions I can post for you.

1 Like

You can tell them I am working on a rawSocket interface that shows hope and have aleady done cloud to TP-Link, UDP, and TCP in Hubitat and SmartThings. They can EM me for details and can always visit my GitHub site. I have the power interface access data for the HS300 (not the same as the HS110) plus the Kasa Bulbs that have EM Functions (if you are interested in 10W consumers).

Hmmm, so I had the rule that turns the plug on after 10 seconds fail. Well... the rule ran but the plug didn't turn back on. I went to turn the air purifier back on a little later and I had to manually turn the plug back on. I suspect when the command was received by the plug it was probably processing a query from Sense and just dropped the packet or something. I don't know how those plugs (especially the HS300) handle rapid fire network requests.

But this is never a time critical thing. So I think I will modify the rule to

  1. Set a boolean variable, lets say isOff to true
  2. Then if isOff is true, wait 10 seconds, send the command to turn on
  3. Wait 5 seconds, poll device. Change isOff to false if the plug is on. Else, send the command to turn on

I'm fairly newish to Hubitat though. I'm not sure how to program a loop using Rule Machine. I'll play around with it when I get home (or maybe over the weekend when I have more time).

Thank you for your work on this. I have a couple brand new HS110 's and am wanting to use the power usage features. I see the current version is marked as Deprecated pending your update so I will watch the thread and look for your announcement.

Much appreciated.

First, I strongly recommend using Hubitat Package Manager.

Secondly, see the below link for information on the latest release; including the code. The old site is active for backward compatability with legacy users. I moved location about two months ago to support using Hubitat Package Manager as one of the installation techniques.


I'm sooo confused.
I just installed the Hubitat Package Manager and it found the 'Kasa Intergration' app. But now I see that both that and TP-Link are on my Hubitat and all my devices are using the TP-Link version.
I assume this means you renamed the app and I need to change all my devices to use the Kasa device drivers and then delete the TP-Link version.
Is this correct? Is this what your previous post refers to?

And thanks for all your work. I really love my TP-Link devices!!!

Yes, I am new to the hubitat world and I found the HPM earlier and tried to install it. Install failed but I did notify @dman2306 in that thread and am following up with him.

I will install your app manually for now and thanks.

Download the Hubitat app