Templating: Youtube link strip from mobile or PC

OK, I am trying to see if this is possible, but I couldnt find anything. Before I give up, I thought I should ask the templating gurus.

I have Kodi on my tv and youtube addon. In my HA interface I have an input_text where I can paste a link from youtube and I have a simple template to strip the url and keep only the necessary part for the addon to play.
Here is what it looks like:

  - data_template:
      entity_id: media_player.tv
      media_content_id: plugin://plugin.video.youtube/play/?video_id={{states.input_text.youtubelink.state.split('=')[1]}}
      media_content_type: video
    service: media_player.play_media

So, from this link https://www.youtube.com/watch?v=D86RtevtfrA, it will only keep D86RtevtfrA.
Now, please bare with me… What I want to achieve is to have the possibility to do the same with a youtube app from a phone.
The link looks like this: https://youtu.be/D86RtevtfrA
So, if I wanted to strip this, I would probably go with this:


and it would do the work.

I want to paste the link into only one text_input field. That means I dont want to create a second one.
Can templating do this kinda stuff? Can I just paste a link and somehow knows how to split it? Or can I have 2 templates into one and just expect the one or the other link? I dont care how much time this will take to make it work. I just want it to be smart.
Oh, boy… If this works, I may get some kudos from the wife!

So if I understand, the input_text will have one or the other URL format you described, and you want a template that will extract the last part no matter which format the URL is. Is that right?

How about:

      media_content_id: >
        {% set link = states('input_text.youtubelink') %}
        {{- link.split('=' if '=' in link else 'be/')[1] }}

This works like a charm! Thank you very much for this!

I can see what you did there with the if statement. This was exactly in my mind but I had no idea how to do this with jinja. Can you please explain to me the whole template if it is not much trouble?

for example why did you have to SET the link at the start? and also why the dash at the beginning of the split template?
The documentation of jinja is way too large to learn and remember all these stuff! I need to learn the most important ones! Any recommendations of the most basic rules to read?

No problem.

First, you can find information about multi-line YAML here.

I set the link variable just to make the template a bit smaller/cleaner/easier-to-read. Also in programming, when you need to use the value of something that might be changing at the same time, it’s better to make a copy and use that copy throughout so different pieces of the template don’t have any chance of seeing different values. So it’s a win-win!

The dash at the beginning (or end) of {% ... %} or {{ ... }} means remove all whitespace in that direction. So using it at the beginning of the {{ expression removes all the spaces between the result of the expression and what comes before it (i.e., all the way back to the equal sign on the previous line.)

The if statement is documented here. It’s pretty much the same as in Python. In fact, Python can be used quite a bit in Jinja templates.

1 Like