Change switch state using url

I would like to be able to turn on a light or change the state of a switch using a URL. What is the best way to do this? A URL that calls a python program which does a Post? Can anyone provide a simple example?

There is the API, but I’m not sure it’s simple enough.

Try the restful switch

Thanks. I saw those docs but I was trying to create a python program to change the state of a switch. I tried the following:


from requests import post

url = "http://192.168.3.160:8123/api/services/switch/light.turn_on"
headers = {
    "Authorization": "Bearer ABCDEF",
    "content-type": "application/json",
}
myobj= '{"entity_id": "light.ohd_left"}'
response = post(url, headers=headers,data=myobj)
print(response.text)

Response is Bad request

Figured it out.

from requests import post
url = "http://192.168.3.160:8123/api/services/light/turn_on"
headers = {
    "Authorization": "Bearer ABCDEF",
    "content-type": "application/json",
}
myobj= '{"entity_id": "light.ohd_left"}'
response = post(url, headers=headers,data=myobj)
print(response.text)
2 Likes