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: https://rc.home-assistant.io/integrations/emulated_kasa/

5 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!).

6 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!

4 Likes