Toggling switch with REST API is not working. Any ideas?

Hi!

I’m trying to toggle switch state with REST API but it seems to fail without any error. Also turn_on or turn_off does not seem to work. My use case is to call HASS REST API to toggle switch, so I can use HASS automation to call another REST API.

Any ideas how to get it working? Been trying to get it working for few hours now :slight_smile:

My curl command line:
curl -v -X POST -H "x-ha-access: mypwd" -H "Content-Type: application/json" -d '{"entity_id":"switch.dummytest"}' http://127.0.0.1:8123/api/services/switch/toggle

When I look at the States window, nothing happens on that command. Also, if I query the state with curl it has not changed.

The switch in configuration.yaml

switch:
  - platform: template
    switches:
      dummytest:
        friendly_name: 'Just dummytest'
        value_template: "{{ is_state_attr('switch.dummytest_toggle', 'sensor_state', 'on') }}"
        turn_on:
          service: switch.toggle
          entity_id: switch.dummytest_toggle
        turn_off:
          service: switch.toggle
          entity_id: switch.dummytest_toggle

I found one issue in Github open: REST API fails to set state · Issue #5772 · home-assistant/core · GitHub
I wonder if it is related to this?

… last line, “switchdummytest_toggle” should read “switch.dummytest_toggle” ? … the . is missing between switch and dummytest_toggle.

Yeah, my post was missing the . It was correctly in the file :slight_smile: Fixed that one. Sorry about that.

your spacing looks wrong, might need another space on - platform: line

Thanks, my post was missing that space too. Pasted my config from bit longer list of switches.

But the original problem still persists :slight_smile:

Try this:

curl -v -X POST -H "x-ha-access: mypwd" -H "Content-Type: application/json" -d '{"entity_id":"switch.dummytest"}' "http://127.0.0.1:8123/api/services/switch/toggle"

What does the old command output?

The output with verbose is the following:

* Connected to 127.0.0.1 (127.0.0.1) port 8123 (#0)
> POST /api/services/switch/toggle HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 127.0.0.1:8123
> Accept: */*
> x-ha-access: mypwd
> Content-Type: application/json
> Content-Length: 33
>
* upload completely sent off: 33 out of 33 bytes
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 2
< Date: Thu, 25 May 2017 22:03:48 GMT
* Server Python/3.4 aiohttp/2.0.7 is not blacklisted
< Server: Python/3.4 aiohttp/2.0.7
<
* Connection #0 to host 127.0.0.1 left intact

Without verbose mode it will just return the prompt.

Did you try what I suggested? It should be “” around the url. Atleast that’s how I use CURL.

Also does it work if you turn off password to HA?

you can also try to toggle from the dev page using switch service toggle and

{“entity_id”:“switch.dummytest”}

Tried with quotes and without. No change. Removing password did not help.

Hold on, I’ll grab my ipad and make dummy switch to test your code. I use the rest api often, should work.

Hmmm… It does not seem to do anything from dev-service page with the following info:

Call a service from a component.

Domain: switch

Service: toggle

Service Data (JSON, optional):
{“entity_id”:“switch.dummytest”}

Little more details on the use case:
I’m polling GPIO input pins over I2C with Python script and the idea is to use REST API to inform HASS that button has been pushed. This API call would change the state of a “virtual” switch in HASS, which can be then configured with automation to do more API calls.

This current way of using template switch might not be optimal, but I’ve not found better way yet. Finding better way to implement this might also fix the problem :slight_smile:

Btw, I noticed that I can turn the switch on and off with -d ‘{“state”: “on”}’ and -d ‘{“state”: “off”}’ . For example:

curl -X POST -H “Content-Type: application/json” -d ‘{“state”: “off”}’ "http://127.0.0.1:8123/api/states/switch.dummytest

But it seems using {“state”: “toggle”} will just turn it on and not off, as the new state will be “toggle” instead of off.

There is no state “toggle” which might be the problem. Toggle mean switching the state to “on” and then to “off” I would try to toggle another switch that you have like a light so you know it works.

Yeah, I know. I was trying to make the same point :slight_smile: Ofcourse one could call the API 2x, first to figure out if it is on or off and then to change it. But I was hoping to get the built in toggle to work.

I tested that the switch will turn on if the state is anything but “off”.

Actually, you should try toggling {“entity_id”:“switch.dummytest_toggle”} on the dev page. I would check your logbook to see if the state change and back. (off -> on -> off) or (on => off => on)

If you toggle switch.dummytest I think your switch.dummytest_toggle will be switched 6 times on and off.

I have to debug it more, but I think that the example on switch template page is broken or not implemented right: https://home-assistant.io/components/switch.template/

Using _toggle did not change anything. And this all works on command_line switch for example. Wierd stuff.

I think that this row in my configuration might be working incorrectly in my case, as i don’t have sensor_state variable:

value_template: “{{ is_state_attr(‘switch.dummytest_toggle’, ‘sensor_state’, ‘on’) }}”

I was just about making a dummy switch yesterday when I noted that your switch does not has any attributes states that you describe, the when I looked at your code I got a bit confused. What is your dummy switch trying to do? Can you describe what you are trying to do, and maybe we could help you.

For example, do you need a value template in your switch? Do you need that to check the state of the switch?

Okay, I read more about the template switch and it seems to be sensor + switch in one, thus you don’t need to automate it. Make sense.

If you use this, it does not make a sense to trigger it via REST-api since the switch triggers to the sensor. So a manual trigg will be overwritten when the sensor is updated.

This is if the component works as it should. But it seems that it does not, and that’s why you don’t get update via REST-api or developer console.

I tested this

- platform: template
  switches:
    test:
       value_template: "{{ is_state('light.kitchen_lamp', 'on') }}"
    turn_on:
      service: switch.turn_on
      entity_id: switch.coffee_switch
    turn_off:
      service: switch.turn_off
      entity_id: switch.coffee_switch

When looking in the developer console, I can see that the ‘switch.test’ is set to on when I turn on the kitchen light and off when I turn off the kitchen light, so the sensor part does work. But it fails to turn on/off my coffee switch.

Then I tried the same thing as an automation

- alias: 'Testing for HA-forum'
  trigger:
    platform: state
    entity_id: light.kitchen_lamp
    from: 'off'
    to: 'on'
  action:
     service: homeassistant.turn_on
     entity_id: switch.coffee_switch

- alias: 'Testing for HA-forum 2'
  trigger:
    platform: state
    entity_id: light.kitchen_lamp
    from: 'on'
    to: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: switch.coffee_switch

This does what the template switch fails to do. My coffee switch is set to on/off.
So it does indeed seem to be a problem with the template switch. Does it work for anybody else?

The template switch in theory is good, as it eliminates the amount of code.