Skip to content

Instantly share code, notes, and snippets.

@docPhil99
Last active March 7, 2023 17:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save docPhil99/08b6e654f767bf6c09f344e779dd7964 to your computer and use it in GitHub Desktop.
Save docPhil99/08b6e654f767bf6c09f344e779dd7964 to your computer and use it in GitHub Desktop.
Controlling Home Assistant remotely via curl

It's actually fairly easy to control Home Assistant remotely using curl but I couldn't find a complete solution on how to do this, so here goes...

  1. Activate the api in configuration.yaml by adding the line api:
  2. Get an Authorization token from HA (It's a Long-Lived Access Tokens which can be created on your HA user profile page)
  3. This list of exposed states can be found using curl -X GET -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" http://YOUR_IP:8123/api/states | prettyjson
  • Note prettyjson is an alias for python -m json.tool, you don't need this it's just easier to read.
  1. To get the state of a device append the entity.id to the URL, eg curl -X GET -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" http://YOUR_IP:8123/api/states/switch.mylight | prettyjson
  2. The API documentation shows you how to change the states but this does not actually turn on the lights, etc. Instead, use the services. First find the correct service and the domain with curl -X GET -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" http://YOUR_IP:8123/api/services | prettyjson
  3. For the switch domain there are toggle, turn_on and turn_off on my system. To turn the switch on use curl -X POST -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -d '{"entity_id": "switch.mylight"}'http://YOUR_IP:8123/api/services/switch/turn_on | prettyjson
  • Note this is a POST and we send the name of the device in the data section as json.
@Magneticdud
Copy link

the final URL is /api/services/switch/turn_on and not /api/service/switch/turn_on - otherwise you'll get a 404

Thanks for the guide, much easier to understand than the official docs!

@docPhil99
Copy link
Author

Thanks @Magneticdud I've fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment