Tenda Beli SP9

Hello!

I have Tenda Beli SP9 Power plug and can control it via rest api (getSta, setSta) getting some data from sensors (getDetail e.g).
This device has a power consumption sensor and I need to get data from it too, but I don’t know which http request to use.

Maybe anyone know it?
http://X.X.X.X/get???

I unpacked the Tenda software and I see only this:

public interface SmartSocketService {
    @POST("/getWifi")
    k<JsonObject> getAroundWifi();

    @GET("/getDetail")
    k<JsonObject> getDetail();

    @POST("/getLightStatus")
    k<JsonObject> getLightStatus();

    @POST("/getLed")
    k<JsonObject> getLocalLed();

    @POST("/getSta")
    k<JsonObject> getSocketSwitch();

    @POST("/guideDone")
    k<JsonObject> setGuideDone(@Body GuideDoneData guideDoneData);

    @POST("/setLightStatus")
    k<JsonObject> setLightStatus(@Body JsonObject jsonObject);

    @POST("/setLed")
    k<JsonObject> setLocalLed(@Body JsonObject jsonObject);

    @POST("/setNick")
    k<JsonObject> setNick(@Body JsonObject jsonObject);

    @POST("/setSta")
    k<JsonObject> setSocketSwitch(@Body JsonObject jsonObject);
}

The Power consumption is in another place:

public interface WebService {
   ...
    @POST("app/EnergyGet")
    k<JsonObject> getDeviceEnergy(@Body DevEnergyBody devEnergyBody); 
   ...

As I see, the data isn’t available via tcp port 5000. Don’t know how to get access to “WebService” interface. Is it possible at all?

Did you ever figure out how to get the energy usage on API?

I am also looking for a solution to get data from the plug. I tried to decompile the official application and look for a way to get this kind of data. If there is a possibility to get data using GET or POST request, then it is not used in the official application. Instead, the power data are collected by the provisioning server where the plug sends data in regular periods. How to setup the plug to own provisioning server is very nicely described in the article.

There is no need to use hostname, you can provide ip address of your rendezvous server.
The plug connects to the ntp server and synchronizes the time. At this point I have no idea what would happen if the plug did not have access to the internet.
If does have acces, every hour on the hour (eg 8:00, 9:00 ), plug sends the overall consumption data to the provisioning server. Actual power draw and partial consumption (past hour) is provided on request. Actual power consumption is provided on every single request, partial consuption data are availible each circa 5 minutes and more frequent request are rejected.

response for actual power draw
{"resp_code":0,"data":{"power":84.481}}'
response for partial consumption
{"energy":["1666809547,2370,0.060,2315,2","1666809547,2370,0.060,2315,3"]}'
response for partial consumption on the request in the first minutes on the hour (eq 8:05, 9:05) - also contains data from the previous hour
{"energy":["1666735585,362,0.000,0,2","1666735585,4048,0.010,1109,3"]}'
overall consumption data - no request
{"ver":3,"energy":["1666810806,203902,1.920,129354,0"]}

I haven’t completely decoded these energy data frames yet because they are slightly different from time to time but i believe:

1666809547 epoch time
2370 time in seconds for which energy consuption is counted
0.060 energy consumed in kwh
2315 time in seconds for which power was actualy drowned
2 I don’t understand the meaning yet

I extended the code from lui_gough by adding reading and requesting of energy data (POC only), to capturing data for afterward “decoding”. Download from here

I am familiarizing myself with the development of integration for Home Assistant which is my main aim. I’ve just started work on the integration, so far nothing to share.

2 Likes

is there any news? i am a newbie and i would like to use this smart plug (tenda beli sp9 ) with home assistant and i found this post. I would be very happy if there were steps forward to integrate it into home assistant

It easily integrates in Home Assistant (tutn off, turn on, show status etc).
This topic is about that nobody knows how to extract power consumption data from it.

can you provide some link?

I finally got around to write the Tenda Beli plug integration. The integration implements the procedure described by this article (using “rendezvous” and “provisioning” servers running locally by Home Assistant).

One switch (tbp_switch_xxxx) and two sensor entities (tbp_power_xxxx and tbp_energy_xxxx) are created when plug is succesfully recognized by connecting to provisioning server. (xxxx are last 4 digits of the serial number)

To “install” the integration you need to

  1. download this code
  2. copy the folder named tendabeli to the homeassistant/custom_components folder
  3. add the line bellow in configuration.yaml.
tendabeli:
  1. Then reboot the home assistant.

Before use, the plug must be configured:

  1. reset the plug by long pressing the on/off button until the orange LED lights up
  2. a plug Wi-Fi AP like Tenda_Smart_Plug_XXXX should appear, connect to it
  3. make a text file and provide credentials for Wi-Fi connection and ip address (if static) or the hostname of the device on which home assistant is running, hostname can´t be used for IPv6:
{"account":"1","ssid":"your_ap_ssid","key":"your_ap_passwd","server":"ipaddress_or_hostname_of_your_hass","location":"Europe/Prague","time_zone":0}
  1. using command line (Windows) send:
curl -d @"link_to_txt_file" http://192.168.25.1:5000/guideDone

or using powershell cmd

curl http://192.168.25.1:5000/guideDone -Method Post -Body '{"account":"1","ssid":"your_ap_ssid","key":"your_ap_passwd","server":"ipaddress_or_hostname_of_your_hass","location":"Europe/Prague","time_zone":0}'

This integration is not fully finished and has been tested with SP9 plugs only . There are easier solutions for SP3, but this should work as well.

this is what ha tells me after following your guide.

2023-07-20 23:16:43.199 WARNING (MainThread) [py.warnings] /config/custom_components/tendabeli/tenda.py:166: RuntimeWarning: coroutine 'Server.serve_forever' was never awaited
  server.serve_forever()

2023-07-20 23:16:44.291 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform tendabeli.switch: Platform not found (cannot import name 'async_get_registry' from 'homeassistant.helpers.entity_registry' (/usr/src/homeassistant/homeassistant/helpers/entity_registry.py)).
2023-07-20 23:16:44.846 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform tendabeli.sensor: Platform not found (cannot import name 'async_get_registry' from 'homeassistant.helpers.entity_registry' (/usr/src/homeassistant/homeassistant/helpers/entity_registry.py)). 

this come out from powershell when i run your command with the path of my txt file instead of @“link_to_txt_file” and the orage led still flashing.

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 192.168.25.1:5000...
* Connected to 192.168.25.1 (192.168.25.1) port 5000 (#0)
> POST /guideDone HTTP/1.1
> Host: 192.168.25.1:5000
> User-Agent: curl/8.0.1
> Accept: */*
> Content-Length: 107
> Content-Type: application/x-www-form-urlencoded

Hi,

I had the default logger triggering only on errors (no warning of deprecated function) and I’m using an older version of HA 2023.4.0. After core 2023.5.0 function async_get_registry is replaced with async_get. Source code updated on Github (link above). Mine log is now clear.

Yeah, you can use just cmd like(but this didn’t cause this problem):

curl -d @“C:\Users\bfu\Desktop\provi.txt” http://192.168.25.1:5000/guideDone

Try to modify your text file to:

{“account”:“1”,“key”:“your_wifi_password”,“location”:“Europe/Prague”,“server”:“ipaddress_or_hostname_of_hass ”,“ssid”:“ssid_of_your_wifi_ap”,“time_zone”:0}

Mine plugs works fine also with minimum configuration, but I honestly don’t know if the plug erase all data during a reset or just rewrite given parameteres.

{“ssid”:“apname”,“key”:“appass”,“server”:“10.10.10.100”}

I found that hostname not work for IPv6, IPv6 must be disabled, or static IPv4 address used, however, this will only cause the plug to not be recognized/ it will not connect to hass.

It seems that you can connect to plug but somehow it wount connect to WiFi.

EDIT:
I am using command line, but with powershell i used this:

curl http://192.168.25.1:5000/guideDone -Method Post -Body '{"ssid":"apname","key":"appass","server":"10.10.10.100"}'

when cannot connect to wifi due to config error or whatever plug will reply with

StatusCode        : 200
StatusDescription : OK
Content           : {123, 34, 114, 101...}
RawContent        : HTTP/1.1 200 OK

                    {"resp_code":2}
Headers           : {}
RawContentLength  : 15

If config is OK, it destroy plug AP without response.

my logs now look like this:

Logger: homeassistant.setup
Source: setup.py:379
First occurred: 09:35:51 (2 occurrences)
Last logged: 09:35:51

Unable to prepare setup for platform tendabeli.switch: Platform not found (cannot import name 'async_get_registry' from 'homeassistant.helpers.entity_registry' (/usr/src/homeassistant/homeassistant/helpers/entity_registry.py)).
Unable to prepare setup for platform tendabeli.sensor: Platform not found (cannot import name 'async_get_registry' from 'homeassistant.helpers.entity_registry' (/usr/src/homeassistant/homeassistant/helpers/entity_registry.py)).

I think the code hasn’t been updated

EDIT: i manually updated the senser.py and switch.py files and now there is no more error in hass.
the powershell problem remains. now using: curl http://192.168.25.1:5000/guideDone -Method Post -Body '{"ssid":"MYapname","key":"MYappass","server":"MYserverstaticIP"}' the powershell returns me:

Warning: built-in manual was disabled at build-time!

EDIT2: even in cmd:

C:\Users\nicol>curl -d @"C:\Users\nicol\Desktop\wifi_credential.txt" http://192.168.25.1:5000/guideDone -vvv
*   Trying 192.168.25.1:5000...
* Connected to 192.168.25.1 (192.168.25.1) port 5000 (#0)
> POST /guideDone HTTP/1.1
> Host: 192.168.25.1:5000
> User-Agent: curl/8.0.1
> Accept: */*
> Content-Length: 121
> Content-Type: application/x-www-form-urlencoded
>

and orange led still flashing…

#################################################################################

lastEDIT: I used the CMD with your suggestions, I changed the quotes because + they were in the wrong character “ ” must be replaced with " " so:

{"account":"1","key":"your_wifi_password","location":"Europe/Prague","server":"ipaddress_or_hostname_of_hass ","ssid":"ssid_of_your_wifi_ap","time_zone":0}

now everything works

Thank you nictronik99, you are right, I used blockquote wrap instead of code and it mess up the characters. You can also update tendy.py file which had an issue too - but it works also with the old version.

Please be noted that integration is not finished, there are some minor issues (as far as I know), eg. when plug reconnect due to timeout or whatever, overall consumption drop to 0 (due to sensor initialization), and when fetch data from the plug, bounce back to the the original value. So if you are using statistical graph and logging the difference from hour to hour, it will mess up the increments. I must dedicate more time to studying the HASS documentation, then i will fix this problem.

No problems, we are here to dev Together

** Update**

I have followed the steps above from jddev & nictronik99… I can see the integration, but if I try and add I get the error:

This device cannot be added from the UI - You can add this device by adding it to your ‘configuration.yaml’. See the documentation for more information.

The SP9 is connected to the wifi of the HASS, and I ran the code below in terminal as root, but still can’t see the SP9 on the HASS, and the orange light is still flashing!

curl http://192.168.25.1:5000/guideDone -Method Post -Body {“account”:“1”,“key”:“your_wifi_password”,“location”:“Europe/Prague”,“server”:"ipaddress_or_hostname_of_hass ",“ssid”:“ssid_of_your_wifi_ap”,“time_zone”:0}

Please can you let me know what I’m doing wrong?

Hi All,

I’m new to Home Assistant and I’m trying to set up my Tenda Beli SP9 smart plugs and my Govee Thermo-Hygrometer Sensors are there any solutions to get these configured and working?

Any help would be really appreciated

Cheers, Dean

I realised that I wasn’t running the command in PowerShell. I have now got that setup, but when I run the command I get the error below:

ParserError:
Line |
1 | … 192.168.25.1:5000/guideDone -Method Post -Body {“account”:“1”,“key”:“ …
| ~~~~
| Unexpected token ‘:“1”’ in expression or statement.

I ended up using the code below, which worked with no errors. My SP9’s are set up and I have the energy monitoring :slight_smile:

The only negative thing is if I set up areas and friendly names, they get lost if HA is restarted.

curl -X POST -H “Content-Type: application/json” -d ‘{“account”:“1”,“ssid”:“your_ap_ssid”,“key”:“your_ap_passwd”,“server”:“ipaddress_or_hostname_of_your_hass”,“location”:“Europe/Prague”,“time_zone”:0}’ http://192.168.25.1:5000/guideDone

Hi Alf,

unfortunately, this is a “feature” that was accidentally programmed (due to limited knowledge of hass). At this point, I can already write a better integration using Config Flow, which allows you to set up integrations through the user interface and allows you to manage devices. Unfortunately, I don’t have much time to rewrite it now. However, I would focus on it in the future. In the 3/4 years I’ve been using this version, I’ve noticed a few flaws too.
Except for user convenience, however, it can also be used in the automations if a “programmed” name is used - unfortunately, I don’t know your exact use case.

Hi Jddev,

I have 3 of the SP9’s set-up and I’m using them in automations by way of the entity id.

Not sure what you mean by a “programmed” name… Is this something I can update?

Also, if I restart HA the entity id sometimes gets updated from “tbp_switch_1234” to “tbp_switch_1234_2”. To keep my automations working I have just been removing the “_2”, so far this hasn’t caused any problems.

It would be great if you could rewrite the integration, but totally understand that these things take time. Tbh I wouldn’t have a clue where to start, and I really appreciate the work you have done so far, as I was about to look for alternative plugs before I came across your post.