Restful switch not working for request on Tenda SP15

I have read about how to set up Tenda plugs using Restful switch and have it working. But I have a Tenda power strip with several sockets, these are labelled as subdevices.

The captured command using the Tenda Beli app looks like this -

POST http://192.168.1.52:5000/setSta HTTP/1.1
Content-Type: application/json; charset=UTF-8
Content-Length: 48
Host: 192.168.1.52:5000
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.10.0

{"sub_device":[{"dev_name":"PLUG1","action":1}]}

I have managed to control them using curl

curl http://192.168.x.xxx:5000/setSta -H "Content-type:application/json" -X POST -d @data.json
{
    "sub_device": [
        {
            "dev_name": "PLUG1",
            "action": 1
        }
    ]
}

Which works to turn the socket I want on.

But using the below code adds an entity but doesn’t switch the plug on or off, I’m wondering if it has something to do with escape characters?

- platform: rest
  name: TendaPLUG1
  resource: http://192.168.1.52:5000/setSta
  state_resource: http://192.168.1.52:5000/getSta
  is_on_template: "{{ value_json.data.status == 1 }}"
  body_on: '{"sub_device":[{"dev_name":"PLUG1","action":1}]}'
  body_off: '{"sub_device":[{"dev_name":"PLUG1","action":0}]}'
  headers:
    Content-Type: application/json``

Any help would be greatly appreciated.

With a slight change to is_on_template it now works

- platform: rest
  name: TendaP1
  resource: http://192.168.1.52:5000/setSta
  state_resource: http://192.168.1.52:5000/getSta
  is_on_template: "{{ value_json.data.action == 1 }}"
  body_on: '{"sub_device":[{"dev_name":"PLUG1","action":1}]}'
  body_off: '{"sub_device":[{"dev_name":"PLUG1","action":0}]}'
  headers:
    Content-Type: application/json``
1 Like

Hi,
when i try to control my SP15 with your code I get a bunch of errors.
Invalid config for ‘rest’ from integration ‘sensor’ at configuration.yaml, line 125: ‘state_resource’ is an invalid option for ‘sensor.rest’, check: state_resource
Invalid config for ‘rest’ from integration ‘sensor’ at configuration.yaml, line 126: ‘is_on_template’ is an invalid option for ‘sensor.rest’, check: is_on_template
Invalid config for ‘rest’ from integration ‘sensor’ at configuration.yaml, line 127: ‘body_on’ is an invalid option for ‘sensor.rest’, check: body_on
Invalid config for ‘rest’ from integration ‘sensor’ at configuration.yaml, line 128: ‘body_off’ is an invalid option for ‘sensor.rest’, check: body_off

Am I missing something at integration level?

following my configuration.yaml section:

  • platform: rest
    name: TendaP1
    resource: http://192.168.41.82:5000/setSta
    state_resource: http://192.168.41.82:5000/getSta
    is_on_template: “{{ value_json.data.action == 1 }}”
    body_on: ‘{“sub_device”:[{“dev_name”:“PLUG1”,“action”:1}]}’
    body_off: ‘{“sub_device”:[{“dev_name”:“PLUG1”,“action”:0}]}’
    headers:
    Content-Type: application/json``

Basically I’ve just changed the IP to match my SP15 reserved one.

Thanks for your help,
GLG

I have mine in switch.yaml rather than configuration.yaml, I’m not sure if that makes a difference, also my name is lowercase, again I don’t know if that makes a difference. Home assistant can be very picky so double check the indentation of each line too.

- platform: rest
    name: tenda
    resource: http://192.168.1.52:5000/setSta
    body_on: '{"status":1}'
    body_off: '{"status":0}'
    is_on_template: "{{ value_json.data.status == 1 }}"
    headers:
      Content-Type: application/json

Hi Daniel,
Thank you for your answer. Unfortunately your code doesn’t work for me, I created the switch.yaml file and your code no longer gives me errors but it does nothing :frowning:
By digging around a bit I was able to get the status of the 3 sockets and the USB ports.
this is what I put in the configuration.yaml.

rest:
  - resource: http://192.168.41.82:5000/getSta
    sensor:
    - name: sp15
      value_template: "{{ value_json.data.status }}" 
    - name: bollitore
      value_template: "{{ value_json.data.sub_device[0].status }}"
    - name: griglia
      value_template: "{{ value_json.data.sub_device[1].status }}"
    - name: friggitrice
      value_template: "{{ value_json.data.sub_device[2].status }}"
    - name: usb
      value_template: "{{ value_json.data.sub_device[3].status }}"

At least it’s a step forward.
Now all I have to do is find a way to control them.

Thanks again for your help,
GLG

The following worked for me

switch:
  - platform: rest
    name: Tenda_SP15_Plug1
    unique_id: e8:65:d4:b9:XX:XX_1
    resource: http://192.168.0.XX:5000/setSta
    state_resource: http://192.168.0.XX:5000/getSta
    is_on_template: "{{ value_json.data.sub_device[0].status == 1 }}"
    body_on: '{"sub_device":[{"dev_name":"PLUG1","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"PLUG1","action":0}]}'
    headers:
      Content-Type: application/json
  - platform: rest
    name: Tenda_SP15_Plug2
    unique_id: e8:65:d4:b9:XX:XX_2
    resource: http://192.168.0.XX:5000/setSta
    state_resource: http://192.168.0.XX:5000/getSta
    is_on_template: "{{ value_json.data.sub_device[1].status == 1 }}"
    body_on: '{"sub_device":[{"dev_name":"PLUG2","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"PLUG2","action":0}]}'
    headers:
      Content-Type: application/json
  - platform: rest
    name: Tenda_SP15_Plug3
    unique_id: e8:65:d4:b9:XX:XX_3
    resource: http://192.168.0.XX:5000/setSta
    state_resource: http://192.168.0.XX:5000/getSta
    is_on_template: "{{ value_json.data.sub_device[2].status == 1 }}"
    body_on: '{"sub_device":[{"dev_name":"PLUG3","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"PLUG3","action":0}]}'
    headers:
      Content-Type: application/json
  - platform: rest
    name: Tenda_SP15_USB
    unique_id: e8:65:d4:b9:XX:XX_4
    resource: http://192.168.0.XX:5000/setSta
    state_resource: http://192.168.0.XX:5000/getSta
    is_on_template: "{{ value_json.data.sub_device[3].status == 1 }}"
    body_on: '{"sub_device":[{"dev_name":"USB","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"USB","action":0}]}'
    headers:
      Content-Type: application/json