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...
- Activate the api in
configuration.yaml
by adding the lineapi:
- Get an Authorization token from HA (It's a Long-Lived Access Tokens which can be created on your HA user profile page)
- 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 forpython -m json.tool
, you don't need this it's just easier to read.
- 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
- 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
- For the
switch
domain there aretoggle
,turn_on
andturn_off
on my system. To turn the switch on usecurl -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.
the final URL is
/api/services/switch/turn_on
and not/api/service/switch/turn_on
- otherwise you'll get a 404Thanks for the guide, much easier to understand than the official docs!