Now that plex has webhooks, and they actually work with the new webhook automation I thought I’d share a bit of progress I’ve made, and hope others can chip in!
Firstly you need to have a plex plus subscription - then you need to go into your settings, and webhooks. Add a webhook in the following format:
https://yourserver/api/webhook/someuniqueid
Now add an automation as follows
automation:
- alias: Plex Webhook
initial_state: 'on'
trigger:
platform: webhook
webhook_id: someuniqueid
action:
- service: mqtt.publish
data_template:
topic: 'plex/update'
payload_template: >
{{ (trigger.data['payload'] | string)[12:][:-2] | replace ("\\\\", "\\") | replace ("\\\'", "'") | replace ("\\x","?") }}
This will convert the data that comes in into json - sort of. It is still a string, and I haven’t figured out a way to directly convert it to json, so to achieve that, I then push the data into a mqtt queue
I then create an automation based on the topic above, and now I can use trigger.payload_json to get all the data! I push this into a script:
- alias: Plex Webhook As Json
initial_state: 'on'
trigger:
platform: mqtt
topic: 'plex/update'
action:
- service: script.process_plex_message
data_template:
event: "{{ trigger.payload_json.event }}"
player: "{{ trigger.payload_json.Player.title }}"
account: "{{ trigger.payload_json.Account.title }}"
The script can then do whatever you want (with conditions)
script:
process_plex_message:
sequence:
- service: notify.slackbot
data_template:
message: "Plex event: {{ event }} - {{ account }} - {{ player }}"
So this is where I’m up to. This works great, Unicode stuff gets escape in a bit of a nasty way, it it does appear to handle it now
I know using a mqtt like this is awful, so any other ideas would be great!
You can see where I’m up to here: https://github.com/rossdargan/hass-config/blob/master/configuration/packages/plex.yaml