How to construct a webhook with form data

I am trying to construct a webhook with form data as described on the Nabu Casa website but the documentation is a little sparse. Ultimately I want to call this webhook from Microsoft Flow. I’ve created a webhook for another automation without the form data and this works fine. I gave up on that and tried to use Postman, also failed and I’m trying curl and also failing.

This is the action YAML in my automation; I’m not sure if this is correct.

service: counter.configure
target:
  entity_id: counter.annual_leave
data_template:
  value: '{{trigger.data.annual_leave}}'

as there is nothing in the Nabu Casa documentation about how to add the form data to the webhook URL. I’ve tried lots of random googling and trial and error but always run into this error message;

2021-03-16 14:18:06 ERROR (MainThread) [homeassistant.components.automation.annual_leave_update] Annual Leave Update: Error executing script. Invalid data for call_service at pos 1: expected int for dictionary value @ data['value']
2021-03-16 14:18:06 ERROR (MainThread) [homeassistant.components.automation.annual_leave_update] Error while executing automation automation.annual_leave_update: expected int for dictionary value @ data['value']

The simplest call I am trying is as follows, as if I can get it working in curl, at least I have somewhere to start.

curl -X POST https://hooks.nabu.casa/my_webhook_id -d '{"annual_leave":18}'

I haven’t used webhooks with JSON payload myself, but I think the following should work:

  • Add a content header to your request:
curl -X POST https://hooks.nabu.casa/my_webhook_id -H "Content-Type: application/json" -d '{"annual_leave":18}' 
  • This should then make the content available as follows:
  value: '{{ trigger.json["annual_leave"] }}'

Thanks, I got there in the end. I did specify “application/json” which I think was a crucial part. I then used the following in the automation.

service: input_number.set_value
target:
  entity_id: input_number.annual_leave
data_template:
  value: '{{trigger.json[''annual_leave'']}}'
1 Like

FWIW, this should also work:

service: input_number.set_value
target:
  entity_id: input_number.annual_leave
data:
  value: '{{trigger.json.annual_leave}}'