REST API turn on/off a command line switch

New to the HASS REST API. Not sure if this is something already supported. I have a command line switch and I want to use HASS REST API to turn it on/off.

When I call the REST API, with {“entity_id”: “switch.door_light”, “state”: “on”}. The API returns the following error. I guess it’s not supported yet?
{“result”: “error”, “message”: “405: Method Not Allowed”}

These won’t work?

Thanks for the quick response! The command line switch works fine on Home Assistant UI. I want to be able to control it from an Android app using HASS REST API. (I need to use command line switch, not a restful switch)

Ok I am with ya now.

Rest API switch on:

http://[YOUR-HASS-IP]:8123/api/services/switch/turn_on

Rest API switch off:

http://[YOUR-HASS-IP]:8123/api/services/switch/turn_off

Rest API switch toggle:

http://[YOUR-HASS-IP]:8123/api/services/switch/toggle

JSON for above:

{"entity_id": "switch.door_light"}

Remember, if you have an api_password set in the configuration.yaml, you will need to either pass that through in the POST header as:

"x-ha-access: YOUR_PASSWORD"

or attach it to the end of the API call like:

http://[YOUR-HASS-IP]:8123/api/services/switch/turn_on?api_password=YOUR_PASSWORD

2 Likes

Thanks a lot @jbardi. It’s working now. looks the JSON body text I sent was not correct, it should only contain the entity_id field.

2 Likes