Simple webhook example

This worked until recently. In automations:

- alias: "Open Door"
  trigger:
    platform: webhook
    webhook_id: Open_Door
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.door_open

The door opened when I entered this into a web browser:
https://myhass.com/api/webhook/Open_Door

Alas, now when I enter that into a web browser, I get:
405: Method Not Allowed

All signs point to some change in Hass (everything is updated as of today). How do I make this work again?

Cheers, Richard

1 Like

When you type that URL into a browser, you’re performing a GET request. Webhooks are expecting a POST, accompanied by some data. You’ll need to test with a utility like Postman or curl.

I don’t know how it ever would have worked from a browser.

I’ll also mention here that your webhooks should be something that is not easily guessable. I typically use two GUIDs concatenated; it ends up looking like this:

http://ha.example.com/api/webhook/9b891494d00545e39c633c6b17c84283614c07f062c0419aaa31f73cf6bec3bb

I know this is probably just a test automation, but if this actually opened or unlocked a door, it could be a real problem if it were guessed. Treat your webhook IDs like passwords because they kind of are.

3 Likes

Right you are. I was mistaken that it previously worked in HASS.

And thanks for pointing out about the security. I’m using this with IFTTT, but I’m not using their API which has a similarly long key. I’m thinking why bother with the IFTTT integration when I can create my own long “key” for each webcommand.

Frankly, I agree. IFTTT is pretty slow. I only use it for integrations with things that don’t have a public API. It’s a last resort.

Hi, since this is one of the top posts regarding home assistant and webhooks: Am I the only one who is not able to find official documentation about webhooks? Do I need to activate that somehow? By default there is not even port 80 or 443 opened, if I am right. Also my port scanner does not find anything there. Or is it configured automatically when I use platform: webhook?

Thank you,
Stefan

Here you go:

FYI, I googled for “home assistant automation trigger webhook”.

3 Likes

I have learned you can’t rely on the search button in the docs.

However, I still use IFTTT (mostly for voice commands) and I get virtually instant results.

As for opening ports, I also recommend checking out Nginx Proxy Manager.

hello, could you explain how to configure via IFTTT?

In IFTTT, set THAT to Webhook - Make a web request. URL is your webhook in HASS. Method is Post. Content Type is text/plain. Everything else blank (default).

You can also download the Postman program to test your webhooks.

With postman work, but with link: https://XXX.duckdns.org:8123/api/webhook/XXX 405: Method Not Allowed :frowning:

That’s because when you visit that URL through a browser, it’s a GET request, but in Postman, you can specify it to be a POST (which it has to be in order to work). It would be useful if HomeAssistant had web hooks that worked via GET, but currently, it does not.

2 Likes

Hi,
I recently was searching for a way to trigger webhooks externally to HASS.
I found out that you can create an automation with a webhook ID.
Afterwards in settings->cloud, when you activate the webhook, it gives you something like https://hooks.nabu.casa/random_id with what you can trigger this webhook also “externally” through the NabuCasa cloud, if you have it actived.

You are not required to use POST with a webhook. You just have to add GET to the list of allowed_methods.

automation:
  - alias: "spotify_resume"
      trigger:
        platform: webhook
        webhook_id: "spotify_resume"
        allowed_methods:
          - POST
          - GET
        local_only: true
      action:
        service: spotcast.start
        data:
          force_playback: true
          device_name: Callisto

Not anymore, that’s true. But this feature has only been in for a couple of months.

Is there any way to grab saved webhook values from database or history?

I have system in place that I forward the alarm state via webhook. It works and it even saves the history somehow, but every time I restart the HA the value is unknown. How could I get there latest value automatically before the state is next time changed?

I haven’t tried this, but I am going to when I get a chance: Use a trigger-based template sensor to store the webhook info. Unlike other template sensors, trigger-based template sensors retain their states through HA restarts.

And since the docs say you can use any of the triggers that are available to automations, that then includes webhook triggers.

So, create a trigger-based template sensor using your webhook as the trigger.

Then all your other actions/automations in HA can be based off state changes to that template sensor.

Edit: here’s a simple example. This webhook will just toggle a binary sensor every time the webhook is hit.

template:
  - trigger:
      - platform: webhook
        webhook_id: test-webhook
        allowed_methods:
          - POST
          - GET
        local_only: false
    binary_sensor:
      - name: "Test Webhook"
        state: >
          {{ this.state=='off' }}
        unique_id: "test_webhook"