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
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.
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
Btw, I noticed that I can turn the switch on and off with -d ‘{“state”: “on”}’ and -d ‘{“state”: “off”}’ . For example:
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 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 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.
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.