What does RESTful Switch send?

Hi!

I’m trying to implement rest switch but I’m running into an error that json cannot be parsed. This page: https://home-assistant.io/components/switch.rest/

Does anyone know what the rest switch is actually sending to REST API and how can I get it to send json formatted data?

If rest switch is not working properly, is there anything else that could be used instead of rest switch?

The content of body_on and body_off. How about using a JSON formatted string {"on": "true"}?

command_line switch with curl.

Yeah. That’s what I was trying to do. I have it setup currently like this:

body_on: {“on”: “true”}
body_off: {“off”: “true”}

I’m using Python 3 and sanic to try out the parsing and it keeps saying:

sanic.exceptions.InvalidUsage: Failed when parsing body as json

I dont get the same if I send the same json body with curl. That’s why i was thinking that it might not be sending data as I’d assume. Maybe I’ll just use command line & curl, but there seems to be some bugs (?) with the rest api or then only the documentation is not as understandable as I would need it to be :slight_smile:

Quotes are missing, '{"on": "true"}'

Quotes are missing, ‘{“on”: “true”}’

That is correct, sir! Thanks!

With quotes on place it is sending proper json. Maybe I’ll try and update the instructions on RESTful Switch - Home Assistant to add that as an example how to send proper json. So others won’t need to bang their heads on this :slight_smile:

Edit: hmm. or then I just missed the quotes last night…

One more question related to this, as I was having trouble to get the switch “stay on”.

The example on the page RESTful Switch - Home Assistant says:

switch:
  - platform: rest
    resource: http://IP_ADDRESS/led_endpoint
    body_on: '{"active": "true"}'
    body_off: '{"active": "false"}'
    is_on_template: '{{value_json.is_active}}'

However I found out that the below is actually working:

is_on_template: ‘{{value_json == {“active”: “true”}}}’

Not completely sure, but I’m suspecting that {“active”: “true”} is not the same as {“active”: true}, where the latter would produce true value from the template {{value_json.is_active}}

Can anyone confirm if the example is correct?

1 Like