Emulated Kasa integration Brings SenseLink to HomeAssistant

In Home Assistant 0.115 (beta version released today, final coming in 10 days) there is a new integration called “emulated_kasa” that can be used to expose devices to Sense using the SenseLink library.

Some smart switches or lights will already report power usage to HomeAssistant, well now you can have HA report those to Sense to show undetected devices.

It’s also possible if you have a simple device with a consistent power usage to specify that. Docs are here: Emulated Kasa - Home Assistant

9 Likes

I’ve kept half an ear on the talk of Kasa emulation. I think it is a wonderful idea! I appreciate the team of volunteers who are working on the project. However, I’m really confused about the specifics. Could the final version that you release in 10 days include some introductory explanation for newbies?

I’ll list a few questions to get you started, but since I hardly know what’s going on, I might be asking the wrong thing. Please try to give your best explanation even if it doesn’t answer these specific questions. If the questions have already been answered before, link(s) in your main post would be fine.

  • What hardware is required to run this emulator? Do I need a computer running 24/7 on the local network which can broadcast the results of the software algorithm? If so, please give at least one example of compatible hardware and operating system. Alternatively, does this get installed on a cloud server? If so, what kind of account (paid versus free) do you need and please give at least one example of a compatible cloud server.
  • What software language is used to code the emulator? People who see the extension .yaml probably know right away, but I’m not familiar with it. Yes, I could Google “yaml,” but then I would have to guess which of the 8,870,000 results is best. A sentence or two from the authors would save hours for people who have no idea what you are talking about.
  • What else should I know before I start? Will I need to establish accounts at GitHub and Home Assistant to use the emulator? What kind of user privileges are required? Does running the emulator open my smart home to any security risks that would not have been there without it?

Thanks in advance for your explanation, either here or at the site hosting the emulator!

2 Likes

Hi @jefflayman!

I think between myself and @enbickar, we can provide some details!

As an initial explanation, the TP-Link Sense integration works as follows: your Sense monitor sends out a UDP broadcast requesting power data from all plugs on it’s subnet. All TP-Link plugs (real or emulated) then respond back with their power usage, which Sense uses to display the plug bubbles. So no cloud computing here, it’s all local - and no accounts required. You won’t need accounts with Github or Home Assistant regardless.

In all cases, the emulation works by either calculating the power usage to report back based on another value (e.g. bulb brightness) or by directly reporting (e.g. device that reports it’s own power usage), and then replying back like a “real” TP-Link plug would.

There are two “branches”/approaches as of right now:

SenseLink:

This is a stand-alone tool, that runs on a computer (even a lightweight one, such as a RaspberryPi). You would need this computer running 24/7 to get continuous data reporting to Sense. It’s written in the Python language, so it should work on a wide variety of operating systems and hardware. It can communicate with a Home Assistant instance or devices running MQTT to source the “raw” power value or the attribute (i.e. brightness) data, in order to report to Sense via the TP-Link emulation.

You configure it by writing a YAML (Yet Another Markup Language, .yaml or .yml) text file, and provide it that configuration file when running the Python program. There are some configuration and usage examples on the README page, and I’m happy to help further as needed.

Emulated Kasa:

This is a version of SenseLink that has been adapted to be a Home Assistant integration, meaning it runs as part of Home Assistant. If you already have a Home Assistant (HA) setup, this the the simpler and more efficient way to do it (as long as you don’t have a specific caveat, see below). HA also needs a computer running 24/7 if you want 24/7 reporting, but you can integrate all kinds of smart home devices and it acts as a central hub for smart home information and integration. Full details of setting up HA are probably a little out of scope for a summary reply, but there are some great tutorials out there.

Emulated Kasa can use any kind of sensor or device that’s setup in HA as source data for an emulated plug, without needing to run SenseLink separately. Configuration is similar and also through a YAML file, although it’s part of a combined YAML configuration for the HA instance as a whole.

Caveat: As described above, your computer running HA does need to be on the same subnet as your Sense monitor, in order to receive and reply-to the UDP broadcast request from your monitor. But if you haven’t set up a separate network/VLAN for your IoT devices, you can ignore this!

Summary

So in short - if you already run Home Assistant (and don’t have an IoT VLAN) you probably want to look at Emulated Kasa. If you don’t run Home Assistant already, you might be interested in the stand-alone SenseLink to source data directly from devices (or HA!).

8 Likes

To follow up, this version is specifically for people already running Home Assistant. I run HA on a raspberryPi which is a computer the size of a credit card powered by a USB cable.

I recommend Home Assistant if you have other smart devices in your home, but it’s a bit more effort to setup. If you’re looking for just the emulator alone, you might just go with installing SenseLink by itself.

2 Likes

Yeah I agree with @enbickar, setting up Home Assistant is totally worth it if you’ve got other smart devices. I’ve run it on an Rpi as well, which sips power (vs a normal computer) at like 4-5W typically.

This is great I was looking at using sense link, but this is likely easier for me. Will probably wait for the release since I need to move some devices around on the network for this to work.

When reading the docs the static value for light doesn’t say if it uses the brightness as a factor for emulation.

Example:

light.dining_room:
  name: "Dining Room Lights"
  power: 40.2

if the dining room was at 50% does it then report 20.1? Or would i need a template sensor value for the power?

light.dining_room:
  name: "Dining Room Lights"
  power: "{{ (state_attr('light.dining_room_lamp', 'brightness')|float/255|float)*40.2}}"

You would need to use a template. I’m not sure all devices scale linearly with brightness

Cool. They likely do not scale linearly, but for me its close enough from what I’ve seen.

1 Like

Very nice!

I upgraded to Home Assistant 0.115.0b5 so I could test this out. I have some Iris Smart Plugs that do power monitoring so I added my ERV as an emulated_kasa entity. It immediately showed up in sense and is working perfectly.

1 Like

I noticed an occasional issue after a home assistant restart where emulated_kasa doesn’t start. It always displays the below error in the logs. It looks to me like it’s occurring because HA hasn’t finished loading the device I’m monitoring which in this case is a ZigBee device. In some cases, starting in a recent release of HA, ZigBee completely fails to load which obviously causes this issue as well. Figured I’d drop this here for investigation before I open a issue on github.

2020-09-19 12:58:24 ERROR (MainThread) [homeassistant.setup] Error during setup of component emulated_kasa
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/setup.py", line 213, in _async_setup_component
    result = await task
  File "/usr/src/homeassistant/homeassistant/components/emulated_kasa/__init__.py", line 62, in async_setup
    server = SenseLink(devices)
TypeError: 'module' object is not callable

Edit #1:

I think I have a workaround. Instead of going after the zigbee switch directly, I set up a binary sensor template that gets its value from the Zigbee switch entity and use that binary sensor as the entity in emulated kasa. I left the power_entity pointing to the zigbee created entity which doesn’t appear to cause any issues. I have restarted several times and have yet to get it to fail whereas previously, it failed about 9 out of 10 restarts.

Edit #2:

Side effect…my device appeared as a new HS110 in sense with a different mac address. I checked the code and I’m not seeing how the mac address is determined and I’m not seeing it stored anywhere in home assistant. Nothing in .storage or anywhere else.

Edit #3:

Well…Back to the same error using the binary sensor. Restarted several times.

Edit #4:

This is embarrassing. I had previously modified the home assistant sense code and had it running as a custom component. This custom component was using sense_energy 0.7.2. I’m not even sure how emulated kasa was working sometimes. I deleted the custom sense component and restarted home assistant. sense_energy 0.8.0 was downloaded and emulated kasa now starts properly after every restart.

Everything is working perfectly! Thanks!

Someone mind helping a HA novice out? I’ve only ever used it with native integrations where I haven’t had to really dable in the YAML.

The below is what I put in to start based on the docs and the code sample from @mattlebaugh
all of the lights sources are pulled from the Lutron integration (RadioRA). The first 3 are all dimmable, the 4th is just a switch.

emulated_kasa:
  entities:
    light.living_room_living_room:
      name: "Living Room Lights"
      power: "{{ (state_attr(‘light.living_room_living_room', 'brightness')|float/255|float)*42.0}}"
    light.kitchen_sink_task:
      name: "Kitchen Task"
      power: "{{ (state_attr(‘light.kitchen_sink_task’, 'brightness')|float/255|float)*150.0}}"
    light.living_room_art:
      name: "Art Lights"
      power: "{{ (state_attr(‘light.living_room_art.’, 'brightness')|float/255|float)*150}}"
    light.living_room_piano_corner:
      name: "Reading Corner"
      power: 14

The error I get on reboot is

Logger: homeassistant.components.hassio
Source: components/hassio/__init__.py:326
Integration: Hass.io (documentation, issues)
First occurred: 9:46:24 PM (2 occurrences)
Last logged: 9:53:14 PM

Invalid config for [emulated_kasa]: expected float for dictionary value @ data['emulated_kasa']['entities']['light.kitchen_sink_task']['power']. Got "{{ (state_attr(‘light.kitchen_sink_task’, 'brightness')|float/255|float)*150.0}}" expected float for dictionary value @ data['emulated_kasa']['entities']['light.living_room_art']['power']. Got "{{ (state_attr(‘light.living_room_art.’, 'brightness')|float/255|float)*150}}" expected float for dictionary value @ data['emulated_kasa']['entities']['light.living_room_living_room']['power']. Got "{{ (state_attr(‘light.living_room_living_room', 'brightness')|float/255|float)*42.0}}". (See /config/configuration.yaml, line 29).

Any insight is appreciated.

Edited to add - I have confirmed that the template is putting out the correct result using the dev tools template builder. but for some reason, that float isn’t getting passed to the power variable. YAML does get mad if I leave out the quotes, but why the quotes surrounding the template as that would make me think that its passing the entire template as a string to Power, vs returning the result of the template.

Check your single quotes. Some are the special start and end quotes.

2 Likes

Ah ha! That did it. Thanks Kevin. I caught those on the double quotes, but didn’t see it on the singles. Weird copy paste glitch I guess from Mac to PC over a screen share.

Glad to see you guys continuing to enhance and simplify how this integrates. For those not running this yet, it has a ton of power to make Sense better. Here’s a simple before/after (This is using standalone SenseLink w/Home Assistant, but results should be the same).

Before


After

Even when you can’t get a direct measurement, if you know the load of a smart switch you can “report” that value for the virtual plug, which should help resolve at least the common Always On and Other loads.

As you can easily see, the before picture was my normal view (outside of AC units, dryer and a few other 220 devices) and the after has far more resolution. For realtime readings I’ve been using the EtekCity plugs off Amazon with energy monitoring that I detailed in another thread. If you’re looking to do something similar, here are the links - they have a normal and now a slim form factor - I have both and they work flawlessly here and are only $7-$10 a piece usually
Normal Size → https://amzn.to/2REJ6YY
Compact Size → https://amzn.to/3iIeMbV

Been working great for a few weeks now! Definitely worth it.

Thanks again guys - great work!

5 Likes

Trying to set this up with my Juicebox integration, and it worked briefly then stopped.

Curious what might be going on (it could be Sense).

Config just looks like:

emulated_kasa:
  entities:
    sensor.home_juicebox_watts:
      name: "Home JuiceBox"

It worked until I told Sense was was plugged into the “outlet”, then it just shut off and shows the power as “Other”.

@staze I saw on the other thread you mentioned you have some VLANs set up - you say it was working briefly until IDing it in Sense, so maybe this isn’t the case, but keep in mind unless your HomeAssistant server also sits on your IoT VLAN with your Sense monitor, it won’t see the broadcast requests for data and thus won’t ever reply with data.

That’s partially why I initially made SenseLink a stand-alone program, because personally I have things split up with my HA server on my main VLAN. I run SenseLink on a spare RPi on the IoT VLAN, and punch a small firewall hole for just the HA API connection to that specific RPi.

This looks fantastic! I’ve used sense for about a year now and was frankly, about ready to put it up on Ebay because its done such a poor job of providing me with any useful information.

Some questions:

  1. I have a few extra raspberry pi’s sitting around, so I can easily set one up to run home assistant BUT I also already run hubitat. Would running them both cause trouble? I suspect it would because I presume that Zwave devices can only be linked to 1 hub. I saw hubitat mentioned somewhere in this thread, is a hubitat port coming?
  2. The devices that I would most like to monitor are 220. Possible to monitor them with this?

@arlomiller - generally for devices that talk talk via IP networking I don’t think you’d run into trouble running both, but yeah Zwave might be a little trickier.

SenseLink (the stand-alone version) could almost certainly work with Hubitat via the Maker API, but of course that would need to be written. It looks like someone has written a HomeAssistant integration for Hubitat, which would pull data into HA from your Hubitat hub. Aside from the fact that you’re starting up a HA instance just to shuttle data to Sense, might be the quickest approach?

In regards to your 220V question - it should work fine, as long as the wattage is reported accurately. Sense doesn’t (currently) display the voltage value from TPLink plugs, and appears to only use the wattage value reported back to it.

i might try the HA hubitat integration

are there any plugs/monitors for 220?

Not that I know of offhand, although you could potentially roll your own with ESPSense and an ESP device with some CT clamps.