RestAPI issue PLEASE HELP!

Hi All,

I can switch on/off all my switch entities, by below command tested on RestAPI testing on webbrowser…

http://192.168.88.101:8123/api/services/switch/turn_off …or turn on. This switches on and off all the switches under HA. … which is OK … BUT

Now I want to switch off individual specific switch under HA and for that I am passing below command but it is not working, It still switches on or off ALL my switches

http://192.168.88.101:8123/api/services/switch/turn_on?{“entity_id”:“switch.bedroom_light”}

switch.bedrrom_light is the entity of my Bedroom light. Above command, no only switches the light but also all other switches like my speakers and my table lamp, even though they all have different seperate entity IDs

I know something is wrong with the syntax, can anyone assist how to pass RestAPI for apecific devices/entity id from URL

Regards
Sammy

I’m not sure what tool you are using but your syntax suggests that you are using the GET method rather than the POST method.

OK what would be the correct syntax for POST method. basically I need to control my switches independently via Rest API HTTP. Can you assist how I can do that.
I am using Postman google extension to test BTW.
Below is the POST syntax as per HA

POST /api/services/domain/service

I dont see where my URL is getting wrong :frowning:

LET ME ADD … In Postman google extension if I enter the command like below

192.168.88.101:8123/api/services/switch/turn_on

and then under Body as RAW data I add

{
“entity_id”:“switch.bedroom_light”
}

This works as it should … meaning it “ONLY” switches off the bedroom light. BUT BUT

if I want to pass the entire command as one single URL, like below

http://192.168.88.101:8123/api/services/switch/turn_on?{“entity_id”:“switch.bedroom_light”}

THIS switches not only bedroon light BUT also all my other switches entities ???.. :frowning: :frowning:

I want to pass the argument as one single URL to make “only” my bedrrom light lit

It’s not the URL, it’s how you are accessing it - what tool or programming language are you using?

Using the POST method (which is what the API requires() you can’t send all the data via just the URL, that’s not how HTTP works. You need to hit the URL with a Post request which takes the additional data and supplies it in the body of the request, not as part of the URL.

The CURL examples in the API docs show how you can do this from the command line. If you are doing this from Python or some other language, the appropriate libraries will have parameters that allow you to specify a POST request and supply the parameters.

I am using imperihome to execute an HTTP request from where I want to controll my switches via HTTP request. Also using Google Postman to test, where it works if I put the arguments in body under RAW. as I have shown in my edited post above.

But this is not what I want I want to use the entire command in one URL like below

http://192.168.88.101:8123/api/services/switch/turn_on

The above works in Imperihome under execute HTTP request BUT switches on ALL my switches entity IDs.

I want to pass URL HTTP request to independently controll my switches??

Hope I was able to make this understand
Regards

I understand what you are trying to do, but read my post above - you can’t do this with just a URL, you need to tell Imperihome to use a POST request and supply the parameters (I have no idea if Imperihome can actually do that)

OK then what the solution .If the URL => …http://192.168.88.101:8123/api/services/switch/turn_on works in imperihome and switches on all my switches, there must be a way to “only” switch on specific switch via same URL ???

imperihome can take a URL and I can also choose “get” or “post” which obviously I chose POST and then past the URL there.

@aimc, check the screen shot of imperihome

Hi @aimc
Chec kthis thread …

ih8gates managed to do the same, what I am trying to do. in his case it works … scroll down to the 4th post from bottom.

No sure why it aint working in my case

Hi ALL

I made some progress … if you can call it a progress. I thought maybe I do not have HTTP password set in YAML file, it might be the reason, so I set the api_password in my YAML file, I supplied the below url to it.

http://192.168.88.101:8123/api/services/switch/turn_on?api_password=mypassword

It works and switches ON ALL my switch entites, (as expected) BUT when I pass the JSON like below,

192.168.88.101:8123/api/services/switch/turn_on?api_password=mypassword{“entity_id”:“switch.bedroom_light”}

it says failed to send a request and I have an error in HA console …“Login attempt or request with invalid authentication from 192.168.88.116”, …but how can authentication be wrong when “http://192.168.88.101:8123/api/services/switch/turn_on?api_password=mypassword” command IS working.

PLease someone assist me … I really want this to work. HELP The POOR Guy …:frowning:

Do you not have a password on your HA instance? Because if you do, you need to specify it in the POST request. None of the examples you gave show this, which is why I asked. I would think a password would be required to access the REST API in HA, just out of security concerns.

Something like this:

http://yourURL/api/services/switch/turn_off?api_password=yourPASSWORD{"entity_id":"switch.yourSWITCH"}

EDIT - Just saw your post. LOL

rpitera, I have set the password in my HA instance as I mentioned in the above post …and I am passing the same, it works, BUT when I pass JSON it says “Login attempt or request with invalid authentication from 192.168.88.116”, which is STRANGE, as if the authentication was the issue it would have failed when I passed the url WITHOUT JSON parameters.

Thanks for the assistance anyway,wish I can solve this. Its the only last link pending in my complete ROOM automating

I just replied in your other post on the same topic!

Yeah, I know. If you look at the post time, I was writing the reply when you posted it. Hence the reason for the EDIT.

@phileep has a good point about testing this in Postman to see if Imperihome is parsing the URL differently than expected.

I managed to solve it. Big thanks to ih8gates member who point me towards IFTTT.

I created an event in IFTTT and in the “then” part connected it to the web request,
IFTTT was already added to YAML file with the KEY. Then was just a matter of passing the below URL

https://maker.ifttt.com/trigger/EventName/with/key/myIFTTTKEY

and that switched on the Lights and only lights and not the speakers. I provided the same URL in Imperihome and IT WORKED!!!

The only tiny complaint is that its bit slow, I mean its takes 1-2 seconds after the button is pressed, Plus I cannot use local IP from IFTTT, I had to use DDNS and fw the HA IP, which I personally did not want to, but if that’s the only way to solve this so be it … I am happy. … :slight_smile:

Regards
Sammy