How to write templates in several strings?

Hey there! I have some issues after Home Assistant update I think (but not sure)

I started to get an error

Error executing script. Unexpected error for call_service at pos 3: {'request_id': '4051a490-df26-423e-94d6-899909bcef46', 'status': 'error', 'code': 'INVALID_VALUE', 'message': Message can contain only strings and symbols "-,!.:=?".'}

It seems like tts command is not processed correctly

This does not work:

  - service: media_player.play_media
    entity_id: media_player.yandex_mini
    data_template:
      media_content_id: >
        Добро пожаловать домой, {% set alias = {'Арго':'Арго', 'Армен':'Армен', 'Гаяне':'Гаяне', 'Олеся':'Олеся'} %}
        {% set people_home = states.person | selectattr('state','eq','home') | map(attribute='attributes.friendly_name') | list %}
        {% set ns = namespace(occupants = '') %}
        {% for i in people_home %}
        {% set ns.occupants = ns.occupants ~ '  ' ~ alias.get(i,i) if loop.index > 1 else alias.get(i,i) %}
        {% endfor %}
        {{ ns.occupants }}

But this works:

  - service: media_player.play_media
    entity_id: media_player.yandex_mini
    data_template:
      media_content_id: Добро пожаловать домой, {% set alias = {'Арго':'Арго', 'Армен':'Армен', 'Гаяне':'Гаяне', 'Олеся':'Олеся'} %}{% set people_home = states.person | selectattr('state','eq','home') | map(attribute='attributes.friendly_name') | list %}{% set ns = namespace(occupants = '') %}{% for i in people_home %}{% set ns.occupants = ns.occupants ~ '  ' ~ alias.get(i,i) if loop.index > 1 else alias.get(i,i) %}{% endfor %}{{ ns.occupants }}
      media_content_type: text

I also tried ‘|’ instead of ‘>’ but that did not work. Seems like my template is not read as one string of text

And by the way, is there a more compact way of a template which says who is at home right now?

I’m very confused by your template. Why even have the map? You have the name of occupants with a list of people_home.

  - service: media_player.play_media
    entity_id: media_player.yandex_mini
    data_template:
      media_content_id: >
        Добро пожаловать домой, {{ states.person | selectattr('state','eq','home') | map(attribute='attributes.friendly_name') | list | join('  ') }}

The thing is, this template was not created by me
here is it

at that time I needed to correct names pronunciation, maybe that’s why template is a little bit longer than it should be

Some more strange stuff
this works
“This is a test”

and this does not
“This is a test”

Difference is only in one thing: in second sentence I have 2 spaces between ‘a’ and ‘test’

I think now that I should reach to yandex alice custom component creator, seems to be it’s issue

Yes, that makes sense. If you don’t need to do that just use the simple one I made. Remove the 2 spaces in the join and only add 1.

Yeah, I just corrected it and it works. Also found open issue on the same problem on yandex component. If anyone needs - here is it (be careful, russian language)

Thanks for quick response and help with the template

Good question. Because in the other post, Argo said TTS wasn’t pronouncing the names correctly (and they were presented using the Latin, not Cyrillic alphabet). I suggested he use appropriate phonetic replacements. At the time, the idea was accepted (although never formally marked as a Solution):

One year later, I see the technique was misapplied to the extent of making it unnecessary … resulting in your valid question: Why even have the map?

Yeah, he already explained that a post or 2 up

As I have mentioned - I needed correct pronouncing that days. I just switched my dumb speaker to smart speaker from yandex (like google home mini). It is pronouncing everything correctly

I really appreciate your help, I used that template for 10 months and it worked as it should. Sorry that I did not mark your answer as a solution, it definitely was.

And I still find jinja a little bit messy even after learning python (junior level) and having around 5 months of working in IT department using mainly python. I still struggle to make my own templates longer than 1 sentence (that’s why I did not cut map and optimise the template)

I see; that explains the use of native Russian spelling. Все хорошо!

1 Like