How to break 'cycle for'

I try to explain myself. I have a sensor voip with this content
{“caller”: “234 4353463”}

the code should check if in the sensor.voip string there is a number among those in the phonebook (phone).
if there is input_text.call = corresponding name. {{names [i]}}
if there is no input_text.call = sensor.voip

is there a way to do this?’

  - service: input_text.set_value
    data_template:
      entity_id: input_text.call
      value: >
        {%- set names = ["John","Phil","Bruce","Ron","Paul","Marie"] -%}
        {%- set phone = ["3262313","468313","5576664","3465356","483731","385436"] -%}
        {% for i in range(0, (phone|length)) %}
        {% if states('sensor.voip')|regex_search(phone[i], ignorecase=False) %}
        {{names[i]}}
				########### BREAK ???? ##############
		{% else %}
		{{sensor.voip}}
        {% endif %} 
        {% endfor%}

according to official documentation, you can not break for iteration. On the other hand, if your condition is not true, just don’t put else statement, or you think that there can be multiple items could validate if statement?

https://jinja.palletsprojects.com/en/latest/templates/
Unlike in Python, it’s not possible to break or continue in a loop. You can, however, filter the sequence during iteration, which allows you to skip items. The following example skips all the users which are hidden:

if the condition is not true, I still need to record the value of sensor.voip to know what number called me

then, let’s seperate for iteation and if you find it while iterating assign a True value to a found variable`. after for iteration if found is False, record sensor.voip.

Does it work?

It’s not possible to break in jinja. Instead use and learn filters for jinja, namespace, or a different method.

Assuming that you’re using a normal viop integration and the sensor returns a phone number and you’re looking at the last digits, you don’t need a loop. Just make a dictionary and pull the info out of the dictionary.

  - service: input_text.set_value
    data_template:
      entity_id: input_text.call
      value: >
        {%- set phones = {
          "3262313":"John",
          "468313":"Phil",
          "5576664":"Bruce",
          "3465356":"Ron",
          "483731":"Paul",
          "385436":"Marie",
        } %}
        {%- set found = states('sensor.voip')[-7:] %}
        {{ phones.get(found, "Unknown") }}

… what if the number is not in the last 7 digits but is in the middle of the text? do you have an idea for that too?

Give some examples of phone numbers you have received where this has happened.

The North American Numbering Plan uses this format:

3-digit Area Code | 3-digit Exchange | 4-digit Line Number

Then check to see if the .get returns unknown and try to get it using the last 6 digits after the 7 digits fail. You have all the building blocks there, if you need help, I can assisnt.

now I can’t provide it to you. But it would still be interesting to solve the problem using the regex_search from my first example. Maybe the solution can be useful on other occasions :slight_smile:

looping through information is rarely needed with Jinja. Everything can typically be solved by filters or a different method. If you have to loop and selectively parse items, use namespace.

I don’t have the presumption of becoming a programmer. I learned yaml only for the necessity of programming hassio’s home automation, but I would not want to go further :slight_smile:

This isn’t yaml, this is jinja. And why bother asking for further methods if you don’t plan on learning how they work?

this makes you understand how much I understand about programming. Now I would only need the solution to my code, perhaps continuing to use jinia

I already gave you the solution…

Not really. The original approach was needlessly complicated. I don’t see the need to continue with it when there’s a simpler solution available.

If you can’t think of an example where the 7 digits do not appear at the end of the phone number, then I don’t see the need to implement regex_search.

I noticed some of the phone numbers are 6 digits. I’m curious to learn in which country 6 digits is sufficient. My knowledge of this is limited to North America where 10 digits is the norm and 7 is the minimum for a given area code.