Plex Webhook Handler

I tried to download the app Aspect Ratio Detector, i tried to register for the website but I still cannot gain access… having time to play around, how about something a little bit out of the box :slight_smile:

Within the your configuration.yaml file place this:

rest_command:
  plex_get_aspect_ratio:
    url: "http://<plex_server_ip>:32400/library/metadata/{{ rating_key }}?X-Plex-Token=<plex_token_id>"
    method: GET
    headers:
      Accept: "application/json"
	  

(changing the plex_server_ip and the plex_token_id)
Reboot your home assistant instance. This will allow a rest command to be sent to your plex instance

then you could use these in the generic playback or movie/tv show playback (which trigger for a certain player). placing this under Generic means every player that plays media wil trigger the rest command.

retrieve a bit more information about the media than what is sent within the webhook.

action: rest_command.plex_get_aspect_ratio
metadata: {}
data:
  rating_key: "{{ payload.Metadata.ratingKey }}"
response_variable: plex_aspect_response

then, extract the ratio information from the data and hold it as a variable (plex_aspect_ratio)

variables:
  plex_aspect_ratio: >-
    {{
    plex_aspect_response.content.MediaContainer.Metadata[0].Media[0].aspectRatio
    }}

finally using the plex_aspect_ratio variable, use it within a choose option as needed:

choose:
  - conditions:
      - condition: template
        value_template: "{{ plex_aspect_ratio == 1.85 }}"
    sequence:
      - action: persistent_notification.create
        metadata: {}
        data:
          message: "aspect ratio: 1.85"
          title: "Ratio: 1.85"
  - conditions:
      - condition: template
        value_template: "{{ plex_aspect_ratio == 2.35 }}"
    sequence:
      - action: persistent_notification.create
        metadata: {}
        data:
          message: "aspect ratio: 2.35"
          title: "Ratio: 2.35"
  - conditions:
      - condition: template
        value_template: "{{ plex_aspect_ratio == 1.78 }}"
    sequence:
      - action: persistent_notification.create
        metadata: {}
        data:
          message: "aspect ratio: 1.78"
          title: "Ratio: 1.78"

if you look in the trace and the changed variables from the rest command there is a lot of information about the media.

@modem7 you could also use this (with a bit of tweaking) to obtain the audio details, as the rest command does return more information about the stream.

variables:
  plex_audio_codec: >-
    {{
    plex_aspect_response.content.MediaContainer.Metadata[0].Media[0].Part[0].Stream[1].codec
    }}
  plex_audio_channels: >-
    {{
    plex_aspect_response.content.MediaContainer.Metadata[0].Media[0].Part[0].Stream[1].channels
    }}
  plex_audio_layout: >-
    {{
    plex_aspect_response.content.MediaContainer.Metadata[0].Media[0].Part[0].Stream[1].audioChannelLayout
    }}

this returns:
plex_audio_codec: ac3
plex_audio_channels: 6
plex_audio_layout: 5.1(side)

Awesome blueprint! It’d be even better if you could also add a configuration to run automations based on a time condition. Sunset to sunrise or the like as I’m sure many will be using this with their lighting in open areas.

I did just add it manually with a block if statement and that works!

1 Like

Thank you.

I suppose I never really thought about adding time conditions to it. I am doing the same as yourself for some of the actions and just defining the time condition within the actions manually.

Hey @kncowans,

I see you have removed the post but if I can help then please let me know and I will do my upmost to asssist.

Hello

Thanks, the Issue I had was a mistake on my Part.

I entered the Full HTTP Address where the Blueprint asks for the Webhook ID, once I figured out that it should just be the Webhook ID everything started working.

One Question, if I may.

Will you be adding the ability to only Run the Automations according to the Sun, i.e. only After Sunset?

This will allow Automations to Setup the Lights if Playing a Movie or TV Show after Sunset, Setting the Lights is not required during the Day.

Thanks for the Blueprint, it is working Great.

Kevin

Glad you got it working. I have been meaning to look more in to this since hekenyancarot mentioned it. I might have a play around with the beta version to see what works.

I am wondering if each media event, movie, track or episode should have its own time setting.

At the moment I run my movie play actions with:

if:
  - condition: state
    entity_id: light.living_room
    state: "on"
then:
  - data:
      transition: 5
    target:
      entity_id: scene.livingroom_plex_playing
    action: scene.turn_on

It just checks to see if the living room lights are on or not, my living room lights are set with a different automation to go on at sunset.

could use something similar:

if:
  - condition: sun
    before: sunrise
    after: sunset
then:
  - data:
      transition: 5
    target:
      entity_id: scene.livingroom_plex_playing
    action: scene.turn_on

Hello

Good to hear that you are looking into it.

I think it would be best for it to be a Setting for each, i.e. Movie, TV Show and Music.

During the Day, before Sunset, I do not alter the Lighting, however, after Sunset I Switch Off the Living Room Lights and Switch On the TV Led Lights (Hue Play Bars), for Movies and TV Shows, for Music I do not change the Lighting.

Thanks

Kevin

I have not had time to fully test this in my Dev Environment, it very much is bleeding edge.

Added seperate time constraints for movie, music, and television actions, and included additional requirements for the Sun integration. The time constraints need to be enabled for it to obey them, otherwise the actions run 24/7. The Sunset/Sunrise is using the state of ‘below_horizon’ from sun.sun

Once I have time to run it through my Dev Environment and test for as many situations as possible I will look to upload it to production.

so bleeding edge that I have discovered a logic issue with times that go over the midnight hour… That is now been addressed in Beta 5.1, which has just been comitted to GitHub.

Hello

I am relatively new to Home Assistant, will the Blueprint AutoUpdate?

If not, do I just go to the GitHub Page and Download it again?

Thanks

Kevin

When I commit to to the production version then you can go to settings > Automations >Blueprints > then go to the options beside the blueprint (3 vertical dots) then click on Re-Import blueprint. This will update all automations that use the blueprint.

If you wish to test this beta version, before I commit it to production then go to the same blueprint page as above but go to Import blueprint in the bottom right hand corner and paste in the url:

https://github.com/thenextbutton/home_assistant/blob/ba436830c6c9dbd5b4225a48ee2a7f7692a23efb/blueprints/plex_webhook_handler/plex_webhook_beta.yaml

you will need to create a new automation from it to test.

I started to test it today in my dev instance but haven’t yet completed testing.

Hello

Thanks for the Reply.

Quick Question, if I may.

When Updating Blueprints, will the Created Automations be Deleted, or, will they be Updated?

Still Learning :slight_smile:

Thanks

Kevin

normally updated.

Hello

Thanks

Kevin

@kncowans, I have done some further testing and have updated the release version of the blueprint. I am not seeing a problem with the changes.

I have mimic’d the same time constraint options for Movies, Music and TV.

The constraint for the media type must be enabled, to start off with. if they are not then all options are ignored and the actions run 24/7

If sunset/sunrise is enabled then the automation will obey this, ignoring Helper and Manual Time Fallback options.

If sunset/sunrise is not enabled and the helper is set, it ignore the Manual Time Fallback options.

If sunset/sunrise and the helper is not set then it will use the manual time fallback settings.

please go to settings > automations & scenes > blueprints

find the Plex Webhook Handler RELEASE and click on the three dots on the right hand side and select the option to re-import the blueprint, it will jump the version up to v0.5.2

I have done mine and all looks to be intact.

@thekenyancarot hope this is of some help to you as well.

Hello

I have been using 0.5.2 since the Release, and, it is working perfectly.

Thanks

Kevin

1 Like