Map webhooks into Home Assistant events (Generate a button by template webhook trigger?)


it is pretty simple to generate template sensors:

  - platform: webhook
    webhook_id: xxxIDxxx
  - name: "PVE MiB written"
    unique_id: pve_mib_written
    state: >-
      {{ (trigger.json.nvme_smart_health_information_log.data_units_written * 512
      / 1024 ) |round }}
    unit_of_measurement: "MiB"
  - name: "PVE Percentage used"
    unique_id: pve_percentage_used
    state: "{{ trigger.json.nvme_smart_health_information_log.percentage_used }}"
    unit_of_measurement: "%"
    state_class: total
  - name: "PVE available Spare"
    unique_id: pve_available_spare
    state: "{{ trigger.json.nvme_smart_health_information_log.available_spare }}"
    unit_of_measurement: "%"

Now I want to have a button entity that get’s “pressed” by some REST call. Possible?

Another option would be to post an event on a REST call. How?

Use the service call.

  - event: your_custom_event
      state: your_value


I guess my question was not clear.

Is there a way to generate a button entity that acs on a webhook (like the sensors above do)?

Same for events. Can I create an event directly from a webhook via template?

I know that I can use the API REST calls to generate events, but this also needs authentication and stuff, which I need to avoid, since my calling part can not include it.

Using a webhook trigger in automation works, but I wanted to avoid these automations which then just fire events.

I want to have events since I can trigger on them at multiple places and they carry more information as last_fired.

Contrary to what you stated in your first post, the examples don’t “generate template sensors”. The three sensor entities are pre-defined and the Webhook Trigger simply supplies them with state values. The Webhook Trigger doesn’t contain information that creates a sensor entity from scratch.

The closest you can get to programmatically create entities is with MQTT Discovery. By publishing an appropriate payload to Home Assistant’s discovery topic, you can truly create new sensor, button, binary_sensor, switch, light, etc entities programmatically.

You can post a custom event using the example I posted above.

The content in the first post is the file template.yaml which is included in configuration.yaml with
„template: !include template.yaml“

There are sensors generated directly! These are not automations.

I want to get the same to generate button entities and or events.

I found this:

With the help of one catch all automation and one clever rest_command it should be possible to map webhook to events.

I just want to share what I finally did.

In configuration.yaml:

    url:{{ event_type }}
    method: post
      authorization: !secret ha_secret_bearer
    payload: "{{ event_data |to_json }}"
    content_type: "application/json"
    verify_ssl: true

You need an auth bearer for that!

Parameters to call are even_type for the name of the event and event_data as all data to include with the event.

This allows me to call the service rest_command.trigger_rest_event from automations.

Then there is only one automation to handle all webhooks and call that service to fire the events.

alias: "REST Event from WebHook"
description: ""
  - platform: webhook
    webhook_id: your_webhook_id1
    id: ShortCuts
  - platform: webhook
    webhook_id: your_webhook_id2
    id: Geofency
condition: []
  - choose:
      - conditions:
          - condition: trigger
            id: ShortCuts
          - service: rest_command.trigger_rest_event
              event_type: webhook.shortcuts.{{ trigger.json.trigger }}
              event_data: "{{ trigger.json }}"
      - conditions:
          - condition: trigger
            id: Geofency
          - service: rest_command.trigger_rest_event
              event_type: webhook.geofency.{{ }}
              event_data: "{{ trigger.json }}"
mode: single

As you see, I implemented two basic formats. One for the iOS app Geofency and the other one for the iOS Shotcuts app.

In Shortcuts one JSON data needs to be added to the request: trigger which is later used as the name for the event: webhook.shortcuts.trigger.

For Geofency the event is named by the place that Geofency tiggered the webhook from:

All JSON data from the webhooks is also added to the events.

Now I can act on the events as on all other events and can also query when they got last_fired.

Since all data is attached to the event I can use them in templates in the automations.

Hope this helps somebody else if needed.

I now derive the event name from the webhook using “-” as separator. Even simpler like that.

  - service: rest_command.trigger_rest_event
      event_type: webhook.{{ trigger.webhook_id.split('-')[0] }}
      event_data: "{{ trigger.json }}"
1 Like