Templating help - Using For Loop to return dictionary values

I have a sensor which contains details about upcoming work shifts as a dictionary.
I’m trying to create sensors from it based on the day of the week (eg. Monday, Tuesday) a shift is on. Each sensor will have the state be true or false for if there’s a shift that day, with attributes giving details of that shift.

My issue is in returning either only the first shift that is on a specific day or returning ‘false’ if there is no shift on that day.

eg. Shift 1 is this Tuesday, and Shift 6 is on next Tuesday. It should only return the details for this Tuesday/Shift 1. ( It would also be good if it were possible to return the shift 6 instead of shift 1 for when I want a sensor based on shift 6)
eg2. There’s no shift on Wednesday, I want the template to return ‘false’, instead of having no value

So far I have this:

{%- set weekly = state_attr('sensor.work_shift_weekly','dict')%}
{% for i in weekly if  (i['Week_Day'] == 'Tue' ) %}
     {{ i['id'] }} {%break%}
 {%- endfor %}

Adding {%break%} resulted in showing Shift 1 and not Shift 6, which is good. But how do I make it return Shift 6 instead and do I make it return ‘false’ for Wednesday? Instead it returns nothing.

I tried adding an else section which works for the Wednesday, but on the Tuesday returns both Shift 1 and ‘false’

{%- set weekly = state_attr('sensor.work_shift_weekly','dict')%}
{% for i in weekly if  (i['Week_Day'] == 'Tue' ) %}
     {{ i['id'] }} {%break%}
 {%- else%}
  {%- endfor %}

Clearly, there’s something I’m not understanding about how this template or break works, but I’m having trouble finding what I need in the docs.

Is there a way to make this work for the sensors I want?

Please show the output of

{{ state_attr('sensor.work_shift_weekly','dict') }}

from Developer Tools / Template. You won’t want a loop to do this, but without knowing the data, I can’t help with what you do need…

The actual return has a lot more attributes with values based on other sensors, but this is the format it shows in.

[{'id': 'Shift 1', 'Week_Day': 'Tue', 'Start_Date_Time': 'date_time'},
{'id': 'Shift 2', 'Week_Day': 'Fri', 'Start_Date_Time': 'date_time'},
{'id': 'Shift 3', 'Week_Day': 'Sat', 'Start_Date_Time': 'date_time'},
{'id': 'Shift 4', 'Week_Day': 'Sun', 'Start_Date_Time': 'date_time'},
{'id': 'Shift 5', 'Week_Day': 'Mon', 'Start_Date_Time': 'date_time'},
{'id': 'Shift 6', 'Week_Day': 'Tue', 'Start_Date_Time': 'date_time'},

I know I could technically do a massive template referencing each of the original sensor data and a bunch of if then statements, but was hoping for something smaller and more manageable since I’d be repeating the template for each day.

{{ state_attr('sensor.work_shift_weekly','dict') | selectattr('Week_Day', 'eq', 'Tue') | map(attribute='id') | first | default(false) }}

Thank you! That is so much simpler

That’s why it’s always helpful to share the actual data you’re working with. The object you’re interrogating isn’t a dictionary, it’s a list of dictionaries. Had you included that in your original post, we could have got there more quickly.