Help me with this template with too many quotes

Allright, I have this and need to make it proper with all these quotes.

hangoutsanswer:
  speech:
    text: >
        '{{["randomanswer1 {{states.device_tracker.google_maps_secret.attributes.address.split(",")[1]}}.
        randomasnwer1continue {{states.device_tracker.google_maps_secret.attributes.address.split(",")[0]}}.
        randomasnwer1continue .",
        "randomanswer2 {{states.device_tracker.google_maps_secret.attributes.address.split(",")[1]}}.
        randomasnwer2continue {{states.device_tracker.google_maps_secret.attributes.address.split(",")[0]}}."]|random }}'

I am not very good at this and I am smashing my head to figure out which quote to use in all these cases. As you can see, I start and end with a single quote, but the random template starts and ends with double quotes in each random answer… Now inside these answers there are attributes I have to split. I used single quotes but didnt work either…

Untested, but give this a try.

hangoutsanswer:
  speech:
    text: >-
        {% set da0 = states.device_tracker.google_maps_secret.attributes.address.split(",")[0] %}
        {% set da1 = states.device_tracker.google_maps_secret.attributes.address.split(",")[1] %}
        {{ ["bla {{da1}}. blabla {{da0}}. blablabla.", "yak {{da1}}. yakyak {{da0}}."]|random }}

Thank you a lot for the suggestion but the result is this:

yak {{da1}}. yakyak {{da0}}.

It doesnt return the attribute. Just the set variable.
I played with your template but couldnt figure out what is wrong.
This seems like a better approach than mine and also cleaner.

Any ideas?

Oops! I’m still a Jinja2 neophyte! Try this one, I confirmed relevant portions of it work in the Template Editor.

hangoutsanswer:
  speech:
    text: >-
        {% set da0 = states.device_tracker.google_maps_secret.attributes.address.split(",")[0] %}
        {% set da1 = states.device_tracker.google_maps_secret.attributes.address.split(",")[1] %}
        {% set r1 = 'bla '+da1+'. blabla '+da0+'. blablabla.' %}
        {% set r2 = 'yak '+da1+'. yakyak '+da0+'.' %}
        {{ [r1, r2]|random }}
1 Like

Worked perfeclty! Thank you for that and your time solving this puzzle for me!

Got some questions if you dont mind.

  1. What is the purpose of >-
    I used > only because I saw it somewhere, but never understood how it works. I got lost in the documentation, so I couldnt figure it out.
  2. Same question for +

I believe the - is used for Whitespace Control. For example, if the template has leading spaces:

                     {{ Hello! }}

The output will retain the leading spaces if you don’t use whitespace control.

                      Hello! 

With whitespace control, whitespaces are removed and the output would look like this:

Hello! 

The + is used to combine strings and/or variables containing strings.

{% set x = 'Hello ' %}
{% set y = x + 'world!' %}
{{y}}

Output is:

Hello world!
1 Like