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

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

Updated URLs:


Hi Jpsy,

I’ve tried your method and it’s working really good. I am completely new to node-red and with your help, it’s working good.

I have one question: this link (http://homeassistant:1880/endpoint/switch-light) opens a new page with {} with this symbol. I just want to know if it’s possible to close this page automatically ?

Thanks again for your help.

You would not typically access that HTTP endpoint from a browser. The browser link is only meant for easy testing of the endpoint as browsers do in fact issue HTTP GET requests when you access a URL. The normal usage would be from some device (like my doorbell) that can issue HTTP GET requests to trigger functions in other devices. Devices like my doorbell do not display the HTTP response.

This being said, it is in fact possible to return a payload with the HTTP response node. This payload can contain a web page with content including Javascript code that will be displayed/executed by the browser. But nowadays it is not possible anymore to immediately close a browser window through Javascript if the window was not opened through Javascript in the first place. But you could still display a nice message in the browser.

1 Like

Thanks for your reply. Actually why I need to access the link from a browser is simple. I just want to give access to only one entity to my brother. He is not living with me. Sometimes he needs this access. Instead of creating an account and installing the app, the Url link is a simple solution.

I will add the link to his home screen (like a button). I don"t know if it’s possible to add an icon to the webpage. Now it looks like this:

Screenshot_20210306-213937

Is that possible ?

Thanks

This is going far beyond HA. You will have to create a /config/www folder, place a favicon there, access it through /local and reference it in the HTML that you return. With these keywords you should be able to ask Google for the details. But this is not an HTML forum.

Thanks. I am going to try it.

Apart from that, your solution works really well. Thanks

It is a pity that the REST API doesn’t accept the token via a GET parameter… There are many devices that only accept to send GET methods…

Thanks for the workarround to use NodeRed!

1 Like

How are you get rid of the problem with the authorisation?
I’ve tried all types of nodes “http in” and the “webhook” with the companion integration. (Also the HA webhook via automations)
But every time the system wants an authorisation.
In the command line I get an “unauthorised” if I call http://homeassistant:1880/endpoint/long
In the browser a login mask appears and prompts me to enter a user + password.
(and after entering the nodered credentials I get an “unauthorised”)

Sadly my Shelly button 1 can’t handle URLs with login credentials.

Sounds like you have set a username and password in your http_node option in the Node RED config of HA. Remove that and your endpoints will work without authentication. Read the docs: addon-node-red/DOCS.md at 3cd3c599928a50dac5cc91d41dee36b97fc3f37a · hassio-addons/addon-node-red · GitHub

Hi,

Thank you for all these post it helps me alot.
Another question if it is possible to get entity (light) status returned in http(s) from node red?

For example if i do http://homeassistant/endpoint/light-state will return on/off or 1/2