What's new

uKasa uKasa - A utility script that manages Kasa smart outlets and switches with Asuswrt-Merlin routers

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

JGrana

Very Senior Member
I have put together YetAnotherUtility that runs on Asuswrt-merlin routers and is used to manage and operate TP-Link Kasa Smart Plugs and Switches. It can show the present state of the plug or switch (Off/On) and if supported how much power it is supplying (along with line voltage). There is also a "monitor" mode that will show the line voltage, current, Watts and Watt Hours continually (until Enter is pressed)

I originally did this for some issues I was having with an AiMesh node. At times, the node would go "offline" and I found that a power cycle always brought it back.

NOTE- it does require bash for some string array manipulation (decoding). The Kasa devices both send and receive in a encoded/decoded fashion...

Here is the github Readme

uKasa​


A utility that manages Kasa smart outlets and switches using Asuswrt-Merlin routers

Installation​

uKasa can be installed on Asuswrt-Merlin routers.

Using ssh/shell, execute the following line:

For Asuswrt-Merlin based routers:

/usr/sbin/curl --retry 3 "https://raw.githubusercontent.com/JGrana01/uKasa/master/ukasa" -o "/jffs/scripts/ukasa" && chmod 0755 /jffs/scripts/ukasa && /jffs/scripts/ukasa install

New install method. Downloads a short install script that checks for bash, if not found, offers to install it.

/usr/sbin/curl --retry 3 "https://raw.githubusercontent.com/JGrana01/uKasa/master/ukasa.install" -o "/jffs/scripts/ukasa.install" && chmod 0755 /jffs/scripts/ukasa.install && /jffs/scripts/ukasa.install

NOTE ukasa requires Entware and bash. The new install checks and will optionally install if not there.


$ opkg update
$ opkg install bash


About​

uKasa is a small script/utility that lets you turn Kasa Smart Plugs and some Kasa Smart Switches off and on via the Linux CLI (or called from a script).It also will show lots of information about the Kasa devices it finds on the local network including power consumption information if the plugs supports energy management.

uKasa attempts to support the Asuswrt-Merlin "AddOn" philosophy. It has an install and uninstall function and puts the executable script in /jffs/scripts (with asymbolic link to /opt/bin) and install a "conf" file in /jffs/addons/ukasa.

During install, ukasa can download a small group of example scripts using using ukasa. If selected, these will be downloaded to the ~ukasa/examples directory.The main reason I ported and worked on this script was to be able to power cycle a few of my AiMesh nodes. I had some problems with one node going offline.So, I used ukasa to remotly power cycle the router.

Installation Process​

When ukasa installs, it checks/downloads apps it needs (jq, column, od and nc), sets up the configuration directory with a config file (ukasa.conf).It then attempts to find any Kasa Smart Plugs and Switches on the local network. It does this using nmap and scans for any open ports at 9999.If one is found, it then looks up it's IP address and hostname. The hostname can be one from /etc/hosts, YazDHCP or will be created by using the last 3 MAC address octets.ukasa then probes the device and attempts to get the devices type (plug, switch or unknown), it's featurs and the name (aka alias) you gave the device when you set it up with the Kasa app.ukasa stores this information in a local file.

Once completed, it displays all the plug/switches it found along with the information it recevied or generated.

NOTE nmap is usually pretty good about finding all the Kasa devices. Once in a while it might miss one. If the number of devices it reports found doesn't match with what you believe, run ukasa in discover mode

NOTE2 at this time ukasa doesn't support the new matter based plugs (i.e. KP125M).

Usage​

usage: ukasa <IP or hostname> <command>

command can be...
<on|off> : turn device off or on
<state> : display the present state
<power> : display the power (Voltage and Watts) being used
<monitor> : continually display the power (Voltage and Watts) being used
<info> : show information about a Kasa device

discover : search the network for Kasa devices
refresh : re-create the list of all the devices discovered
devices : show all the devices discovered
help (this screen) : show this screen
install : run the install script
uninstall : remove ukasa files and directories
version : show script version
update : check for and update ukasa

ukasa can accept either the IP address of the Kasa device or the Linux hostname.

When installed, ukasa will scan the local network for devices. If the device is in the table, ukasa can perform commands on it.If you add a new Kasa device or you believe the initial scan didn't find them all, perform the "discovery" command.

This command will scan the local network and add any new devices to the known device list. To see the device list and information about the Kasa device, issue the "ukasa devices" command.

Here is an example of the output from the command:

Device IP Hostname Model Type Features Alias
---------------------------------------------------------------------------------
192.168.1.105 CasitaDeckLights HS200(US) Switch TIM Casita Deck
192.168.1.106 DeckLights HS200(US) Switch TIM Porch Lights
192.168.1.55 KP115US099914 KP115(US) Plug TIM:ENE Test1
192.168.1.90 NetworkSys KP125(US) Plug TIM:ENE Christmas Tree
192.168.1.92 WiFIWAN KP125(US) Plug TIM:ENE WiFi-WAN
192.168.1.93 TMOGateway KP125(US) Plug TIM:ENE TMO Gateway
192.168.1.91 NetworkCloset KP125(US) Plug TIM:ENE Network Closet

If a hostname can't be found, ukasa will create one with the model name and last 3 octects of the devices MAC address. Note Device IP 192.168.1.55 - ukasa created host.

See next post for more info
 
Last edited:
Part 2 - command descriptions and notes


Commands​


These command require the Device IP or Hostname of the Kasa device. I.e. ukasa <hostname or IP> <command>

on/off - "on" and "off" will turn the device on or off

state - display the present "on" or "off" state of the device

192.168.1.91 "Network Closet" ON

info - show a various information about the device

IP Hostname Model Type Features Alias
192.168.1.91 NetworkCloset KP125(US) Plug TIM:ENE Network Closet

Sofware Ver: 1.0.13 Hardwar Ver: 1.0 Model: KP125(US) ("Smart Wi-Fi Plug Mini")
Device ID: 8006F3AD0AF3B86B87EE251210E096AE123456
WiFi rssi: -9 Power state: ON MAC address: "1C:61:B4:11:22:33" On time: 21d:6h:35m
Voltage: 122.2 (V) Current: 989.0 (mA) Power: 109.8 (W) Total Watt Hours: 56659 (Wh)

power - display the present voltage, current, power and Watt Hours of the device

Power Information Plug 192.168.1.91

Voltage: 122.4 (V) Current: 910.0 (mA) Power: 109.0 (W) Total Watt Hours: 56664 (Wh)

NOTE ukasa will report if the device does not support Energy Management (look for ENE in the Features column).

monitor - continuous display (every 5 seconds) of the power being supplied by the device. Press the Enter key to stop.

Voltage: 121.0 (V) Current: 917.0 (mA) Power: 109.7 (W) Total Watt Hours: 56699 (Wh)

These commands do not need a device identifier. They are informational or maintenance.

discover - perform a network scan for Kasa devices. If one or more are found, add to the device listNote this command can take a few minutes for the scan and information retreival from the found device. Also, ignore the RTTVAR warnings, it's nmap making adjustments...If a new device is discovered, it will be added. Devices are NOT removed from the list. To do a compete rescan of the local network, use the command refresh.

refresh - This command is identical to discover except it completely re-creates the internal devices list/file. If you remove plugs or switches, you will need to do a "refresh".

devices - this command will display a table of the presently known Kasa devices it has discoveded

Here are the Kasa devices found:

Device IP Hostname Model Type Features Alias
---------------------------------------------------------------------------------
192.168.1.105 CasitaDeckLights HS200(US) Switch TIM Casita Deck
192.168.1.106 DeckLights HS200(US) Switch TIM Porch Lights
192.168.1.55 KP115US099914 KP115(US) Plug TIM:ENE Test1
192.168.1.90 NetworkSys KP125(US) Plug TIM:ENE Christmas Tree
192.168.1.92 WiFIWAN KP125(US) Plug TIM:ENE WiFi-WAN
192.168.1.93 TMOGateway KP125(US) Plug TIM:ENE TMO Gateway
192.168.1.91 NetworkCloset KP125(US) Plug TIM:ENE Network Closet

install - Install ukasa. Install first checks for apps that it requires - nmap, jq, od and base64. If any are not installed, ukasa installs them via opkg. The install script will then create a directory in /jffs/addons/ukasa and create a small config file (/jffs/addons/ukasa/ukasa.conf).It then performs and nmap scan of the local network to discover Kasa plugs and switches on the network. For every one found, ukasa attempts to find the local hostname of the device. It first looks in /etc/hosts then YazDHCP if it is installed.If it can't determine the hostname, it creates one by using the Type of device and the last 3 octects of it's MAC address (i.e. KP125AF0155). ukasa then adds an entry to the devices file /jffs/addons/ukasa/ukasa.devices with the devices IP address, it's local hostname, model, type of device, features and alias (name of the device assigned by the Kasa iOS or Android app.Install will then offer to install a directory in /jffs/addons/ukasa with one or more example scripts that use ukasa.

uninstall - Uninstall will remove all traces of ukasa - the script itself, the link in /opt/sbin and it's directory in /jffs/addons.

update - update checks for the latest version of ukasa on github and if newer will offer to download it over the existing script. It will also optionally re-install the examples.

version - prints the version of the installed ukasa

help - prints a help screen showing commands and usage

Notes​


At this time, ukasa _does not suport the Matter enabled plugs and switches. These models typically end the model number in an "M"
The discover command sometimes does not get a response from a plug or switch. Typically running it a few times finds them all
 
@JGrana!! You are on a roll with the smart home automation apps!! Look at you go! :) Adding this to the add-ins catalog! :)
 
To install bash on Asuswrt-merlin routers (assuming Entware has been installed via AMTM, perform these commands:


$ opkg update
$ opkg install bash
Why are we still forcing users to manually add things when one codes a script that can automate this?
Look for /opt/bin/opkg, if found run the commands else abort installation. It‘s that simple.
 
Congrats for the addon 👏
Does this mean it will also recognise the Tapo devices, considering that is the same company that makes them?
 
Why are we still forcing users to manually add things when one codes a script that can automate this?
Look for /opt/bin/opkg, if found run the commands else abort installation. It‘s that simple.
It is unfortunate. For the other required apps (jq, nmap and decode64) during install, I look for them and install if not there.

One function required bash. It's the decode function since these device talk in tongues. Bash handles arrays quite well.

As far as I know, shebang must be on the first line. #!/opt/bin/bash

A bit of a catch 22... I could do a separate install script that checks for bash, installs if not there then does the download of the actual app.

Maybe I will do that.
 
Congrats for the addon 👏
Does this mean it will also recognise the Tapo devices, considering that is the same company that makes them?
Thanks!
Give it a try if you want. If the Tapo's listen on port 9999 and encode/decode the same as the Kasa switches - might just work.

Let me know if you do.
 
I just installed it. Just to clarify: this is only for outlets/switches? It doesn't support smart bulbs/sensors/buttons?
In the location I am now I only have smart bulbs/temp. sensors and I get "No outles detected. Please run ukasa discover" - you might want to add the missing "t" in the "outles".
I'll try it tomorrow to my other location where I have 2 tapo smart sockets.
Good luck !
 
I just installed it. Just to clarify: this is only for outlets/switches? It doesn't support smart bulbs/sensors/buttons?
In the location I am now I only have smart bulbs/temp. sensors and I get "No outles detected. Please run ukasa discover" - you might want to add the missing "t" in the "outles".
I'll try it tomorrow to my other location where I have 2 tapo smart sockets.
Good luck !
Correct, it only supports the plugs and switches.
Thanks for the spelling correction. Im sure there are others!
 
What went wrong?

install error.jpg
 
NOTE nmap is usually pretty good about finding all the Kasa devices. Once in a while it might miss one. If the number of devices it reports found doesn't match with what you believe, run ukasa in discover mode

Interesting - nmap is one of the more serious security concerns...

I imagine netcat would be a bit more serious...

@thiggins @RMerlin - all of these "scripts" have the potential to create serious security issues here...
 
Would you elaborate on the perceived threat?

The last thing you want running on your firewall is a tool that is specifically designed to do network recon for addtional vulns and targets for exploits...
 
Thanks.

View attachment 56412

Two of my power strip was found.
Looks like a good start. I have never tested with a power strip.
Interesting that the model is Unknown for 192.168.1.201. I have added some diagnostic code to ukasa.

Also, the good news is that 192.168.1.202 supports energy management.
 
Why are we still forcing users to manually add things when one codes a script that can automate this?
Look for /opt/bin/opkg, if found run the commands else abort installation. It‘s that simple.
I have created an new install script. More a pre-install script. The install directions now point to this script.

It checks for Entware; if not installed tells user to install via AMTM
Then checks for /opt/bin/bash; If not installed, offers to install it.
 
Looks like a good start. I have never tested with a power strip.
Interesting that the model is Unknown for 192.168.1.201. I have added some diagnostic code to ukasa.

Also, the good news is that 192.168.1.202 supports energy management.

This is my power strip. I have 2 of them. From the support page you can find there's different hardware version. Mine is version 1 and 2. In case something went wrong by PM I attach my log files here for your reference. Looks like the attachment need has file extension. So I just add .txt to them.
 

Attachments

  • unknownkasa2.txt
    42 bytes · Views: 14
  • unknownkasa1.txt
    38 bytes · Views: 15

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top