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:
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.
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.
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 …
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
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.