Get URL query parameter from webhook trigger

Hi all,
I’m trying to get the URL query parameters from a webhook trigger.

As per the documentation I’m trying to get from the trigger.query variable but I’m doing something wrong because I’m not able to get them properly.

I’ve created the following ‘dummy’ automation:

- alias: '[Webhook - Slack] Slack webhook'
  trigger:
    - platform: webhook
      webhook_id: slack-webhook
  action:
    - service: persistent_notification.create
      data_template:
        title: 'Slack Test'
        message: >-
          @ trigger.data:<br><br>{{ trigger.data }}<br><br>
          @ trigger.query.payload: '{{ trigger.query.payload }}'<br><br>
          @ trigger.query['payload']: '{{ trigger.query['payload'] }}'<br><br>
          @ trigger.query.get('payload'): '{{ trigger.query.get('payload') }}'<br><br>
          @ trigger.query: '{{ trigger.query }}'<br><br>
          @ trigger.query.keys(): '{{ trigger.query.keys() }}'<br><br>
          @ trigger.query.items(): '{{ trigger.query.items() }}'<br><br>
          @ trigger.query.values(): '{{ trigger.query.values() }}'<br><br>

I want to extract the json value from the payload query parameter.

Executing the shown automation I get the following persistent notification:

How can I extract the info from payload query parameteter?

Reponding to myself for others facing similar same problem.
I used trigger.data.payload instead trigger.query.payload.

From what I’ve read and seen you cannot parse the json string to a json object by using the tosjon filter. You need to “parse” the json string by yourself doing splits and replaces until you get your desired data.
In my case:

{% set action = trigger.data.payload.split(",")[3].replace("]","").replace("}","").split(":")[1].replace("\"","").split("#") %}
{{ action[0] } # This is the service name

{% set action = trigger.data.payload.split(",")[3].replace("]","").replace("}","").split(":")[1].replace("\"","").split("#") %}
{{ action[1] }} # This is the entity_id
2 Likes

Also,
worth pointing out that the data which is sent via a post is available as trigger.data but the query strings are simply available using the objects.

For example, if you send this query string to your instance:
curl -X POST https://myhomeassistantaddress.duckdns.org/api/webhook/beaver?message=oh%20yes

Then you can create an automation with a trigger which is a webhook with the ID of beaver and then get the results like this:

- id: '1603620844963'
  alias: MS Flow - Announce Anything
  description: Webhook which takes a string to say a phrase
  trigger:
  - platform: webhook
    webhook_id: beaver
  condition: []
  action:
  - service: notify.alexa_media_office_show
    data_template:
      data:
        method: speak
        type: announce
      message: 'Message from MS Flow is {{ trigger.query.message }}'
      title: 'Beaver came in '

Now you have an end point for an external system to send any announcement, if it knows the secret Webhook ID.

1 Like

Hi @xerxel. Is it possible to include multiple objects in trigger.query? For example

message: 'Camera named {{ trigger.query.monitor }} detected an object at {{ trigger.query.time }}'

If yes, how does the webhook need to be formatted? I was trying to delimit the objects like this but it didn’t work…

curl -X POST http://192.168.1.115:8123/api/webhook/shinobiwebhook?monitor=ABCDEFGH&time=001230

That is correct, you just need to put the whole URL in quotes in your case:

curl -X POST "http://192.168.1.115:8123/api/webhook/shinobiwebhook?monitor=ABCDEFGH&time=001230"