Handling Emby Webhooks - datatype/formatting issue?

I use Emby as my media player and I want to send a notification when new media is available. Emby has an option to send a webhook when this happens, so I figured it would be an easy automation. I configured it how I thought it should work to no avail - it seems like the incoming data is not being parsed as JSON.

According to Emby, this is the HTTP POST request it’s sending:

multipart/form-data; boundary="xxxxxxxxxxxxxx"

application/json; charset=utf-8
{"Title":"Test Notification","Description":"Test Notification Description","Date":"2023-11-27T02:30:03.2982964Z","Event":"system.webhooktest","User":{"Name":"xxxxxxxxx","Id":"xxxxxxxxxxxxxxxxx"},"Server":{"Name":"emby","Id":"xxxxxxxxx,"Version":"4.7.14.0"}}

Given that it’s JSON data and marked application/json, I would expect it to work like other JSON webhooks i’ve configured in the past:

alias: Send Emby Notifications
description: ""
trigger:
  - platform: webhook
    allowed_methods:
      - PUT
      - POST
    local_only: true
    webhook_id: "xxxxxxxxxxxxxx"
condition: []
action:
  - service: script.send_notification_to_all
    data:
      title: Emby
      message: "Msg: {{ trigger.json['Title'] }}"
mode: single

Accessing trigger.json.xxxx doesn’t work, however. If I examine the trace, I can see that trigger’s data is in a bytearray with a nested data object, as if it’s not being parsed as JSON:

trigger:
  platform: webhook
  webhook_id: 'xxxxxxxxxxxx'
  data:
    __type: <class 'multidict._multidict.MultiDictProxy'>
    repr: >-
      <MultiDictProxy('data': bytearray(b'{"Title":"Test
      Notification","Description":"Test Notification
      Description","Date":"2023-11-26T23:11:05.0736133Z","Event":"system.webhooktest","Server":{"Name":"emby","Id":"xxxxxxxxxxxxxxx","Version":"4.7.14.0"}}'))>
  query:
    __type: <class 'multidict._multidict.MultiDictProxy'>
    repr: <MultiDictProxy()>
  description: webhook
  id: '0'
  idx: '0'
  alias: null

I’ve tried a number of things based on this thread, but it looks like OP ultimately gave up and used the Emby integration. I’m stubborn and want to get this webhook working (besides, I’m not sure if the Emby integration has triggers for new media anyway).

I’ve landed on this as my closest-to-success attempt:

service: script.send_notification_to_all
data:
  title: Emby
  message: "{{ trigger.data['data'].decode('utf-8') }}"

With this, I receive a string which looks like JSON, but isn’t:

{"Title":"Test Notification","Description":"Test Notification Description","Date":"2023-11-27T02:39:04.2683300Z","Event":"system.webhooktest","Server":{"Name":"emby","Id":"xxxxxxxxxxxxxxx","Version":"4.7.14.0"}} 

If I try to access the Title property here, nothing is returned. I tried the tojson filter, which also didn’t seem to work and just returned the same string but with the quotes escaped. No matter what I just get a plain string. How do I get this to be parsed as JSON so that I can access the fields properly?

Hey, I’ve been working on the same thing and came across your post. You got me half way there so I thought I’d share how I got it to work.

I used the from_json method, and stored the result in a variable which I could then call. Here’s my code:

alias: Emby
description: ""
trigger:
  - platform: webhook
    allowed_methods:
      - POST
    local_only: true
    webhook_id: "xxxx"
condition: []
action:
  - service: notify.mobile_app_m_iphone
    data:
      message: "Added: {{ payload.Title }}"
variables:
  payload: "{{ trigger.data['data'].decode('utf-8')|from_json }}"
mode: single

However, I then realised you need Emby premiere to actually get the calls to work, which I don’t have! So instead I created webhooks from Radarr and Sonarr which seems to work fine.

I hope that helps you or anyone else trying to do the same thing.