Making a REST-switch

I have a switch that I can control through REST, but i am struggling a bit

- platform: rest
  resource: http://192.168.1.12/dev/sps/io/lx_ovn_kjellerstue
  body_on: 'On'
  body_off: 'Off'
  #is_on_template: '{{ value_json.is_active }}'
  name: rest_ovn_kjellerstue

this is my setup for the switch, but it doesn’t work

running the following manually in postman works:

192.168.1.12/dev/sps/io/lx_ovn_kjellerstue/On

or

192.168.1.12/dev/sps/io/lx_ovn_kjellerstue/Off

It returns the following info

<?xml version=“1.0” encoding=“utf-8”?> <LL control=“dev/sps/io/lx_ovn_kjellerstue/Off” value=“0” Code=“200”/>

It might seem like the “Rest switch” in HA is made for taking input data in the body, instead of in the address? But most of the stuff I have that has a REST api takes commands in the address, including this.

That appears to be returning xml not json.

This might help: Use XML result from rest API

I don’t think the RESTful switch will work. Try using the Command line Switch instead. E.g.:

switch:
  - platform: command_line
    switches:
      rest_ovn_kjellerstue:
        command_on: curl -s http://192.168.1.12/dev/sps/io/lx_ovn_kjellerstue/On
        command_off: curl -s http://192.168.1.12/dev/sps/io/lx_ovn_kjellerstue/Off

This will run in “optimistic” mode, which means it will remember the last command (on or off) and that will be its state. It will not know if the actual switch changes. But, if you can send a command that will return the state without changing it, then you can add command_state, and possibly value_template, so that the HA switch updates according to the actual state of the switch.

1 Like

Hi Phil,

Thanks for all your help over the years, I have a sonoff mini R2 I can control using RESTer (API), I cant get it to unlock OTA so I am stuck in this limbo, the command below works fine for "on "and “off”:

the state can be received via another commend in RESTer as seen below:

How do I implement this as a switch in HA able to also get states from the info?

Thanks in advance and apologies if this has been answered elsewhere.

Regards
Caswell

This does not work:

  - platform: command_line
    switches:
      minikamatepe2:
        friendly_name: "minikamatepe2"
        command_on: "/usr/bin/curl -s http://192.168.68.181:8081/zeroconf/switch -d '{\"deviceid\":\"\",\"data\":{\"switch\":\"on\"}}' -H 'Content-Type: application/json; charset=utf-8' -H 'Server: openresty'"
        command_off: "/usr/bin/curl -s http://192.168.68.181:8081/api/zeroconf/switch -d '{\"deviceid\":\"\",\"data\":{\"switch\":\"off\"}}' -H 'Content-Type: application/json; charset=utf-8' -H 'Server: openresty'"
        command_state: "/usr/bin/curl -s http://192.168.68.181:8081/zeroconf/info -d {\"deviceid\":\"\",\"data\":{}} -H 'Content-Type: application/json; charset=utf-8' -H 'Server: openresty'"
        value_template: '{{value_json.switch == "on" }}'

some times the relay does come “on” and click, but its random, also the switch on HA UI goes back to "off " position after its switched “ön”

image

It now works, heres the full config:

  - platform: command_line
    switches:
      minikamatepe2:
        friendly_name: "minikamatepe2"
        command_on: "/usr/bin/curl -X POST http://192.168.68.181:8081/zeroconf/switch -d '{\"deviceid\":\"\",\"data\":{\"switch\":\"on\"}}' -H 'Content-Type: application/json; charset=utf-8' -H 'Server: openresty'"
        command_off: "/usr/bin/curl -X POST http://192.168.68.181:8081/zeroconf/switch -d '{\"deviceid\":\"\",\"data\":{\"switch\":\"off\"}}' -H 'Content-Type: application/json; charset=utf-8' -H 'Server: openresty'"
        command_state: "/usr/bin/curl -X POST http://192.168.68.181:8081/zeroconf/info -d '{\"deviceid\":\"\",\"data\":{}}' -H 'Content-Type: application/json; charset=utf-8' -H 'Server: openresty'"
        value_template: '{{value_json["data"]["switch"] == "on"}}'

This should also work:

        value_template: '{{value_json.data.switch == "on"}}'

will try it again, it did not work before, but maybe it was in combination with other errors.