Turn on lights when plex credits roll

Hi everyone,

in my growing quest to make my home act just like a movie theater. after hours of trying I got my lights to turn on right when the credits roll. This is perfect moment to discus the movie with my girlfriend or just sit and ponder.
Sensors needed down below because I saw some people online asking this question a few years ago and nobody seems to have a way to do it.

command_line:
  - sensor:
      name: Plex_Media_ID
      command: "curl -s 'http://<Plex_IP>:32400/status/sessions?X-Plex-Token=<Plex_Token>' | grep -o 'key=\"/library/metadata/[0-9]*' | head -n 1 | sed 's/key=\"//' | sed 's/\"$//'"
      scan_interval: 10
      value_template: "{{ value }}"
  - sensor:
      name: Plex_Credit_Chapters
      command: >
          curl -s GET "http://<Plex_IP>:32400{{ states('sensor.plex_media_id') }}?X-Plex-Token=<Plex_Token>&includeChapters=1&includeMarkers=1" |
          grep -o '<Marker[^>]*type="credits"[^>]*>' |
          head -n 1 |
          sed -n 's/.*startTimeOffset="\([^"]*\).*/\1/p'
      scan_interval: 10
      value_template: "{{ value }}"
  - sensor:
      name: Plex_Position
      command: curl -s 'http://<Plex_IP>:32400/status/sessions?X-Plex-Token=<Plex_Token>' | grep -o 'viewOffset="[0-9]*"' | sed 's/viewOffset="//' | sed 's/"//'
      scan_interval: 10
      value_template: "{{ value }}"

first sensor is needed so we can actually see what is being played on the session. If you don’t know your plex token check out this from plex’s website
This first sensor basically gets the media ID so we can grab the actual info that we need to see when the credits roll. second sensor grabs the credit marker. this is the point in the movie/show where the credits start rolling. I then tried to compare that to the play position in the original plex integration but that did not update quickly enough for me to actually use it so now we have to make an api call every 10 seconds to get the position (that’s what the last sensor is for.) then for an automation the just check if the position is above the credit marker, if so we turn on the lights.
You could probably update the last API call a lot more but I did not want to risk overloading my plex server with API calls and I didn’t mind the 10 seconds it sometimes takes to turn on the lights (my home town movie theater has about the same delay so I kind of like it).

Would also probably be smart to only update the sensors when your mediaplayer detects when it is playing plex.
could probably do it with the restful integration but I was more comfortable using curl commands

3 Likes

I don’t have Plex, but is there a media_position attribute in the media_player entity? It shows the number of seconds since the stream started.

Also the media id may be available as an attribute.

As an example, this is all the attributes of my Kodi player while starting a movie (4 seconds in)

friendly_name: n2
supported_features: 186303
dynamic_range: sdr
volume_level: 1
is_volume_muted: false
media_content_id:
  unknown: tt4703048
media_content_type: movie
media_duration: 5674
media_position: 4
media_position_updated_at: "2025-01-18T03:15:28.047044+00:00"
media_title: 6 Days
media_album_name: ""
media_series_title: ""
media_season: -1
media_episode: -1
entity_picture: >-
  /api/media_player_proxy/media_player.n2?token=c666b455f71c824100e3c1bcffedb103da03e4869d32b01f1887ab52f5d6a9c6&cache=7540ad50b8d17f80


Hello, I am a very novice user, but I would like to create the same, this code that you show would have to be put inside the yaml of a new automation or what would the process be like? Thank you very much in advance.

It’s not an automation, it’s a command line sensor. Command line - Home Assistant

Ok, thanks for clarifying the doubt. So if I put this code in my config.yaml file and enter my Plex data, should it work on its own?

I asked the question about automation above. For this to work, turning on the lights when credits are detected, would we have to somehow call it from an automation system? Or am I mistaken?

Regards.

Yes you’ll need an automation to actually turn the lights on.

Thank you, I’m a very novice user, that’s the question, how do I call this sensor from an automation? I’m trying but I can’t see any of these sensors created in the code lines. Thank you.

I already have the sensor here, but I don’t know if I’m using the correct triggers, in this case a number to verify the position of the credit marker. Thank you in advance.