Template help syntax for calling a service media_content_id:

I am trying to play a movie on plex that works like this:

entity_id: media_player.plex_player
media_content_type: movie
media_content_id: '{ "library_name": "Adult Movies", "title": "die hard", year=1995 }'
# Would find the sequel, "Die Hard: With a Vengeance"

and in the Developer tools Templeate
i have

{"library_name": "Movies", "title": "{{state_attr('sensor.plex_recently_added','data')[1]['title']}}", "year": "{{state_attr('sensor.plex_recently_added','data')[1]['aired'].split("-")[0]}}"}"

that gives me
{“library_name”: “Movies”, “title”: “Date With an Angel”, “year”: “1987”}"

that is exactly what i need to send in the media_content_id:
but i get an error all the time:
can not read a block mapping entry; a multiline key may not be an implicit key at line 12, column 8: style: ^

here is my config

type: 'custom:config-template-card'
entities:
  - sensor.plex_recently_movies_poster_1
card:
  type: 'custom:hui-picture-card'
  tiltle: Movie 1
  image: '${states[''sensor.plex_recently_movies_poster_1''].state }'
tap_action:
  entity_id: media_player.plex_for_samsung_tv_livingroom
  media_content_type: movie
  media_content_id: '{"library_name": "Movies", "title": "{{state_attr('sensor.plex_recently_added','data')[1]['title']}}", "year": "{{state_attr('sensor.plex_recently_added','data')[1]['aired'].split("-")[0]}}"}"'
  style:
    top: 44%
    left: 12%
    width: 13%
    border: 2px solid gold

i also have a sensor e same value if it helps:

  - platform: template
    sensors:
      plex_recently_movies_play_movie_1:
        friendly_name: "Movie 1 play button"
        icon_template: mdi:movie-roll
        value_template: >-
         "library_name": "Movies", "title": "{{states('sensor.plex_recently_movies_name_1')}}", "year": "{{states('sensor.plex_recently_movies_year_1') }}" 

Can some one help me point to my error in this, it would be alot of help, thanks!!

good day, did you find a solution/
trying to do the same.

No iI haven’t :frowning:
but if I have time this days ill put a bit of time on it, but if you find the solution share it!! :smiley:

yes, you can now do something like this:

bruno_videos:
  alias: Bruno Videos
  sequence:
  - variables:
      video: "{{states.input_select.bruno_videos.state}}"
  - service: homeassistant.turn_on
    data:
      entity_id: '{{states.sensor.media_players_map.state}}'
  - wait_template: > 
      {% if states.input_select.cast_players.state.split(' ')[0] == "Bruno" %} {{not is_state('media_player.bruno_tv_2','unavailable')}}
      {% elif states.input_select.cast_players.state.split(' ')[0] == "Sofia" %} {{not is_state('media_player.sofia_tv_2','unavailable')}}
      {% elif states.input_select.cast_players.state.split(' ')[0] == "Living" %} {{not is_state('media_player.living_tv_2','unavailable')}}
      {% else %} {{not is_state('media_player.masterroom_tv','off')}} {% endif %}
    timeout: 00:00:03
  - service: media_player.volume_set
    data:
      entity_id: '{{states.sensor.media_players_map.state}}'
      volume_level: 0.1
  - service: media_player.play_media
    data:
      entity_id: '{{states.sensor.media_players_map.state}}'
      media_content_type: movie #EPISODE PLAYLIST
      media_content_id: 'plex://{"library_name": "Bruno", "title": "{{video}}"}'

declare it as a variable at the top and then use it in the corresponding place. in my case i use the variable “video”

Hi thanks for your reply,
i didnt get how this would apply to this, i allready have a sensor for the movie that gives me wht i need to put on the media_content_id:
sensor.plex_recently_movies_play_movie_1 = “library_name”: “Movies”, “title”: “The Movie Name”, “year”: "

my problem is that tha when i put the sensor is not taking it.

Ideas?

You can’t template in Lovelace. Put that into a script and call the script. Also, remove the quotes around the json dict

Edit: also it looks like there’s a lot of errors in that configuration too.

Hi thanks for your answer,
Got it to work, and it goes like this:
first i get the plex recently added, from there i separate the info for every movie using templates like this

############################################
########### Home Cinema Sensors  ###########
############################################

  - platform: template
    sensors:
      plex_recently_movies_name_1:
        friendly_name: "Movie 1 name"
        icon_template: mdi:movie-roll
        value_template:  "{{ state_attr('sensor.plex_recently_added', 'data')[1]['title'] }}"

  - platform: template
    sensors:
      plex_recently_movies_poster_1:
        friendly_name: "Movie 1 poster"
        icon_template: mdi:movie-roll
        value_template: "{{ state_attr('sensor.plex_recently_added', 'data')[1]['poster'] }}"

  - platform: template
    sensors:
      plex_recently_movies_fanart_1:
        friendly_name: "Movie 1 fanart"
        icon_template: mdi:movie-roll
        value_template: "{{ state_attr('sensor.plex_recently_added', 'data')[1]['fanart'] }}"
        
  - platform: template
    sensors:
      plex_recently_movies_flag_1:
        friendly_name: "Movie 1 play status"
        icon_template: mdi:movie-roll
        value_template: "{{ state_attr('sensor.plex_recently_added', 'data')[1]['flag'] }}"
        
  - platform: template
    sensors:
      plex_recently_movies_year_1:
        friendly_name: "Movie 1 Year"
        icon_template: mdi:movie-roll
        value_template: "{% set text = state_attr('sensor.plex_recently_added', 'data')[1]['aired'] %}{{ text.split('-')[0].strip()}}" 
        
  - platform: template
    sensors:
      plex_recently_movies_play_movie_1:
        friendly_name: "Movie 1 play button"
        icon_template: mdi:movie-roll
        value_template: >-
         { "library_name": "Movies", "title": "{{states('sensor.plex_recently_movies_name_1')}}", "year": "{{states('sensor.plex_recently_movies_year_1') }}" }

Then i used a custom template card:

type: 'custom:config-template-card'
entities:
  - sensor.plex_recently_movies_poster_3
  - sensor.plex_recently_added
  - sensor.plex_recently_movies_play_movie_3
card:
  type: 'custom:hui-picture-card'
  tiltle: Movie 3
  image: '${states[''sensor.plex_recently_movies_poster_3''].state }'
  tap_action:
    action: call-service
    service: media_player.play_media
    service_data:
      entity_id: media_player.plex_for_samsung_tv_livingroom
      media_content_type: movie
      media_content_id: '${states[''sensor.plex_recently_movies_play_movie_3''].state  }'
    style:
      top: 44%
      left: 12%
      width: 13%
      border: 2px solid gold

It works! the only issue i have found is that all info must be there, library_name title and year.
This is one of my play movie sensors:

sensor.plex_recently_movies_play_movie_1 | { “library_name”: “Movies”, “title”: “my movie name”, “year”: “1989” }

hope it helps some one :smiley:

2 Likes