Help with REST switch to turn led ON/OFF using ESP8266

I made some experiments with HASS and an endopoin based on ESP8266.
I created a REST switch and I set:
switch:
- platform: rest
resource: http://IP_ADDRESS/led

I have no problem sendind POST parameters to the ESP8266 which turn on LED correctly, but I cannon send the proper GET data to HASS and my switch turn off into the web UI.

Can someone provide an example of JSON/template I need to send back to HASS from my microcontroller?

You’re not using a standardized component so there’s no way anyone could tell you what you need to send to modify the light state or what the microcontroller may send back to that response or if there’s an endpoint to get the status. Do you have the code somewhere public?

I use this thread as a reference whenever I want to use the HA rest api. I’m sure there is some documentation somewhere, but the example is always enough for me

Yes, it’s not standard device, but I thinki this it’s not so important.
Reading documentation I understand the logic is:

  1. HASS send a POST to the endpoint with custom data
  2. HASS perform GET to the endpoint to get the switch status
    Am I doing any mistake?

Actually I set:
switch:
- platform: rest
name: switch_test
resource: !secret api_simple_switch
body_on: ‘{“active”: true}’
is_on_template: ???

My issues is about ‘is_on_template’ parameter. I read documentation but it’s not so clear.
Actually on the GET I send as response this JSON:
{“is_active” : true/false}
so what is the right template for this?

Home Assistant sends a POST for both on and off. It sends a GET to retrieve state. It uses the same endpoint for all of these operations.

If the JSON is exactly as you posted and that response comes from a GET to the endpoint the template you’d use is simply
'{{value_json.is_active}}'

Thanks!
It works like a charm :smiley:
My error was I used the dot instead of underscore, because in documentation (https://home-assistant.io/components/switch.rest/ it says:

Default is equivalent to ‘{{ value.json == body_on }}’. This means that by default, the state of the switch is on if and only if the response to the GET request matches body_on

I went and checked the parent component docs and there’s nothing that details the variable that’s used to store response data. Maybe that should be added.

you can also go with MQTT instead of REST. There is already some great codes availble for that. Check BRUH’s DIY multisensor for instance.

Is this updated documentation more useful/helpful?

imho yes, it is :slight_smile:
It will be cool to have small examples (or external links to them) in some sections of documentation too, so it’s more easy to understan what you are doing.

@lambtho
That’s very cool!
Thank’s a lot to suggestion :smiley:

@Noisemaker That change will go out whenever they do the next website release. If you can find an example that you’d like to use I can add it.