Looking for way to search string for integers

I have a template sensor that returns content types based on the media_title attribute of my Nvidia Shield TV Pro.

A typical TV Show state is:

media_title: Severance - S01E09 - The We We Are

and a Movie state would be:

media_title: Godzilla Minus One (2023)

Here is the Tempate Sensor I came up with:

{% if state_attr('media_player.shield', 'app_name') == 'SmartTube' %}
  youtube
{% endif %}

{% if ' (' in state_attr('media_player.shield', 'media_title') and state_attr('media_player.shield', 'app_name') != 'SmartTube' %}
  movie
{% elif ' - S0' in state_attr('media_player.shield', 'media_title') and state_attr('media_player.shield', 'app_name') != 'SmartTube' %}
  tvshow
{% endif %}

The problem is that in it’s current form if i am watching a TV Show with more than 9 Seasons, the sensor does not recognise it due to the ' - S0' section. If i change it to ' - S' the problem becomes when an epidode starts with the letter S.

I have tried asking Google Gemini for help with the sensor and it seems to think i can use ' - S\d\dE\d\d' which would search the string for S and then any two numbers, then E followed by any two numbers. Is this possible? or anything like it?

Does anyone have any ideas how i can make home assistant recognise if i am watching a TV Show even if there are more than 9 Seasons or if the episode starts with the letter S?

First time posting here. I hope i have made myself clear and appreciate anyone’s time looking at this.

{% set mt = state_attr('media_player.shield', 'media_title') %}
{% if state_attr('media_player.shield', 'app_name') == 'SmartTube' %}
  youtube
{% elif mt is search('\(\d+\)') %}
  movie
{% elif mt is search('S\d+E\d+') %}
  tvshow
{% endif %}

NOTE

You should also include a final {% else %} to handle the possibility where none of the previous conditions were met.

{% set mt = state_attr('media_player.shield', 'media_title') %}
{% if state_attr('media_player.shield', 'app_name') == 'SmartTube' %}
  youtube
{% elif mt is search('\(\d+\)') %}
  movie
{% elif mt is search('S\d+E\d+') %}
  tvshow
{% else %}
  unknown
{% endif %}
1 Like

If only you knew how many hours I have been searching for an answer like this. Thank you

2 Likes

hint hint There’s a heart button under each post, to show additional appreciation, a “like” if you will… :wink: :laughing:

Gladly :grin:. I wonder if you could help me with another piece of the puzzle? The sensor is part of my conditional dashboard media players. The episode title has a similar issue:

label: >
[[[return
<marquee>${states['media_player.shield'].attributes.media_title.split(' - S')[1].split(' - ')[1]} <\marquee>]]]

:raising_hand_man:t3: Never mind, figured it out :grin:

1 Like