Can't add more than 3 switches without some vanishing

I have a switch.yaml with the following content. But the third switch won’t show “TendaP3”. I can delete “TendaP2” and P3 will show.

It just seems that if I add more than 3 switches the last ones are ignored. I have tried reformatting in various ways and nothing seems to work. I’m guessing it’s a really simple mistake but I can’t seem to spot it.

  - platform: rest
    name: Tenda
    resource: http://192.168.x.xx:5000/setSta
    state_resource: http://192.168.x.xx:5000/getSta
    body_on: '{"status": 1}'
    body_off: '{"status": 0}'
    is_on_template: "{{ value_json.data.status == 1 }}"
    headers:
      Content-Type: application/json

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

  - platform: rest
    name: TendaP2
    resource: http://192.168.x.xx:5000/setSta
    state_resource: http://192.168.x.xx:5000/getSta
    body_on: '{"sub_device":[{"dev_name":"PLUG2","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"PLUG2","action":0}]}'
    is_on_template: "{{ value_json.data.action == 1 }}"
    headers:
      Content-Type: application/json

  - platform: rest
    name: TendaP3
    resource: http://192.168.x.xx:5000/setSta
    state_resource: http://192.168.x.xx:5000/getSta
    body_on: '{"sub_device":[{"dev_name":"PLUG3","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"PLUG3","action":0}]}'
    is_on_template: "{{ value_json.data.action == 1 }}"
    headers:
      Content-Type: application/json

Are all the IP addresses different? There’s little point hiding your LAN IP addresses, as they only exist on your network.

Isn’t this that the GUI is just displaying three, but the other is there?
What if you write “3” after? Does that work?

What does the developer tools => states say?

No they are all the same, it’s just habit hiding them.

Adding 3 doesn’t show it and the switch isn’t showing in developer tools => states at all.

There seems to be something odd going on as it just showed all 4 (I hadn’t changed the yaml). I then made a backup of my switches.yaml. After that I edited switches.yaml again and it went back to showing only 3 again. I restored the previously working backup and it’s still showing just 3 switches which makes no sense at all.

I have tried restarting HA and just reloading yaml but it’s still not working. I’m wondering if my HA install is messed up somehow. Or if it’s something in the core.entity_registry but I don’t want to break everything or start from scratch really.

Perhaps a tab as opposed to spaces crept in?

I have just gone through and rewritten the code to make sure there are no tabs, same result, HA just sees the first 3 rest switches. I also tried a clean virtualbox install of HA with the same results. It’s very strange, has anyone else got an example using more than 3 rest switches that is known to work? I’d like to compare it to see if I’m doing something different.

Current code -

  - platform: rest
    name: TendaP1
    resource: http://192.168.1.52:5000/setSta
    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: TendaP2
    resource: http://192.168.1.52:5000/setSta
    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: TendaP3
    resource: http://192.168.1.52:5000/setSta
    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: TendaU
    resource: http://192.168.1.52:5000/setSta
    body_on: '{"sub_device":[{"dev_name":"USB","action":1}]}'
    body_off: '"sub_device":[{"dev_name":"USB","action":0}]'
    headers:
      Content-Type: application/json

I am sure that there are people running scores of rest switches.

1 Like

Well I have managed to use command line switches as a workaround. I need to figure out how to check the plug status of individual sockets, but that’s a task for another day.

I’ll paste the code below for anyone else using a Tenda SP15 power strip.

If anyone can figure out why the rest version doesn’t work please let me know.

  - platform: command_line
    switches:
      tenda_2:
        command_on: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG2","action":1}]}'
        command_off: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG2","action":0}]}'
      tenda_1:
        command_on: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG1","action":1}]}'
        command_off: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG1","action":0}]}'
      tenda_3:
        command_on: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG3","action":1}]}'
        command_off: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG3","action":0}]}'
      tenda_u:
        command_on: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"USB","action":1}]}'
        command_off: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"USB","action":0}]}'
  - 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, wondering if you’ve made any progress with this. I’m running into the same problem. Only 3 switches come up. It’s also not just a gui issue as the other two switches aren’t in the list of entities on the developer-tools/state page.

Show your config. The format for command_line has changed since that last post: see about halfway down this page (can’t link directly due to duplicate anchors in the page):

Hi maud,

After changing to the new command line config it seems to work.

My command_line.yaml now looks like this -

- switch:
    unique_id: tenda_2
    name: Tenda Socket 2
    command_on: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG2","action":1}]}'
    command_off: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG2","action":0}]}'
- switch:
    unique_id: tenda_1
    name: Tenda Socket 1
    command_on: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG1","action":1}]}'
    command_off: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG1","action":0}]}'
- switch:
    unique_id: tenda_3
    name: Tenda Socket 3
    command_on: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG3","action":1}]}'
    command_off: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"PLUG3","action":0}]}'
- switch:
    unique_id: tenda_u
    name: Tenda USB
    command_on: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"USB","action":1}]}'
    command_off: curl http://192.168.1.52:5000/setSta -H "Content-type:application/json" -X POST -d '{"sub_device":[{"dev_name":"USB","action":0}]}'

and switches.yaml -

  - 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
  - platform: rest
    name: tendap1
    resource: http://192.168.1.52:5000/setSta
    state_resource: http://192.168.1.52:5000/getSta
    body_on: '{"sub_device":[{"dev_name":"PLUG1","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"PLUG1","action":0}]}'
    is_on_template: "{{ value_json.data.subdevice.dev_name == PLUG1 and value_json.data.subdevice.status == 1 }}"
    headers:
      Content-Type: application/json``
  - platform: rest
    name: tendap2
    resource: http://192.168.1.52:5000/setSta
    state_resource: http://192.168.1.52:5000/getSta
    body_on: '{"sub_device":[{"dev_name":"PLUG2","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"PLUG2","action":0}]}'
    is_on_template: "{{ value_json.data.subdevice.dev_name == PLUG2 and value_json.data.subdevice.status == 1 }}"
    headers:
      Content-Type: application/json``
  - platform: rest
    name: tendap3
    resource: http://192.168.1.52:5000/setSta
    state_resource: http://192.168.1.52:5000/getSta
    body_on: '{"sub_device":[{"dev_name":"PLUG3","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"PLUG3","action":0}]}'
    is_on_template: "{{ value_json.data.subdevice.dev_name == PLUG3 and value_json.data.subdevice.status == 1 }}"
    headers:
      Content-Type: application/json``
  - platform: rest
    name: tendau
    resource: http://192.168.1.52:5000/setSta
    state_resource: http://192.168.1.52:5000/getSta
    body_on: '{"sub_device":[{"dev_name":"USB","action":1}]}'
    body_off: '{"sub_device":[{"dev_name":"USB","action":0}]}'
    is_on_template: "{{ value_json.data.subdevice.dev_name == USB and value_json.data.subdevice.status == 1 }}"
    headers:
      Content-Type: application/json``
# Example configuration.yaml entry
  - platform: template
    switches:
      tendarestp2:
        unique_id: tendauniquep2
        # value_template: "{{ is_state('switch.tendap2', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.tendap2
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.tendap2
  - platform: template
    switches:
      tendarestp1:
        unique_id: tendauniquep1
        # value_template: "{{ is_state('switch.tendap1', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.tendap1
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.tendap1
  - platform: template
    switches:
      tendarestp3:
        unique_id: tendauniquep3
        # value_template: "{{ is_state('switch.tendap3', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.tendap3
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.tendap3
  - platform: template
    switches:
      tendarestu:
        unique_id: tendauniqueu
        # value_template: "{{ is_state('switch.tendau', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.tendau
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.tendau
  - platform: template
    switches:
      tendarest:
        unique_id: tendaunique
        # value_template: "{{ is_state('switch.tenda', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.tenda
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.tenda

Now they all show up, whether it was switching to the new config format or something random that fixed the issue I’m not sure.

EDIT: I just went back to HA and noticed that they’re all there now. Only changes I can think of is that since adding the switch to HA, I powered it off for a couple of days and I completely shut down HA (previously just restarted via the UI). Perhaps either of those helped? I’ll leave the original post below in case it helps others.

Thanks for the reply Troon and Daniel.

Here is the entry from my configuration.yaml. I haven’t gone the command line route yet as I was trying to keep things ‘simple’.

The below code works for me with the only issue being that only the first 3 entries are loaded. So with the example below, TendaUSB and TendaAll aren’t loaded. Is going the command line route the only option?

HA version are below:
Home Assistant 2022.8.7
Supervisor 2023.06.4
Operating System 8.5
Frontend 20220802.0 - latest

switch:
  - platform: rest
    name: TendaP1
    resource: http://192.168.1.244:5000/setSta
    state_resource: http://192.168.1.244: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
  - platform: rest
    name: TendaP2
    resource: http://192.168.1.244:5000/setSta
    state_resource: http://192.168.1.244:5000/getSta
    is_on_template: '{{ value_json.data.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: TendaP3
    resource: http://192.168.1.244:5000/setSta
    state_resource: http://192.168.1.244:5000/getSta
    is_on_template: '{{ value_json.data.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: TendaUSB
    resource: http://192.168.1.244:5000/setSta
    state_resource: http://192.168.1.244:5000/getSta
    is_on_template: '{{ value_json.data.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
  - platform: rest
    name: TendaAll
    resource: http://192.168.1.244:5000/setSta
    state_resource: http://192.168.1.244:5000/getSta
    is_on_template: '{{ value_json.data.status == 1 }}'
    body_on: '{"status":1}'
    body_off: '{"status":0}'
    headers:
      Content-Type: application/json

Some additional findings:

If you reset the “REST ENTITIES AND NOTIFY SERVICES” via the developer tools, it will revert back to the the first 3 REST entries (I imagine the same will happen if you restart HA). In order to get it to load properly, I found that I needed to reset the powerboard and while it’s reconnecting to wifi, clicked on the “REST ENTITIES AND NOTIFY SERVICES” via the developer tools. This loaded all of the entities.

The issue I’m having now is getting the status to work properly. I think the below code refers to the status of the main power switch which turns all of the switches off/on.

    is_on_template: '{{ value_json.data.status == 1 }}'

So if one switch is on, all of the socket status will be set to on regardless of real world status. So switching the device off via HA results in the switch status turning off for a brief moment before changing back to on.

Using action instead of status (see below) results in the opposite, all devices are seen as off regardless of real world status.

    is_on_template: '{{ value_json.data.action == 1 }}'

I also tried Daniel’s code (see below), but this resulted in the off/on toggle turning into two lightning bolts. One for off, one for on and no status.

    is_on_template: "{{ value_json.data.subdevice.dev_name == PLUG1 and value_json.data.subdevice.status == 1 }}"

Any ideas, should I give up trying to get it to just work via the REST entries?

Answering my own question :slight_smile:

I did some more research on how to parse the json data and have now got per switch status working.

switch1:

is_on_template: '{{ value_json.data.sub_device[0].status == 1 }}'

switch2:

is_on_template: '{{ value_json.data.sub_device[1].status == 1 }}'

switch3:

is_on_template: '{{ value_json.data.sub_device[2].status == 1 }}'

USB:

is_on_template: '{{ value_json.data.sub_device[3].status == 1 }}'

Hope this helps others. Still no progress on only having the first 3 rest entries being loaded if you restart HA or reload the rest entities via the developers page.

NOTE: https://jsonpath.curiousconcept.com/ and JSONPath Syntax | AlertSite Documentation were a great help

1 Like

That’s great work, I was struggling to get the subdevice status working.

I am just using the command line configuration currently as it seems to be less buggy and temperamental than just using REST.

Thanks for the links too, I’ll add those to my bookmarks, I spent many hours searching for good resources about this and just ended up even more confused.

I took the leap and deleted my command line switches and everything now works under REST! I’m still not sure if the problem was something specific with this Tenda power strip, or if there was an update in Home Assistant that fixed things.

Thanks maud for figuring out how to get the subdevice status and pushing forward when I had given up.

No worries Daniel. Couldn’t have done it without having your config as a starting point.

And yeah those two links were definitely key to solving the problem. The expression tester in particular.

Are all 5 of your devices still there after a HA restart / REST entity reload?

Yes all 5 are showing now, even after multiple restarts they seem to be working :slight_smile: