Plex template conditions?

Hi everyone,

I’m new to Home Assistant and am after a bit of guidance. I’ve been reading through the wiki and trying to work this out but it’s eluding me.

Basically what I’m trying to create is for the living room light to switch off when I begin a Plex movie on either my living room TV (Android TV app) or living room ChromeCast. I then want the light to come back up to ~25% brightness when the movie finishes or is paused. Finally I only want this to happen after sunset (as why would I want my lights coming up during the day?).

I currently achieve this using Plex’s built-in webhook, which sends playback information in a JSON payload to a PHP script I’ve written, which does the relevant checks (client ID, media type, and time of day). If the checks pass the relevant command is then sent as a webhook to IFTTT, which then either switches off or on the light. It’s become apparent that this could all be replaced by Home Assistant, and would also be kept local (reducing points of failure and latency).

So far I’ve got everything recognised on HA, and can successfully use it’s webhooks in place of IFTTT to keep things local, however would be interested in replacing the Plex webhook and my PHP script with HA’s built-in functionality.

I’m able to bind an automation event to the relevant Plex clients state change, however I’m struggling with the conditions. How specifically do I extract and check the attribute data of a given device in HA? Ideally I’d need to check “media_content_type” from the relevant device to ensure the content being played back is a movie. I’ll also need to use this further when isolating ChromeCast streams, but I suspect doing so will be much simpler once I crack how to use attribute data as a condition.

From what I’ve read on the wiki a template condition of the following should work:
value_template: "state_attr('media_player.plex_plex_for_android_tv_living_room_tv', 'media_content_type') == 'movie'"
But it doesn’t. Can anyone shed any light as to what I’m missing here?

Thanks,
Dean

OK So playing around in the dev tools -> templates section allows me to see that:

{{ state_attr('media_player.plex_plex_for_android_tv_living_room_tv', 'media_content_type') == 'movie'}}

enumerates correctly to either true or false as expected, however when I insert this as a condition in my automation as such:

  condition:
  - condition: template
    value_template: "{{ state_attr('media_player.plex_plex_for_android_tv_living_room_tv', 'media_content_type') == 'movie'}}"

the action fires regardless of media_content_type (so it’s always enumerating to true somehow).

Can anyone see what I’m missing?

I have this same setup, except I use that as my trigger. My condition is whether the lights are already on. What trigger are you using?

maybe switch your double and single quotes? The following works fine for me.

  - condition: template
    value_template: '{{ state_attr("media_player.plex_plex_for_android_tv_shield_android_tv", "media_content_type") == "movie" }}'

Thanks for the idea. Switching the quotes had no effect though. No idea why it’d work for you and not for me. Does make me wonder if I’m going wrong elsewhere in the automation- as the template does seem to enumerate correctly in the dev tools -> template area.

I’m using the media player’s state changing to “playing” as the trigger. Don’t suppose you could share your automation yaml by any chance? So I could see the other approach.

Maybe post your full automation so we can take a look.

I’m currently using a different player and different light for testing- as they’re easier to control directly from my PC. As it stands regardless of the “media_content_type” it’s still activating the light.

- id: '1586632307631'
  alias: tv_play_hass
  description: ''
  trigger:
  - entity_id: media_player.plex_plex_for_windows_desky
    platform: state
    to: playing
  condition:
  - condition: template
    value_template: '{{ state_attr("media_player.plex_plex_for_windows_desky", "media_content_type") == "movie" }}'
  action:
  - device_id: 68fd91b2702e46af9c74324287328362
    domain: switch
    entity_id: switch.strobe
    type: turn_on

I actually took a multiple step approach.

First a sensor:

- platform: template
  sensors:
    plexmediatype:
      value_template: "{{state_attr('media_player.plex_plex_for_lg_lg_49uj6300_ua','app_name')}}"

Then an automation:

- id: Media_Plex_Dim_Movie_Play
  alias: Media Plex Dim Movie Play
  trigger:
  - entity_id: sensor.plexmediatype
    platform: state
    to: Movies
  condition:
  - condition: state
    entity_id: light.living_room
    state: 'on'
  action:
  - data:
      entity_id: script.lr_movie_dim
    service: script.turn_on

Then a script to set the lights.

lr_movie_dim:
  alias: LR Dim
  sequence:
  - entity_id: light.living_room_lamps
    service: light.turn_off
  - data:
      brightness: 135
      entity_id: light.tv_light_strip
      rgb_color:
      - 255
      - 129
      - 40
      transition: 3
    service: light.turn_on
  - data:
      brightness: 70
      effect: Solid
      rgb_color:
      - 255
      - 166
      - 88
      transition: 5
    entity_id: light.wled_lr
    service: light.turn_on

I have other automatons and scripts for pause, resume, and stop.

Those Pause and Resume trigger off of the media player state change, while the stop automation triggers off of the sensor state change.