[SOLVED] Find best quality video file

Hi,

I’m trying to create a template sensor that looks for the best quality in a list of YouTube videos.

The issue I’m having is that only the last item in the list is used. I’m not sure how to select the best value or end the loop once I get a result I’m happy with.
I’m thinking now that I can’t use a ‘for’ loop for this, and I need to look at the whole list without using a loop, but I’m not sure if this is correct, or where to look next.

          {% set videos = namespace(search=[]) %}
          {% set search = state_attr('sensor.rss_search_result', 'titles') %}
          {% set input = state_attr('input_text.rss_search', 'pattern') %}
          {% for item in search %}
            {%- if input in item %}
              {%- if ' 720p ' in item %}
                {% set videos.search = [item] %}
              {%- endif %}
              {%- if ' 1080p ' in item %}
                {% set videos.search = [item] %}
              {%- endif %}
              {%- if ' 2160p ' in item %}
                {% set videos.search = [item] %}
              {%- endif %}
            {%- endif %}
          {% endfor %}
          {{ videos.search }}

Heya, from what I understand you’re trying to append to an empty list you created. But then instead of appending to the list, you are replacing it with the individual item in each loop. This might be, why you are left with only the last checked item.

I have a sensor looking for the earliest time of extreme weather warning starts by adding each to a list and then returning the minimum. My approach is similar to yours but keeps the already defined list and adds each new item. Hope this helps you adjust yours successfully.

{% if states('sensor.dwd_weather_warnings_current_warning_level')|int(0) > 0 %}
	{%- set warning = namespace(start=[]) -%}
	{%- for i_warning in range(state_attr('sensor.dwd_weather_warnings_current_warning_level', 'warning_count')|int(0)) -%}
		{%- set warning.start = warning.start + [state_attr('sensor.dwd_weather_warnings_current_warning_level', 'warning_' ~ (i_warning+1) ~ '_start')] -%}
	{%- endfor -%}
	{{ min(warning.start) }}
{% else %}
	{{ '-' }}
{% endif %}

Thanks – ended up solving it like this:

{%- if this.state | lower not in ['unknown','unavailable','none','0'] %}
  {% set torrents = state_attr('sensor.rss_search_result','torrents') %}
  {% set rip_pris = {'REMUX': 4, 'Blu-Ray': 3, 'WEB-RIP': 2, 'WEB-DL': 1} %}
  {% set audio_pris = {'TrueHD': 8, 'DTS-HD': 7, 'DTS-X': 6, 'DTS': 5, 'DDP': 4, 'DD+': 4, 'DD': 3, 'AC3': 2, 'AAC': 1} %}
  {% set audio_ch_pris = {'Atmos': 4, '7.1': 3, '5.1': 2, '2.0': 1} %}
  {% set hdr_pris = {'Hybrid': 3, 'DV': 2, 'HDR': 1} %}
  {% set res_pattern = ".*?(\d{3,4}[p]).*" %}
  {% set size_pattern = "Size:(\d+(?:\.\d+)?)" %}
  {% set audio_ch_pattern = "\d\.\d|Atmos" %}
  {% set best = namespace(movie=None, resolution=0, size=0, rip_pri=0, audio_pri=0, hdr_pri=0) %}
  {% set found = namespace(audio='', audio_ch='') %}
  
  {% for torrent in torrents %}
    {% set torr_low = torrent|lower %}
    {% set current_resolution = (torr_low | regex_findall_index(res_pattern)) | regex_replace('p', '') | int %}
    {% set current_size = (torrent.size | replace(' GB','') | replace(' MB','') | float)  %}
    
    {# RIP #}
    {%- if 'remux' in torr_low %}
      {% set current_source = 'REMUX' %}
    {%- elif 'blu-ray' in torr_low or 'bluray' in torr_low %}
      {% set current_source = 'Blu-Ray' %}
    {%- elif 'web-rip' in torr_low %}
      {% set current_source = 'WEB-RIP' %}
    {%- else %}
      {% set current_source = 'WEB-DL' %}
    {%- endif %}

    {# AUDIO #}
    {% for audio in audio_pris %}
      {% if torrent | regex_search("\s{1}"+audio+"\s{1}") %}
        {% set found.audio = audio %}
      {% endif %}
    {% endfor %}
    {% set current_audio = found.audio %}

    {# HDR #}
    {%- if 'hybrid' in torr_low %}
      {% set current_hdr = 'Hybrid' if ' dv ' in torr_low and ' hdr ' in torr_low %}
    {%- elif ' dv ' in torr_low and ' hdr ' in torr_low %}
      {% set current_hdr = 'Hybrid' %}
    {%- elif ' dv ' in torr_low %}
      {% set current_hdr = 'DV' %}
    {%- elif ' hdr ' in torr_low %}
      {% set current_hdr = 'HDR' %}
    {%- else %}
      {% set current_hdr = 'none' %}
    {%- endif %}
    
    {% set current_rip_pri = rip_pris.get(current_source, 0) %}
    {% set current_audio_pri = audio_pris.get(current_audio, 0) %}
    {% set current_hdr_pri = hdr_pris.get(current_hdr, 0) %}
    {% set current_quality = current_rip_pri + current_audio_pri + current_hdr_pri %}
    {% if current_quality > best.rip_pri + best.audio_pri + best.hdr_pri %}
      {% set best.movie = torrent %}
      {% set best.resolution = current_resolution %}
      {% set best.size = current_size %}
      {% set best.rip_pri = current_rip_pri %}
      {% set best.audio_pri = current_audio_pri %}
      {% set best.hdr_pri = current_hdr_pri %}
    {% elif current_quality == best.rip_pri + best.audio_pri + best.hdr_pri and current_resolution > best.resolution and current_size > best.size %}
      {% set best.movie = torrent %}
      {% set best.resolution = current_resolution %}
      {% set best.size = current_size %}
    {% endif %}
  {% else %}
    No torrents found
  {% endfor %}
  {% if best.movie %}
    {{ best.movie }}
  {% endif %}
{% endif %}