RESTful Switch: available in HA, and shows status (on/off) correctly, but switching does not work

Hi,

Given following URL, which turns a device on (value=true) and off (value=false):
http://IP:PORT/set/abc.def?value=true

This works fine when calling in web browser or executing in the Home Assistant Terminal: curl http://IP:PORT/set/abc.def?value=true

configuration.yaml:

switch:   # https://www.home-assistant.io/integrations/switch.rest/
  - platform: rest
    name: "666"
    resource: http://IP:PORT/set/abc.def
    state_resource: http://IP:PORT/get/abc.def # The resource or endpoint that reports the state if different from resource. Used by is_on_template
    body_on: 'value=true'
    body_off: 'value=false'
    is_on_template: '{{ value_json.val }}'
    headers:
      Content-Type: application/json
    verify_ssl: false

When adding a card, it reflects the status (on / off) correctly (with a delay, of course) if I turn the device on/off e.g. through the web browser. So, this works fine.

However, when clicking on the HA card to turn on, it does not turn the device on, following error in the log:

Logger: homeassistant.components.rest.switch
Source: components/rest/switch.py:172
Integration: RESTful (documentation, issues)
First occurred: 2. November 2021, 19:21:29 (5 occurrences)
Last logged: 18:40:58

Can't turn on http://IP:PORT/set/abc.def. Is resource/endpoint offline?

Calling http://IP:PORT/set/abc.def?value=true in web browser returns
{"id":"abc.def","value":true,"val":true}

Response header per browser (Edge):

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Content-Type: application/json; charset=utf-8
Date: Wed, 03 Nov 2021 17:47:05 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 60

Now I am really stuck and have no clue how to investigate further to get it working. I also tried the following for body_on/body_off in the config, but this did not work either:

    body_on: '{"value": "true"}'
    body_off: '{"value": "false"}'

Any help would be very much appreciated.

Thanks!

Try upping the logging level to DEBUG to see if you get any more information on the issue.

Your curl examples send the value as query parameters which is not the same as sending it in the body of the request. The default method is post so the body you defined should be sent by HA, but you will have to confirm if your device actually supports receiving the value in the body.

Thank you. I’ve tried that, but the debug log did not really provide any further helpful information.

Thank you, this was exactly the issue! The device did not support that, and I had to use a different resource URL for allowing post, which is now working.

So issue resolved.

1 Like