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

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)

@mobile.andrew.jones Thank you for your help.
This is amazing. I am kinda a newbie in node red this really points me to the right direction.

One last one for http toggle on the lights.

Currently i am using:
[{“id”:“4de0edb64ae67216”,“type”:“tab”,“label”:“http”,“disabled”:false,“info”:"",“env”:[]},{“id”:“ef4e4794.3c12a8”,“type”:“http in”,“z”:“4de0edb64ae67216”,“name”:"",“url”:“malvin-light”,“method”:“get”,“upload”:false,“swaggerDoc”:"",“x”:210,“y”:240,“wires”:[[“1d707ade.fdea35”,“adc182c0.51cc5”]]},{“id”:“1d707ade.fdea35”,“type”:“http response”,“z”:“4de0edb64ae67216”,“name”:"",“statusCode”:“200”,“headers”:{},“x”:340,“y”:320,“wires”:[]},{“id”:“adc182c0.51cc5”,“type”:“api-call-service”,“z”:“4de0edb64ae67216”,“name”:"",“server”:“cfe985c2.dd9ee8”,“version”:3,“debugenabled”:false,“service_domain”:“light”,“service”:“toggle”,“entityId”:“light.malvin_room_light”,“data”:"",“dataType”:“json”,“mergecontext”:"",“mustacheAltTags”:false,“outputProperties”:[],“queue”:“none”,“x”:480,“y”:240,“wires”:[[]]},{“id”:“cfe985c2.dd9ee8”,“type”:“server”,“name”:“Genisys Home Assistant”,“version”:2,“addon”:true,“rejectUnauthorizedCerts”:true,“ha_boolean”:“y|yes|true|on|home|open”,“connectionDelay”:true,“cacheJson”:true,“heartbeat”:false,“heartbeatInterval”:30}]

Is it possible to create payload request on light toggle instead of creating multiple entries?

If you mean move the http response node AFTER the service call, yes.
Use the debug node and take an output of the service call node to the debug node and then you can see what it outputs. And paste the output here, I’m fairly sure it outputs the state of the entities that have been changed.

Hi,

This is awesome!
I got it to work now. Thanks.

Just FYI,

I am using this small hack to get Alexa to work with HASS locally.

So i am using homebridge http light bulb to integrate home assistant via node red.
And homebridge has a free intergration to Alexa.

This helps to save my home assistant monthly subscriptions.

Cheers.

I am trying to create a Keyboard Maestro macro that turns an light on or off to act as an “on air” light when I’m on WebEx. I’m trying to use the API per the /api/services/<domain>/<service> “Turn the light on” curl example:

curl \
  -H "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpZVCJ9.eyJpc3Mi0iI0MzRiY2Q5NzE4ZDc0ZDU0OWRlY2RjZGVkZmU4NTNhMSIsImlhdCI6MTY3NjMwNTc0NiwiZXhwIjoxOTkxNjY1NzQ2fQ.3VLu1n2hREJupOla8xBLhAjIu4wwem0g9pQ8UPMaOwA" \
  -H "Content-Type: application/json" \
  -d '{"entity_id": "light.ledvance_a19_rgbw_light"}' \
  http://homeassistant.local:8123/api/services/light/turn_on

(The key is slightly edited.)

However, I consistently get 401: Unauthorized as my return code. I’ve created and tried different two Long-Lived Access Tokens with the same result; I’ve also tried different light entities. I’m clearly missing something - any pointers?

hi all!

I need to do the same thing.
Using the API to turn on a device (a wiz bulb). I can do it on the front end obviously but i need to do it via API.

This is my CURL request:

curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer mytokenhere" -d '{"entity_id": "wiz_bulb"}' http://myhost.home:8123/api/services/switch/turn_on

Any clue how to do it simple?.

1 Like

Another option is to use the MQTT facility that is included in ZwaveJS-UI. I have my HA set up to respond to MQTT messages. So for example to turn on a light I can use an MQTT app to send a message with the device name and “on” to tell HA what it should do. You can trigger an automation, so it’s not limited to controlling just Zwave devices.

uhmm, ok, i can do some mqtt but this devices cannot connect via that protocol. The HA integration connect the devices via wifi and register their firmware and so on.

I need to be able to manipulate those settings via the HA API.

Thanks!