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

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

Yes, using the get entities node, I use this to provide an easy way to provide access to PHP scripts running on other machines, to get access to temperature data that is inside Home Assistant.

But my flow provides access to the entire list of entities:

[{"id":"bdf8cd6f.ad5c3","type":"tab","label":"PHP State Endpoint","disabled":false,"info":""},{"id":"b05b14fc.2bb988","type":"http in","z":"bdf8cd6f.ad5c3","name":"","url":"/hastate","method":"get","upload":false,"swaggerDoc":"","x":150,"y":60,"wires":[["62eb1352.a9756c"]]},{"id":"4414b44.8b3f44c","type":"ha-get-entities","z":"bdf8cd6f.ad5c3","name":"","server":"49949cfc.3312d4","version":0,"rules":[{"property":"entity_id","logic":"is","value":"[a-z].*\\..*","valueType":"re"}],"output_type":"array","output_empty_results":true,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":150,"y":120,"wires":[["67bd9d28.06ebb4"]]},{"id":"67bd9d28.06ebb4","type":"http response","z":"bdf8cd6f.ad5c3","name":"","statusCode":"","headers":{"content-type":"application/json"},"x":470,"y":200,"wires":[]},{"id":"d1e53794.50d368","type":"debug","z":"bdf8cd6f.ad5c3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":640,"y":80,"wires":[]},{"id":"62eb1352.a9756c","type":"switch","z":"bdf8cd6f.ad5c3","name":"","property":"payload.req","propertyType":"msg","rules":[{"t":"nempty"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":40,"wires":[["5827eb49.e4ed94"],["4414b44.8b3f44c"]]},{"id":"5827eb49.e4ed94","type":"ha-get-entities","z":"bdf8cd6f.ad5c3","name":"","server":"49949cfc.3312d4","version":0,"rules":[{"property":"entity_id","logic":"starts_with","value":"payload.req","valueType":"msg"}],"output_type":"array","output_empty_results":true,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":410,"y":120,"wires":[["67bd9d28.06ebb4"]]},{"id":"49949cfc.3312d4","type":"server","name":"","version":2,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":""}]

This means, that I can access any entity specifically with for example: http://homeassistant:1880/endpoint/hastate?req=light.my_light to get the entire entity item for that one light, or http://homeassistant:1880/endpoint/hastate?req=light.garden to get ALL the lights that start with the word garden. Or ?req=light to get ALL lights. Or ?req=weather to get all weather entities.

Finally, I can do http://homeassistant:1880/endpoint/hastate and get EVERYTHING.

Thank you for your quick reply.
Is it possible just to get a return of 0 for off and 1 for on?

Pretty much i am looking for something below for the response.

statusUrl <string | urlObject> required: Defines the url (and other properties when using
an urlObject) to query the current power state from the light bulb. By default it expects the http server to
return ‘1’ for ON and ‘0’ for OFF leaving out any html markup.

Use something like a function node to output the state to the payload.

var status = (msg.payload[0]['state'] == "on" ? 1 : 0);
var msg.payload = status;
return msg;

Stick that before the http response node, and after the get entities node.
It will return 1 if it’s on and 0 otherwise (including unavailable states)