Plex + Webhooks (WIP)

I now have the Plex JSON coming through as JSON through MQTT.

I would like to have all the attributes such as event and sub-attributes come through as “JSON attributes” (see: https://www.home-assistant.io/components/sensor.mqtt/ )

Any ideas on the right format to get the secondary attributes coming through under the JSON attributes section?

I’ve tried, e.g.:
Account.id
Account[id]
Account.[id]
Account[0].id

In appdaemon:

            mydict = json.loads(str(data['payload']))
            user = mydict['Account']['title']
            event = mydict['event']

Thanks. I don’t have app daemon. Will look into it but would prefer not to use anything extra like node red etc (or appdaemon). Will look into it.

Thanks for this project. The package works great exept i get warnings for invalid authentication. Anyone else who has this issue?

I have two-factor auth enabled.

I’m using your setup with sensors for each room. See below. I keep getting an error that a duplicate key exists for these sensors, and only the Great Room sensor shows up sometimes in HA. Using 0.85. Any idea what may be happening? For formatting purposes, I’m using:

sensor: !include_dir_list sensors

Very grateful for the work you’ve done. Very helpful!!

platform: mqtt
name: "Movie Room Plex"
state_topic: "plex/update/movieroom"
#value_template: '{{ value_json.Event == "media.resume" or value_json.Event == "media.play" }}'
value_template: "{{ value_json.Event }}"
force_update: false
json_attributes_topic: "plex/update/movieroom"
  
platform: mqtt
name: "Great Room Plex"
state_topic: "plex/update/greatroom"
#value_template: '{{ value_json.Event == "media.resume" or value_json.Event == "media.play" }}'
value_template: "{{ value_json.Event }}"
force_update: false
json_attributes_topic: "plex/update/greatroom"

I get no title displayed then playing movie. Anyoneelse has this issue?

my config below

      - alias: Plex Fredrik mobil
        initial_state: 'on'
        trigger:
          platform: mqtt
          topic: 'plex/update'
        condition:
          condition: and
          conditions:
            - condition: template
              value_template: "{{trigger.payload_json.Player.title == 'Falfr mobil'}}"
            - condition: template        
              value_template: "{{trigger.payload_json.event != 'media.scrobble'}}"          
            - condition: template
              value_template: "{{trigger.payload_json.event != 'media.rate'}}"                    
        action:
          - service: mqtt.publish
            data_template:
              topic: 'plex/update/falfr_mobil'
              retain: true
              payload_template: >
               {  "Event":"{{ trigger.payload_json.event }}", 
                  "Account":"{{ trigger.payload_json.Account.title }}", 
                  "Title":"{{ trigger.payload_json.Metadata.grandparentTitle }}", 
                  "Type":"{{ trigger.payload_json.Metadata.librarySectionType }}", 
                  "Image":"{{ trigger.payload_json.Metadata.thumb }}" }

    
   sensor:
       - platform: mqtt
        name: "Plex Fredrik mobil"
        state_topic: "plex/update/falfr_mobil"    
        value_template: '{{ value_json.Event == "media.resume" or value_json.Event == "media.play" }}'  
        json_attributes:
          - Account
          - Title
          - Type
          - Image
          - Event

thanks for all the input and ideas here. After combining some of them I got it all working.
Only problem is most of the times it won’t fire from the first play. So I have to pause and play again to get it firing.

link to configuration

It seems that I still have a problem with the media.play.
I think it is because the payload sends a picture with media.play and therfore the parsing fails. (as documented on plex site)
Anyone found a way to parse or evade this problem?

Help would be appreciated

Anyone please?

This started bothering me so I wrote a simple custom component that will trigger an event for every webhook! It’s compatible with HACS, just need to add it as a custom repository.

If you guys have an issues right them up in github and I’ll do my best to fix!

1 Like

I opened a PR to Telegraf for supporting Plex Webhooks if anyone wants to try it out and give feedback: https://github.com/influxdata/telegraf/pull/6511

Hi thanks for sharing this. i have tried setting up but not sure how to see if it is working - how can i test it, and can you give some automation examples please?
cheers,
james

Here is an example from my config:

As you can see all you need to do is use the event platform for a trigger with the event_type of PLEX_EVENT. From there you can filter out the events you care about by using event_data. In my example I filter to only a single player, my living room TVs uuid.

Thanks will give that a go
Cheers

Hi,

Thank you JBasset! for sharing this ! Tonight I tried to implement it but unfortunately it doesn’t work. Plex can’t deliver the payload to Home Assistant. I think I’ve missed something but don’t know what.

For your information, I’m running home-assistant with docker-compose.

configuration.yaml

default_config:

tts:
  - platform: google_translate

switch:
  - platform: wake_on_lan
    name: Synok
    mac_address: "blablab"
    host: 192.168.1.5
    turn_off:
      service: shell_command.turn_off_synok

shell_command:
  turn_off_synok: "ssh [email protected] -o StrictHostKeyChecking=no 'sudo shutdown -h now'"

plex_webhooks:
  webhook_id: plex_webhook

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

home-assistant.log

2019-12-18 21:03:03 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for plex_webhooks which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.

Plex Media Server.log

Dec 18, 2019 21:07:08.585 [0x7f474d6fb700] WARN - Webhook: Error delivering payload to http://192.168.1.2:8123/api/webhooks/plex_webhook: 404

http://192.168.1.2:8123 Is the base URL of Home Assistant running on my raspberry, works great. But when I go to http://192.168.1.2:8123/api/webhooks/plex_webhook :

Any idea ? Please let me know if you need more informations.

In case everyone doesn’t read all of the HA release notes in detail, the native Plex integration now uses websockets to update the sensor and media_players. This means instant updates instead of the old polling mechanism. If you were using webhooks for faster updates, give the native integration another shot.

2 Likes

Thank you jlaw, works like a charm :slight_smile:

I don’t think I need to use webhooks anymore.

1 Like

I just tried to get that working.
Also loaded the repo from @JBassett (https://github.com/JBassett/plex_webhooks) (thanks for the work) in my raspberry (HASS Host)

I stuck in step 9. Write automations, as most here in that thread :wink:

HASS UI – > Configuration --> Automations – > Add Automations --> New Automation --> Trigger

And now? Where do i have to copy which code to dim a light when Plex start playing?

Thank you

I’ve been using this method of getting the status of plex for quite some time and with the latest updates I am curious to what how the new MQTT trigger would be formatted. I would think it would be something like:

- trigger:
    mqtt:
        topic: "plex/update"

Anyone have any ideas or have gone through updating their configuration with the MQTT changes?

alias: Webhook From Plex
description: ""
trigger:
  - platform: webhook
    allowed_methods:
      - POST
    local_only: true
    webhook_id: "webhook_fingerprint"
condition:
  - condition: template
    value_template: "{{ (trigger.data.payload.decode() | from_json).event == 'library.new' }}"
action:
  - service: notify.persistent_notification
    data:
      message: |
        {{ (trigger.data.payload.decode() | from_json).event }}
         Új film a Plex-en: {{ (trigger.data.payload.decode() | from_json).Metadata.title }}
      title: Plex
  - service: script.nymnotify
    data:
      message: >
        Új film a Plex-en: {{ (trigger.data.payload.decode() |
        from_json).Metadata.title }}
mode: single