Dynamic Plex playlist in a script

Hi

I have a challenge with a plex service.
When in /developer-tools/service, my code works perfectly:

service: media_player.play_media
data:
  media_content_type: PLAYLIST
  media_content_id: "plex://{"playlist_name":"{{states('sensor.template_selected_plex_playlist_uuid')}}","shuffle":"1"}"
target:
  entity_id: {{states('sensor.template_selected_media_music_device')}}

When I copy/paste this code into a (conditional) script, it doesn’t work anymore, it gives

Message malformed: Service does not match format <domain>.<name> for dictionary value @ data['sequence'][2]['choose'][1]['sequence'][1]['service']

The full script:

alias: Media - Music Play
sequence:
  - service: media_player.media_stop
    data: {}
    target:
      entity_id: "{{ states('sensor.template_selected_media_music_device') }}"
  - choose:
      - conditions:
          - condition: state
            entity_id: input_select.media_music_source
            state: Plex
        sequence:
          - service: media_player.play_media
            data:
              media_content_type: PLAYLIST
              media_content_id: "plex://{"playlist_name":"{{states('sensor.template_selected_plex_playlist_uuid')}}","shuffle":"1"}"
            target:
              entity_id: {{states('sensor.template_selected_media_music_device')}}
      - conditions:
          - condition: state
            entity_id: input_select.media_music_source
            state: Radio Web
        sequence:
          - service: media_player.play_media
            target:
              entity_id: "{{ states('sensor.template_selected_media_music_device') }}"
            data:
              media_content_id: >-
                media-source://radio_browser/{{
                states('sensor.template_selected_webradio_uuid') }}
              media_content_type: audio/aac
mode: single
icon: mdi:music-circle-outline

Any clue?

You’re missing quotes for target’s entity_id around your template and your quoting is wrong for media_content_id. If you want to avoid quoting, use multiline yaml notation.

          - service: media_player.play_media
            data:
              media_content_type: PLAYLIST
              media_content_id: >
                plex://{"playlist_name":"{{states('sensor.template_selected_plex_playlist_uuid')}}","shuffle":"1"}
            target:
              entity_id: >
                {{states('sensor.template_selected_media_music_device')}}

the rules of quoting are simple, but often overlooked. You can’t have the same inside quotes as outside quotes. So if you use " inside, you have to use ’ outside and vice versa. Also, if your value starts with a letter, it’s assumed to be a string and you don’t need quotes.

Meaning the following would have worked for media_content_id:

              media_content_id: plex://{"playlist_name":"{{states('sensor.template_selected_plex_playlist_uuid')}}","shuffle":"1"}

or

              media_content_id: 'plex://{"playlist_name":"{{states("sensor.template_selected_plex_playlist_uuid")}}","shuffle":"1"}'

and the following would have worked for entity_id:

              entity_id: "{{states('sensor.template_selected_media_music_device')}}"

or

              entity_id: '{{states("sensor.template_selected_media_music_device")}}'
1 Like

Thank you!

Good day, can you share your “sensor.template_selected_plex_playlist_uuid” and “sensor.template_selected_media_music_device” yaml?
thank you

Hi,

Those are just variables I fill in based on a input_select field.
eg, for devices, I have an interface to select the target

input_select:
  media_music_device:
    name: Media - Music Hauts-Parleurs
    options:
      - Cuisine
      - Rez-de-chaussée
      - Maison entière
      - Chambre Enfants
      - Chambre Parents
      - Salon
      - Garage
    icon: mdi:cast-audio

With a template converting it in the right format for my script.

template:
  - sensor:

      - unique_id: "selected_media_music_device" 
        icon: "mdi:cast-audio"
        state: >-
          {% if is_state("input_select.media_music_device", "Cuisine") %} media_player.paire_cuisine
          {% elif is_state("input_select.media_music_device", "Rez-de-chaussée") %} media_player.rez_de_chaussee
          {% elif is_state("input_select.media_music_device", "Maison entière") %} media_player.maison
          {% elif is_state("input_select.media_music_device", "Chambre Enfants") %} media_player.chambre_enfants
          {% elif is_state("input_select.media_music_device", "Chambre Parents") %} media_player.mini_parents
          {% elif is_state("input_select.media_music_device", "Salon") %} media_player.paire_salon
          {% elif is_state("input_select.media_music_device", "Garage") %} media_player.garage_audio
          {% endif %}
        attributes:
          friendly_name: "Audio Media Player choisi"
          filter_key: "Music_Player"

I am using a similar principle for the Plex playlist.

@Chacsam
Thank you