brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a Home Assistant webhook trigger lets any script start an automation with curl

Curl
~/tips/tip-home-assistant-webhook-trigger-trigger-json

- trigger: webhook

Starts a Home Assistant automation from curl and reads the JSON you send in templates.

A backup script, a build server or an iPhone shortcut can all talk to Home Assistant without an API token if you use a webhook trigger. I run Home Assistant on my Unraid server, and this is the simplest way I know to turn “something finished” into a phone notification.

Add a Webhook trigger to an automation, and Home Assistant fills in a Webhook ID that is hard to guess. The address of the webhook is /api/webhook/ followed by that ID. In YAML:

triggers:
  - trigger: webhook
    webhook_id: "YOUR_WEBHOOK_ID"
actions:
  - action: notify.send_message
    target:
      entity_id: notify.my_device
    data:
      message: "{{ trigger.json.message }}"

Then call it. The documentation’s example for JSON data sets the content type header:

curl -X POST -H "Content-Type: application/json" \
  -d '{ "message": "Backup finished" }' \
  http://YOUR_HOME_ASSISTANT_ADDRESS/api/webhook/YOUR_WEBHOOK_ID

Form data lands in trigger.data, JSON in trigger.json and URL query parameters in trigger.query, so the automation above forwards the message from the request.

The security notes in the documentation matter more than the syntax. A webhook needs no password or token: anyone who knows the ID can call it, so treat it like a password and don’t copy IDs from blueprints or other public sources. By default a webhook only accepts POST and PUT from your local network. The options allowed_methods and local_only change that, and the documentation advises keeping local_only on unless you need access from the internet. It also says not to use a webhook for anything destructive or safety related, such as unlocking a door.

One debugging surprise: Home Assistant almost always answers 200 OK, even for an ID that doesn’t exist or a request from outside the local network to a local-only webhook. Only a method that isn’t allowed gets 405. If nothing happens, look at the automation’s traces and the system logs. This follows the documentation and was not run against a live instance.

Further reading: Webhook trigger

More Curl tips curl -si -X POST -H 'Content-Type: application/json' \ curl --oauth2-bearer "$TOKEN" https://api.example.com/v1/items curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ All Curl tips (74) All topics

Link