❱ Plex Assistant

Just using the example from here

Maybe something like this? I don’t have a Roku so I couldn’t tell you for sure.

automation:
  - alias: Plex Assistant Automation
    trigger:
    - event_data:
        action: call_service
      event_type: ifttt_webhook_received
      platform: event
    condition:
      condition: template
      value_template: "{{ trigger.event.data.service == 'plex_assistant.command' }}"
    action:
      - data:
          entity_id: media_player.roku
          source: "Plex"
        service: media_player.select_source
      - data:
          command: "{{ trigger.event.data.command }}"
        service: '{{ trigger.event.data.service }}'

IFTTT sends no identifying information like which device sent the command, so if you have multiple devices you could try using a jinja template that either looks for the name of the device in trigger.event.data.command or the absence of any device names to use the default device.

so it’s something like this:

  action:
  - service: media_player.select_source
    data:
      source: Plex - Stream for Free
    entity_id: media_player.living_room
  - service: plex_assistant.command
    data:
      command: update sensor
  - data_template:
      command: '{{ trigger.event.data.command }}'
    service_template: '{{ trigger.event.data.service }}'

but if I want it to work on media_player.kitchen_tv or media_player.basement etc, I gotta parse trigger.event.data.command and figure out how to call the correct entity. I’ll have to dig into what a jinja template is. Also looks like I’ll have to add some delay in after the update sensor command. Not sure if it’s worth the hassle but I think I can make it work. I have it launching plex and it plays the selection on a hard coded device, but sometimes it sits at the plex screen if the plex client isn’t registered yet.

Yeah, unfortunately plex clients aren’t advertised unless they are open and the time that plex recognizes them depends on a multitude of factors. Doing this automatically won’t be the easiest thing and may not always work, but other than opening the plex app on the device yourself it’s the only option.

I’ve managed to get this working nicely but it’s for 1 device specifically. I created a sensor to check if plex can see my living room roku as an available player

      plex_on_livingroom:
        value_template: >
          {% if states.sensor.plex_assistant_devices.attributes['Connected Devices']['Plex Clients'][0]['Living room'] %} 
            true
          {% else %}
            false
          {% endif %}

Then have the plex assistant action keep checking the value of that sensor and running update sensor until it is true before having plex assistant do it’s magic.

  action:
  - service: media_player.select_source
    data:
      source: Plex - Stream for Free
    entity_id: media_player.living_room
  - repeat:
      until:
      - condition: state
        entity_id: sensor.plex_on_livingroom
        state: 'true'
      sequence:
      - service: plex_assistant.command
        data:
          command: update sensor
      - delay: 2s
  - data_template:
      command: '{{ trigger.event.data.command }}'
    service_template: '{{ trigger.event.data.service }}'

edit- well this worked until I got more plex clients and my living room could show up at [1] or [2],etc. I need to fix my sensor but I don’t know how to search through the different attributes.

edit2 - I cut it off at [‘Plex Clients’] then pipe to string then pipe to regex search and it’s working

Hello @mayker,

This is really interesting! I was longing for Plex to support this out of the box, but thank you for doing this yourself!

I’m having issues in setting this up though.
I followed the guide, configured as a DialogFlow integration, but I always get the error “this intent is not yet configured in Home Assistant” and in home assistant itself I see in the log an error [homeassistant.components.dialogflow] Unknown intent Plex

The weird thing I noticed is that, in the DialogFlow console, when I put the intent “Plex” as the implicit one, it’s not get saved. It only show “Default Welcome Intent” in the drop down.
Explicit Invocation looks ok instead.

Do you know what I could be doing wrong?

Thank you for your time!

EDIT:

This is my setup in the yaml:

plex_assistant:
  url: !secret aegis_plex
  token: !secret aegis_token
  default_cast: 'TV'
  language: 'it'
  tts_errors: true
  aliases:
    TV: !secret aegis_lgtv_id

sensor:
- platform: plex_assistant


conversation:
  intents:
    PlexAssistant:
      - "Di a Plex di {command}"
      - "{command} con Plex"
      - "{command} su Plex"

intent_script:
  PlexAssistant:
    speech:
      text: "Comando inviato a Plex."
    action:
      service: plex_assistant.command
      data_template:
        command: "{{command}}"

Edit 2:
I managed to progress a bit more by creating an intent script for Plex, not PlexAssistant as the guide states.
I still get an error though in the logs
Error handling Plex and Error executing script: connection refused

I’m connecting to the local address of the server, and ssl is not required for local network connections, so I don’t know why this happens.

To be fair the intent was always named “plex” in the readme for DialogFlow, but in the “Home Assistant Conversation Setup” it was named “PlexAssistant”. I updated the readme to avoid confusion.

What is the full error you are receiving? Click on it to expand if you need to.

As far as DialogFlow setup, if that is where the errors are happening (in the DialogFlow component and not Plex Assistant), it can be finicky and should only be used if your language isn’t supported by IFTTT. Best advice I can give is to follow the instructions exactly, no more, no less.

Thank you for your reply!

I honestly don’t like IFTTT, that’s why I went Dialogflow from the get go.

I believe the issue is on Home Assistant side though, this is the error

Intent Script Plex: Error executing script. 
Unexpected error for call_service at pos 1: 
HTTPConnectionPool(host='127.0.0.1', port=32400): 
Max retries exceeded with url: /player/playback/playMedia?address=

(address and command removed)

and this error

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 169, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.8/site-packages/urllib3/util/connection.py", line 96, in create_connection
    raise err
  File "/usr/local/lib/python3.8/site-packages/urllib3/util/connection.py", line 86, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

I will try using the public address provided by plex, even though I have ssl on preferred, and not required for local network.

Okay, yeah, just unable to connect to Plex. Instead of using 127.0.0.1 have you tried the IP address of the machine?

The IP is the right one: I’m not using localhost but the right address from the server. It’s a weird error indeed.
Plex runs on a Shield, it’s in a separate machine from where HA runs.

EDIT: the address I removed was showing the right IP. I was just wondering if some private info were there

Using a browser on a different machine, can you connect to your Plex server’s API? Adding your info in the URL below, do you see anything?

http://[IP]:[PORT]/?X-Plex-Token=[TOKEN]

If not, there are connection issues unrelated to Plex Assistant.

Yes, it works.

I also tried enabling IFTTT right now, but I have the same error:

Plex Assistant Automation: 
Error executing script. Unexpected error for call_service at pos 1: HTTPConnectionPool(host='127.0.0.1', port=32400): 
Max retries exceeded with url: /player/playback/playMedia?address=192.168.1.47&commandID=1&containerKey=%2FplayQueues%2F2866%3Fwindow%3D100%26own%3D1&key=%2Flibrary%2Fmetadata%2F3212

This time I only removed the machine identifier and transient token. The ip in the call is the one where Plex is running.

EDIT:
I don’t think the issue is in dialogflow or IFTTT, as they both reply something, but it’s HA not able to run the script it seems.

I get the same error if I invoke the plex_assistant.command service from HA.

EDIT2:

I’ll try using the actual chromecast device (Shield) instead of the client of my smart TV. But I don’t know if this matters tbh.

Ok, some steps forward. Probably the issue was the client in my LG Tv.

I tried with the Cast device, and it actually started something, but it’s not the “real” plex client, it’s using the Shield as a chromecast device.

The sensor.plex_assistant is not picking up the actual Plex client running on the Shield. I’ve tried waiting, while playing some content, but it never shows up. Web Clients are shown.

I will probably try again setting back DialogFlow again with the cast device, but the chance to use the real Plex client would be much better.

Is there anything that I can do to help troubleshooting this issue?

Thank you for your time!

If you don’t see the Shield Plex client reporting playback progress when controlling it via the API, it may be related to this known Plex client issue: https://forums.plex.tv/t/no-session-status-when-starting-playback-on-shield-over-remote-control-api/555067.

It’s not that. Plex assistant has a sensor that reports plex client. That wasn’t working in a timely fashion.

So is this about finding the Plex client to control? Is the Shield on a different subnet than the Plex server?

All the sensor is doing is using Plex API to poll your Plex server for available clients. The sensor is updated manually and can only see what is reported by Plex. Plex Assistant also polls Plex for clients on every command.

Thank you both for your inputs.

Eventually the ID for the Plex Client running on the shield showed up. I was able to send some commands to it through Plex Assistant, but it’s not working well - I imagine mostly due to the language used, that even if supported by DialogFlow, is not working that well.
I also set the DialogFlow to work in English, but again the results are inconsistent.

Thank you again for your time.

Hi, I’ve done everything but I still can’t update sensor.
In HA’s “Developer Tools”, Status, this is the entity:

sensor.plex_assistant_devices

Connected Devices:
  Cast Devices: None
  Plex Clients: None
friendly_name: Plex Assistant Devices

This is not supposed to happened, right? I’m totally lost.

Whenever I tell google assistant to play something, it says “ok, starting the test app” - then my app responds with “ok, what is the command?”. I thought I could just say the damn thing once instead of twice. What am I doing wrong?

@diogo.rg Not sure why it isn’t finding your devices, could be they are on a different subnet? The next version of Plex Assistant will use media_player entities instead of searching for devices thanks to some upcoming changes in HA and PlexAPI.

@ciB DialogFlow is finicky and unless you need to use it (your language isn’t supported by IFTTT) then use IFTTT. Otherwise, delete the DialogFlow agent and start over, follow the directions and don’t do anything extra. Exit out of the DialogFlow window after following the directions.