Simple Speaker Selector Switch

Looking for some guidance here for my very simple Speaker Selector setup. I have a Speaker Selector that can turn on/off my in ceiling speaker pairs via a very straight forward REST API. When you query the Speaker Selector with a GET, then you get the following response:

{
  "spkr_00": "off",
  "spkr_01": "off",
  "spkr_02": "off",
  "spkr_03": "off"
}

You can run a POST with any combination of the speakers above, with the same JSON format:

i.e.

{ "spkr_00": "on" }

So, I decided to create REST API “Switches” in HA for each of the active speaker pairs. The configuration.yaml looks like this:

switch speakers:
- platform: rest
  name: "Living Room Speakers"
  resource: http://192.168.10.42/api
  body_on: '{"spkr_00": "on"}'
  body_off: '{"spkr_00": "off"}'
  is_on_template: "{{ value_json.spkr_00 == 'on' }}"
  headers:
    Content-Type: application/json
  verify_ssl: false
- platform: rest
  name: "Kitchen Speakers"
  resource: http://192.168.10.42/api
  body_on: '{"spkr_01": "on"}'
  body_off: '{"spkr_01": "off"}'
  is_on_template: "{{ value_json.spkr_01 == 'on' }}"
  headers:
    Content-Type: application/json
  verify_ssl: false
- platform: rest
  name: "Dining Room Speakers"
  resource: http://192.168.10.42/api
  body_on: '{"spkr_02": "on"}'
  body_off: '{"spkr_02": "off"}'
  is_on_template: "{{ value_json.spkr_02 == 'on' }}"
  headers:
    Content-Type: application/json
  verify_ssl: false

This seems to work just fine. I can setup the toggles in the UI and I can even ask Google Assistant to turn on / off the Dining Room Speakers which is super amazing.

My question is, can this be done in a simpler way with all of the switches in one entity? I can see three separate API calls going to the speaker selector, every 30 seconds - just to get the status. It seems like it should be able to do just one API call, and get all of the switch states. But maybe I’m over-complicating something that already works fine.

Thanks in advance!!
Ben.

1 Like

can we see the hardware like the idea

You can avoid multiple calls by configuring like this RESTful - Home Assistant

Sure! It’s version 3.0 of my speaker select project, which I have evolved to use a custom PCB. However it still needs plenty of refinement, it is working pretty well in my home.

I wanted to put some pictures here, but unfortunately I’m limited because I’m a new user. :frowning:

There is a web interface, physical button front panel, and IR input if you want to use a remote control. On the old version, I had an API that was working with IFTTT (and Google Assistant), but after IFTTT started going subscription, I stopped using that. However, I realized I could just use a similar API for Home Assistant and get the functionality back!

Oh, BTW, I noticed you are a Kiwi and wanted to say “so am I”!! Except I moved to the states as a child and have only been back once in 2006.

Thank you @nickrout!! I’ll have to experiment with this today. It looks like this article is for sensors, will it also work with switches?

For example, does this look about right for my setup?

rest:
  - resource: http://192.168.10.42/api
    switch:
      - name: "Living Room Speakers"
        body_on: '{"spkr_00": "on"}'
        body_off: '{"spkr_00": "off"}'
        is_on_template: "{{ value_json.spkr_00 == 'on' }}"
        headers:
          Content-Type: application/json
        verify_ssl: false
    switch:
      - name: "Kitchen Speakers"
        body_on: '{"spkr_01": "on"}'
        body_off: '{"spkr_01": "off"}'
        is_on_template: "{{ value_json.spkr_01 == 'on' }}"
        headers:
          Content-Type: application/json
        verify_ssl: false
    switch:
      - name: "Dining Room Speakers"
        body_on: '{"spkr_02": "on"}'
        body_off: '{"spkr_02": "off"}'
        is_on_template: "{{ value_json.spkr_02 == 'on' }}"
        headers:
          Content-Type: application/json
        verify_ssl: false

You’re right. And I don’t know if it works for a switch.

Yeah, I tried the above and it doesn’t seem to like it. It gets hung up on the “switch:” line and complains about duplicate definitions.