Template: part of string to input_text

Hi everybody,

I am trying to create a template_sensor entity that will save the most recent audio book episode that we have been listening to.

{% if "Sinclair" in state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
  {{ state_attr("media_player.mpd_schlafzimmer", "media_content_id") }}
{% endif %}

The if statement is only there to determine whether a particular audio book series is playing (in this case, “John Sinclair”). The snippet above will display Hörspiele/John Sinclair 2000/F001 Im Nachtclub der Vampire/01 Teds Spiel.mp3. This is the output I expected. When I change to something not containing “Sinclair”, it will display nothing.

However, I would like to have my template sensor display the following:

  • state=F001 Im Nachtclub der Vampire
  • episode=F001
  • title=Im Nachtclub der Vampire

jinja2 is a little confusing to me. I know a bit of regex, but I don’t know how to apply it here. F\d{3} should match the value I want for episode and F\d{3}.*\/ what I’d like to have for state (except this would have a trailing /, which would have to be removed).

Can you please tell me what I’d need for the actual template_sensor yaml in order to extract these values? Thank you in advance for your help :slight_smile:

1 Like

Copy-paste this into the Template Editor and experiment with it to understand how it works.

{% set mci = 'Hörspiele/John Sinclair 2000/F001 Im Nachtclub der Vampire/01 Teds Spiel.mp3' %}
{% set x = mci.split('/')[2] %}
{% set y = x.split(' ', 1) %}
state={{ x }}
episode={{ y[0] }}
title={{ y[1] }}
2 Likes

Thank you so much! This works perfectly.

template:
  - sensor:
      - name: "John Sinclair"
        state: >
          {% set mci = state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
          {% set x = mci.split('/')[2] %}
          {% set y = x.split(' ', 1) %}
          {{ x }}
        attributes:
          Folge: >
            {% set mci = state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
            {% set x = mci.split('/')[2] %}
            {% set y = x.split(' ', 1) %}
            {{ y[0] }}
          Titel: >
            {% set mci = state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
            {% set x = mci.split('/')[2] %}
            {% set y = x.split(' ', 1) %}
            {{ y[1] }}

I got to revive this, because I realized there is a problem: this sensor will update any time media_player.mpd_schlafzimmer changes the media_conent_id attribute; as per this template, that is correct. However, I want this sensor only to update when John Sinlair is playing.

So I changed to the snippet below. Now it should only trigger if media_artist matches; however, it will still always change the sensor value when anything changes about media_player.mpd_schlafzimmer. But if there is something other than “John Sinclair 2000” playing, it will set empty values for the sensor and its attributes.

What do I need to change? Let’s say I am currently listening to John Sinclair 2000/F001 Im Nachtclub der Vampire; the template sensor will display the correct value. This value should stay until I play something else by “John Sinclair 2000”. So if I play Misftis/12 Hits From Hell/Some Song.mp3, I want the sensor to stay the way it is (with the value that is not the current playback value any longer, but the most recent playback of “John Sinclair 2000”) until I play another episode. I hope I explained this understandably… the template_sensor is supposed to keep track of what episode of this play we listened to last, not of what is currently playing (unless it happens to also be by “John Sinclair 2000”). This way, if we don’t remember where we left off, we can look at the sensor and start the next episode accordingly…

template:
  - trigger:
      - platform: template
        value_template:  "{{ state_attr('media_player.mpd_schlafzimmer', 'media_artist') == 'John Sinclair 2000' }}"
  - sensor:
      - name: "John Sinclair"
        state: >
          {% if state_attr("media_player.mpd_schlafzimmer", "media_artist") == "John Sinclair 2000" %}
            {% set mci = state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
            {% set x = mci.split('/')[2] %}
            {% set y = x.split(' ', 1) %}
            {{ x }}
          {% endif %}
        attributes:
          Folge: >
            {% if state_attr("media_player.mpd_schlafzimmer", "media_artist") == "John Sinclair 2000" %}
              {% set mci = state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
              {% set x = mci.split('/')[2] %}
              {% set y = x.split(' ', 1) %}
              {{ y[0] }}
            {% endif %}
          Titel: >
            {% if state_attr("media_player.mpd_schlafzimmer", "media_artist") == "John Sinclair 2000" %}
              {% set mci = state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
              {% set x = mci.split('/')[2] %}
              {% set y = x.split(' ', 1) %}
              {{ y[1] }}
            {% endif %}
        picture: >
          {% if state_attr("media_player.mpd_schlafzimmer", "media_artist") == "John Sinclair 2000" %}
            {% set hass = "url:8123" %}
            {% set img = state_attr("media_player.mpd_schlafzimmer", "entity_picture") %}
            {{ hass }}{{ img }}
          {% endif %}