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?