API/URL Access from custom app to HA

Hi all,

I was wondering if HA or HASS.io can be managed/access and devices turned on or off/dimmed via an external app/link/call. I have had a look at https://home-assistant.io/developers/api/ and it seems it can be done, but the documentation is a bit slim on the implementation and actual changing states on the HA devices.

What I am looking for is something like this, calling this will set the dimmer to 50%:

https://haserver:8123/deviceid=dimmersitch1?setvalue=50

Or something like that because I have a requirement to do this from an external app/browser

Any thought/ideas/samples anyone seen before?

The RESTful API is exactly what you are looking for.

Hi Daniel,

Thank you for the info, I am trying to figure out how to get and set the data, note that I only have a browser/links to manipulate the devices.

I have tried the following, and I can see all the devices and stuff returned in JSON ! Yay!

https://myhassio.duckdns.org:8123/api/config?api_password=SuperPassword

Question now is, how do set a device on or off with a single browser link?

From the link, you do it with curl, but I do not have access to curl (Windows box):

Turn the light on:

$ curl -X POST -H “x-ha-access: YOUR_PASSWORD”
-H “Content-Type: application/json”
-d ‘{“entity_id”: “switch.christmas_lights”}’
http://localhost:8123/api/services/switch/turn_on

Any idea how this translates into a single link?

You can’t. Setting stuff requires you to send a POST request, along with some headers. A regular link can’t do that.
If however you have to option to use JavaScript, you could use something like jQuery and do the POST request that way.

And if you have to use vanilla JavaScript: https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest

Unfortunately my JavaScript is non-existent - I do however know a bit of C++ and C#

Anyone done anything with c# and any idea how to call it to make a change in device state?

First esult on Google looks promising: https://stackoverflow.com/a/4015346/1369460

Nice if you know what to search for - Thank you very much for pointing me into the right direction - appreciated.

I got 404’s in all my code, so I have gone back to basics and tested the following:

SSH onto the HASS.io device (RasPi3), then run the following:

curl -X POST -H “x-ha-access: SuperSecret” -H “Content-Type: application/json” -d ‘{“entity_id”: “light.hallway”}’ https://myhome.duckdns.org:8123/api/light/turn_on

I get a responce:

404: Not Foundcore-ssh:~#

Surely this is not correct. Anyone else have this?

Can you access the regular UI with the same URL? It doesn’t make sense if your local computer can connect to https://myhome.duckdns.org:8123, but appending /api/foo leads to a 404. That’s exactly where the API is at. The only case where I could imagine that happening would be with a reverse proxy in front when does some rewriting based on the path at the end of the URL.

EDIT:
Wait, it should be /api/services/light/turn_on I think

Daniel shoots, he scores !!! - Thank you very much, for reference, here is a simple C# function to turn a light on:

Not sure if the preview will render it correct, but hey, it works

Note the ‘using’ includes below with all the stuff in UPPERCASE needs changing, maybe this will help someone else - next step is to figure out how to set the dim level on a light - any ideas when to get that info from, the help/documentation is a bit this on that?

using System.Net;
using System.IO;
using System.Web.Script.Serialization;

void switch()
{
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(“https://YOURHASSIO.duckdns.org:8123/api/services/light/turn_on?api_password=SUPERSECRET”);
httpWebRequest.ContentType = “application/json”;
httpWebRequest.Method = “POST”;

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    entity_id = "light.HALLWAY",
                });
                streamWriter.Write(json);
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString(),"Error in you code!");
        }
    }