JSON payload from a webhook call

Hi. Im trying to make an automation which would make a telegram notification based on a message incoming in JSON data through a webhook.
Unfortunately, with webhooks its hard to debug those automations, as i cannot use template debuger.

Here’s my JSON:
{ "event": "charging" }

And here’s my automation:

    - service: notify.notifier_telegram
      data_template:
        title: "Car battery"
        message: "Car engine is now {{ trigger.json.event }}!"

Which fails.
If i remove event from the template:
message: "Car engine is now {{ trigger.json }}!"
the automation works, but the notification is just “Car engine is now” message.
I have tried:
message: "Car engine is now {{ trigger.json.data.event }}!"
message: "Car engine is now {{ trigger.json.data['event'] }}!"
message: "Car engine is now {{ trigger.json['event'] }}!"

But they all fail - no message is sent at all.

I have made some progress:

    - service: notify.notifier_telegram
      data_template:
        title: "Car battery"
        message: "Car battery is now {{ trigger.data }}!"

creates a message:

Car battery is now <MultiDictProxy(‘{“event”: “charging”}’: ‘’)>!

But i have also tried
message: "Car battery is now {{ trigger.data.event }}!"
and
message: "Car battery is now {{ trigger.data['event'] }}!"

and with both of them the message is just:

Car battery is now !

You need to make sure the web request has Content-Type set to application/json. E.g., with curl you’d need to use:

-H "Content-Type: application/json"

If the request has that in the headers, then it will JSON decode the message and create trigger.json. Otherwise it will put the raw data in trigger.data.

Thank you! I believe it works now :slight_smile:

1 Like