Hi,
I wanted multiple phone numbers in a string sent to a TTS to be spoken as single digits instead of a huge number. But for only phone numbers.
Sounds like it should be easy, right.
Well, its not so easy. In fact, I could not even one single example of this being done for home assistant.
For example, A text Msg: “Msg from 19721234567, please also call 18005551212 for help.”
To get this spoken via TTS with a slight pause between digit it would need to be translated as “Msg from 1! 9! 7! 2! 1! 2! 3! 4! 5! 6! 7!, please also call 1! 8! 0! 0! 5! 5! 5! 1! 2! 1! 2! for help.”
For context, I have voice assistant access my text messages. I ask my voice assistant to read my last X number of text messages. I use pushover to store by text messages which are forwarded by my phone. The problem was phone numbers were a huge number.
Here’s the template to process this message:
{% set your_string = "Text from 19721234567, please also call 18005551212 for help." %}
{% set phone_regex = '\d{10,}' %}
{% set ns = namespace(output_string = your_string) %}
{% set phone_numbers = your_string | regex_findall(phone_regex) %}
{% for phone in phone_numbers %}
{% set formatted_phone = phone | list | join('! ') %}
{% set ns.output_string = ns.output_string | replace(phone, formatted_phone) %}
{% endfor %}
{{ ns.output_string }}