Home Assistant API - Turn On/Off Light via simple (HTTP) command from other device

All,

Would there be documentation/information available on how to turn on/off a device via a simple command from an external device? (preferably HTTP)

Thanks!

https://developers.home-assistant.io/docs/en/external_api_rest.html

Or maybe even:

https://developers.home-assistant.io/docs/en/external_api_websocket.html

(I don’t know much about the latter.)

yeah…I found that documentation too… but still don’t get it what HTTP call I would need to make to trigger the light on/off

E.g., to use the REST API to turn a light on or off you’d use it to make a service call. See this section on how to do that. It even includes an example of how to turn a light on.

@DieterClaeys - what device are you referring to?

If it’s an Android device, I can recommend HTTP Shortcuts:

1 Like

I’m using commandfusion on iPad. Now I’m controlling the lights with a VeraEdge, but I want to get rid of that device as I am not so happy with it.

The problem is not so much the device or knowing if its capable of doing it.
The problem is that I don’t have a full understanding what command I need to send.
Would you have an example?

https://xxx.duckdns.org/api/services/light/toggle?api_password=yyyyyy

{
“entity_id”:“light.lamp”
}

2 Likes

It gets a little more tricky when you need to use the new authorization/token.

Haven’t figured that out myself yet, but here’s a curl command line example - maybe this helps:

is there something i need to add between the password and the {“entity_id”:…}

If I type:
https://xxx.duckdns.org:8123/api/services/switch/turn_on?api_password=xxxxxx{“entity_id”:“switch.kitchen_lamp_16”}
in one line, it responds with:
405: Method Not Allowed

no, the command is
https://xxx.duckdns.org/api/services/light/toggle?api_password=yyyyyy

the rest has to go in the options somewhere (not in the URL)

1 Like

Problem is that I don’t have options.
This weekend I will install wireshark - see if I can capture what is actually send.

Did you get something?

no I did not

I managed to switch via a http command using a php-script made by @willd, look here: https://community.home-assistant.io/t/create-a-trigger-via-http/19120/7. You have to customize it to your needs.

Is it possible to make a single like HTTP call which includes the entity_id as OP is asking? I’m having trouble with this from my Garmin smart watch.

Hi, no you can’t. if you want to use rest api, you need to use more than http to send request. like JS or Php or some others. if you just want, at the end, a http link, this link should be a script somewhere else : like http://domain.com/myScript.html which contain js that will do a request to the api

If you use the Node-RED integration for HA, there is a simple way to trigger actions with a basic HTTP GET request, without any headers, tokens or payload. I have an IP door bell that can only execute simple HTTP requests, and this works perfectly:

Use the http in node in Node-RED to expose a new HTTP endpoint. This endpoint can be triggered by a simple HTTP GET, i.e. by calling the URL from a web browser. Don’t forget to combine the node with a http response node to send back a success status code 200.

2020-02-15_19-26-57

[{"id":"ef4e4794.3c12a8","type":"http in","z":"9d33a910.bd3758","name":"","url":"switch-light","method":"get","upload":false,"swaggerDoc":"","x":170,"y":100,"wires":[["1d707ade.fdea35","adc182c0.51cc5"]]},{"id":"1d707ade.fdea35","type":"http response","z":"9d33a910.bd3758","name":"","statusCode":"200","headers":{},"x":220,"y":140,"wires":[]},{"id":"adc182c0.51cc5","type":"api-call-service","z":"9d33a910.bd3758","name":"","server":"cb38d2a3.10198","version":1,"debugenabled":false,"service_domain":"light","service":"toggle","entityId":"light.buro","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":420,"y":100,"wires":[[]]},{"id":"cb38d2a3.10198","type":"server","z":"","name":"Home Assistant"}]

This new endpoint would be available under the URL:
http://homeassistant:1880/endpoint/switch-light
(Replace “homeassistant” with your domain or IP address!)
Each request would toggle the HA light entity.

13 Likes

Thanks @Jpsy , it helped me a lot !

@Jpsy @sebcbien, I’m not sure if you guys are still following this forum thread. I’m brand new to Home Assistant. Also, I never used Node-RED.

I want to be able to toggle my Media_Room_Light entity ON/OFF in HA using a single CURL command.

If I create a Node-RED flow like the one shown in the screenshot above, will it be obvious how/where to expose the full URL I need (to be used for my CURL GET request)? Will the URL require/include my HA username/password?

This may sound like a dumb question, but do I have to save/keep this flow in order to be able to use CURL to toggle ON/OFF my Media_Room_Light?

Lastly, could you please post a example (full Curl command) that would toggle a light entity in HA? Thank you so much for your help!

Hi @mkanet,

the url that is exposed will start with the IP address or domain name of your Home Assistant installation (in my example “http://homeassistant”) and the port of your Node Red integration as defined in your integration setup (":1880") then always continues with the path “/endpoint/” followed by the final endpoint configured in your http_in node (i.e. “switch-light”).

You have to save and activate that flow in Node Red using the “deploy” button. And yes, you must keep that flow. Deleting it will delete it. :smiley:

Your username or password will not be required in the call. A simple, empty GET request will suffice. You can issue and test that GET first by simply entering the endpoint URL into your Chrome browser’s address field and hitting return. If that works you can get the corresponding complete curl request from Chrome by opening the Chrome developer tools (F12), go to the network tab, issue the request once more, then right click the request in the network tab and choose to copy as curl.

3 Likes