Media_player_play is it possible to use a variable inside the "media_content_id"


i try to add a dynamic title in the “media_content_id:” call for media_player_play.

i tried it with this code:

media_content_id: >-
                    [[[ return '{ "library_name":' + ' "Filme",' + ' "title": '
                    +  ' "' +[variables.i].title + '"' +

this return this :

{ “library_name”: “Filme”, “title”: “ROT”}

in the wiki stands this is the correct call

{ “library_name”: “Adult Movies”, “title”: “Blade” }

i think they are both the same (until my library name and the movie name) or?

but i always get the error :

[548209342400] Expecting value: line 1 column 5 (char 4)```

and the log says:  

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/websocket_api/”, line 192, in handle_call_service
File “/usr/src/homeassistant/homeassistant/”, line 1636, in async_call
File “/usr/src/homeassistant/homeassistant/”, line 1673, in _execute_service
await cast(Callable[[ServiceCall], Awaitable[None]],
File “/usr/src/homeassistant/homeassistant/helpers/”, line 204, in handle_service
await self.hass.helpers.service.entity_service_call(
File “/usr/src/homeassistant/homeassistant/helpers/”, line 668, in entity_service_call
future.result() # pop exception if have
File “/usr/src/homeassistant/homeassistant/helpers/”, line 931, in async_request_call
await coro
File “/usr/src/homeassistant/homeassistant/helpers/”, line 705, in _handle_entity_call
await result
File “/usr/src/homeassistant/homeassistant/components/media_player/”, line 721, in async_play_media
await self.hass.async_add_executor_job(
File “/usr/local/lib/python3.9/concurrent/futures/”, line 58, in run
result = self.fn(*self.args, **self.kwargs)
File “/usr/src/homeassistant/homeassistant/components/plex/”, line 503, in play_media
src = json.loads(media_id)
File “/usr/local/lib/python3.9/json/”, line 346, in loads
return _default_decoder.decode(s)
File “/usr/local/lib/python3.9/json/”, line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/local/lib/python3.9/json/”, line 355, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 38 (char 37)```

perhaps someone has a idea?

i missed a space at the end " }
the correct code, and the solution, perhaps someone search for it.

 [[[ return '{ "library_name":' + ' "Filme",' + '
 "title": ' +  ' "' +[variables.i].title + '" ' +   '}';  ]]]    

Please post the complete script or automation you are trying to use… it is hard to help when information is missing.

Are you asking about the media_player.play_media service call? If so you need to say which integration you are using it with, because the format of media_content_id is integration dependent. For example, you can provide URLs to Sonos and Cast but only a playlist ID to iTunes. Also, that is not the right templating format for use in service calls.

1 Like

you are right, its the “media_player.play_media” call, in combination with the plex server.

but i found the problem a post above, i missed a space at the end… :grimacing: