Help by a loop

Hello,

i try to get only the values from a sensor where the “flag” attribute is true.

{% for i in states.sensor.plex_recently_added_movies.attributes.data %}
 {{ i }}
  

{% endfor %}

with this code i get all the values

{'title_default': '$title', 'line1_default': '$episode', 'line2_default': '$release', 'line3_default': '$number - $rating - $runtime', 'line4_default': '$genres', 'icon': 'mdi:eye-off'}
  


 {'airdate': '2022-05-17T18:56:22Z', 'aired': '2022-02-04', 'release': '$day, $date $time', 'flag': True, 'title': 'Moonfall', 'episode': '', 'runtime': 130, 'studio': 'Centropolis Entertainment', 'genres': 'Action, Adventure', 'rating': '★ 3.7', 'poster': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/p10335.jpg', 'fanart': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/f10335.jpg'}
  


 {'airdate': '2022-05-15T18:56:55Z', 'aired': '2022-04-01', 'release': '$day, $date $time', 'flag': True, 'title': 'Morbius', 'episode': '', 'runtime': 103, 'studio': 'Columbia Pictures', 'genres': 'Horror, Science Fiction', 'rating': '★ 1.7', 'poster': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/p10302.jpg', 'fanart': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/f10302.jpg'}
  


 {'airdate': '2022-05-15T18:56:55Z', 'aired': '2022-05-12', 'release': '$day, $date $time', 'flag': True, 'title': 'Firestarter', 'episode': '', 'runtime': 94, 'studio': 'Blumhouse Productions', 'genres': 'Drama, Horror', 'rating': '★ 1.3', 'poster': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/p10303.jpg', 'fanart': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/f10303.jpg'}
  


 {'airdate': '2022-05-13T06:20:10Z', 'aired': '2022-04-22', 'release': '$day, $date $time', 'flag': False, 'title': 'Die Gangster Gang', 'episode': '', 'runtime': 100, 'studio': 'DreamWorks Animation', 'genres': 'Animation, Adventure', 'rating': '★ 8.7', 'poster': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/p10283.jpg', 'fanart': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/f10283.jpg'}
  


 {'airdate': '2022-05-13T06:20:09Z', 'aired': '2022-03-25', 'release': '$day, $date $time', 'flag': False, 'title': 'The Lost City - Das Geheimnis der verlorenen Stadt', 'episode': '', 'runtime': 111, 'studio': 'Paramount', 'genres': 'Action, Adventure', 'rating': '★ 7.8', 'poster': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/p10282.jpg', 'fanart': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/f10282.jpg'}
  


 {'airdate': '2022-05-13T06:20:08Z', 'aired': '2022-04-15', 'release': '$day, $date $time', 'flag': True, 'title': 'Choose or Die', 'episode': '', 'runtime': 85, 'studio': 'Anton', 'genres': 'Drama, Horror', 'rating': '★ 3.0', 'poster': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/p10276.jpg', 'fanart': '/local/upcoming-media-card-images/plex/Plex_Recently_Added_Movies/f10276.jpg'}
  

now i tried to add a if condition to filter but he always said there is no attribute flag

{% for i in states.sensor.plex_recently_added_movies.attributes.data %}
 {{ i }}
  {%- if i['flag'] == True -%} 
     
  {% endif %}

{% endfor %}

I think it goes wring with the first ‘i’;

{'title_default': '$title', 'line1_default': '$episode', 'line2_default': '$release', 'line3_default': '$number - $rating - $runtime', 'line4_default': '$genres', 'icon': 'mdi:eye-off'}
  

it doesn’t have ‘flag’ :thinking:

Instead of using a loop, you can use template functions to select matching entries:

{{ state_attr('sensor.plex_recently_added_movies', 'data') | selectattr('flag', 'eq', 'True') | list }}

okay there i get the same error, perhaps because the first result, has no flag?
how @aceindy says

UndefinedError: 'dict object' has no attribute 'flag'

Show us the output of this template:

{{ state_attr('sensor.plex_recently_added_movies', 'data') }}

If it is identical to the information you showed in your first post, try this:

{{ state_attr('sensor.plex_recently_added_movies', 'data')
  | selectattr('flag', 'defined')
  | selectattr('flag', 'eq', true) | list }}
2 Likes

Hello @123
yes it works well, he only shows the flags that are true. thank you.

Do you know how i can now save this to a sensor or entity ?
that it has the same structure as the original sensor?

EDIT

okay i get it to work.

- platform: template
    sensors:
      plex_unwatched:
        value_template: "{{ states('sensor.plex_recently_added_movies') }}"
        attribute_templates:
          data: "{{ state_attr('sensor.plex_recently_added_movies', 'data') | selectattr('flag', 'defined') | selectattr('flag', 'eq', true) | list }}"

thats really nice now i had only the unwatched movies from plex in my widget.

gives perhaps a way in the request to say when there is no result that he should take a fallback?