How do I trigger a HA script remotely over command line?

Home Assistant is running on a Raspberry Pi using the standard hassio install img. SSH Server is installed and working. I can FTP into Home Assistant from my Mac, and in terminal on my Mac, I can log on at ssh root@my_HA_IP.

Let’s say I have a script in Home Assistant named watch_tv (watch_tv.yaml). How do I trigger it from my Mac?

Not sure HA REST API allows script running directly, but you can always set up an automation with MQTT trigger to run the script. I don’t have Mac, you should be able to do MQTT publish with any MQTT client from command line, or HA REST API can do that:

Send a MQTT message:

$ curl -X POST \
     -H "Content-Type: application/json" \
     -H "x-ha-access:YOUR_PASSWORD" \
     -d '{"payload": "OFF", "topic": "home/fridge", "retain": "True"}' \
     http://localhost:8123/api/services/mqtt/publish

This sounds like a job for an API. Fortunately homeassistant has one. https://developers.home-assistant.io/docs/en/external_api_rest.html. Then just set up a binary switch you can trigger and hit the endpoint.

I’ve been struggling with the API and gotten nowhere, sadly.

I used to be able to do this:

curl 'MY_HA_IP:8123/api/services/homeassistant/turn_on?api_password=PASSWORD' --data-binary '{\"entity_id\":\"script.watch_tv\"}'

That stopped working when I updated Home Assistant and no matter what I’ve tried, I can’t figure out why.

I ended up settling on using Home Assistant CLI, which is included when installing “SSH & Web Terminal”. with Home Assistant CLI, this simple command works:

ssh root@MY_HA_IP hass-cli service call --arguments entity_id= script.watch_tv

So… I’ve got remote access to my HA scripts, but I feel like Home Assistant CLI runs a bit slower than when I used to send the command directly to the HA API… but like I said, I can’t figure out how to get the HA API to accept commands (I get “unauthorized” errors).

As for the HA API: I’ve tried using my HA password. I’ve tried using a Long-Lived Access Token.

Any idea what’s going wrong?

You need to authenticate properly with a long lived token, see the page you were referred to

curl -X GET -H "Authorization: Bearer eyJ0eXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXGy08tHRdlsEbjfTKcjglETWdbx5vTlMzfuY" host:8123/api/
{"message": "API running."}

Nick is correct that a token is the best way on this, but you still can use password, oh and I believe in this scenario you need to do a post. The API requires a post or get for interaction.
curl -X post https://yoururl/api/services/automation/trigger?api_password=you_password
-H {“content-type”: “application/json”}
-D {“entity_id”: “automation.your_automation”}

Just in case it helps anyone in the future, the following works well for me:

curl -X POST -H "Authorization: Bearer ABCDE" -H "Content-Type: application/json" -d '{"entity_id": "script.myscript"}' http://192.168.1.10:8123/api/services/script/turn_on

ABCDE - replace this with a long lived token which can be generated from your user profile within HA.

2 Likes

jarrah, you’re exactly right. That’s what works for me.

So, for Applescripts, it looks like this:

do shell script "curl -k -X POST -H \"Authorization: Bearer MY_LONG_LIVED_ACCESS_TOKEN\" -H \"Content-Type: application/json\" -d '{\"entity_id\": \"script.MY_SCRIPT\"}' http://MY_HA_IP:8123/api/services/homeassistant/turn_on"
1 Like

this might help someone else, I was trying to execute a script and pass 3 arguments to the script from curl. this is what worked

I got the clue from here


POST /api/services/<domain>/<service>
#!/bin/bash
cam="$1"
detection="$2"
confidence="$3"
ha_token="ABCDE"

curl -X POST \
    https://yourdomain.duckdns.org/api/services/script/your_script_name \
    -H "Authorization: Bearer ${ha_token}" \
    -H "Content-Type: application/json" \
    -d '{ "cam": "'"${cam}"'", "detection": "'"${detection}"'", "confidence": "'"${confidence}"'" }'

your_script_name does not include “script.” in my case your_script_name was aitool_direct_notification

my script was defined like this

aitool_direct_notification:
  description: 'aitool direct notification'
  fields:
    cam:
      description: 'name of camera object was detected on'
      example: 'frontPorch, frontDoor, frontLeft, frontRight'
    detection:
      description: 'what kind of object detected'
      example: 'person, car, truck, bicycle, motorcycle, cat, dog'
    confidence:
      description: 'confidence percentage of object detected, 0-100%,  80% is good'
      example: '90.0'
  sequence:
    - service: tts.google_say
      data_template:
        entity_id: media_player.family_room_display
        message: 'attention, a {{ detection }} was detected on {{ cam }} camera with confidence {{ confidence }}% '

my router setup allows me to not specify port 8123 as I have SNI setup in my router
most people will have to use this url
https://yourdomain.duckdns.org:8123/api/services/script/your_script_name

1 Like