Toggle switch read/set with rest api

Hi there,

I have a switch (true/false) that I can read or set with rest API. I like to have a toggle that can set this switch on/off or being updated when the switch got changed (without using this toggle).

I am so confused if I should use switch or sensor or binary_sensor or a combination?

switch:
  - platform: rest
    name: water_zone1
    resource: https://192.168.0.200/api/zone_1

The JSON data returned is

value: true or
value: false

So I think i need to add some thing like: value_template: '{{ value_json.value.status }}'

But after that I’m clueless. The code check is accepted and I can restart HAS but there is no trace of an additional switch. I must be skipping a part but I don’t know what.

Any pointers would be very much appreciated!

OK, I figured out that using https was preventing it from doing anything. Now I got an almost working config.

switch:
  - platform: rest
    name: water_zone1
    resource: http://192.168.0.200/api/zone_1
    body_on: '{"true"}'
    body_off: '{"false"}'
    is_on_template: "{{ value_json['relay']['value'] }}"

This reads the value correctly and toggles the switch in HAS when the value is being changed from the device side. But when I try to toggle the switch in HAS, it return to it’s previous status after about 2 second. ie setting from OFF to ON ends in being OFF again.

Is this because the POST and GET command within the REST are done at the same time? Anything I can do to prevent that?

edit: or does the POST simply not work… :thinking:

Thanks

Got it :slight_smile:

switch:
  - platform: rest
    name: water_zone1
    resource: http://192.168.0.200/api/zone_1
    body_on: '{"relay":{"value":true}}'
    body_off: '{"relay":{"value":false}}'
    is_on_template: "{{ value_json['relay']['value'] }}"

I was able to understand the output from the rest switch by linking it to webhook.site by replacing de resource with a webhook.site link. Not sure why but I only managed to do this once. After that my switch just disappeared. Not sure what I did different the first attempt.