Help with Template in Variable

I don’t know if is possible or not.
I have this addon to make Phone calls from HA to my mobile: https://github.com/sdesalve/hassio-addons/tree/master/dss_voip.

I have this automation:

- id: '1575543714490'
  alias: ProvaScripttelefono
  description: ''
  trigger: []
  condition: []
  action:
  - data:
      entity_id: script.02notifica_telefonata
      variables:
        message: "Attenzione! Attenzione!!! ALLARME ATTIVATO!! Sensori Attivi:
        {% for state in states.binary_sensor -%}
        {% if state.state == 'on' and state.name not in ['Router Antenna Studio', 'Router Studio'] %} 
        {{ state.name }}
        {%- endif -%}
        {%- endfor -%}"
    service: script.turn_on

but when the automation pass the variable to the script this is the result:

[Info] Received messages {"call_sip_uri": "sip:[email protected]", "message_tts": "Attenzione!!! ALLARME ATTIVATO!! Sensori Attivi: {% for state in states.binary_sensor -%} {% if state.state == 'on' and state.name not in ['Router Antenna Studio', 'Router Studio'] %} {{ state.name }} {%- endif -%} {%- endfor -%}"}
Converting audio file 'https://xxxxxx.org/api/tts_proxy/xxxxxxxx_it_-_google_translate.mp3'...
Audio succesfully converted...
Starting SIP Client and calling 'sip:[email protected]'...

Someone have some idea?

There are many things that are wrong here:

  1. You don’t have a trigger.
  2. You need data_template instead of data. This will allow you to template any field under the data field. In your case, you’re trying to template message.
  3. When using multi-line templating, you need to use >. Using a string with carriage returns has the possibility deliver unwanted results.
  - data_template:
      entity_id: script.02notifica_telefonata
      variables:
        message: >
          Attenzione! Attenzione!!! ALLARME ATTIVATO!! Sensori Attivi:
          {% for state in states.binary_sensor -%}
          {% if state.state == 'on' and state.name not in ['Router Antenna Studio', 'Router Studio'] %} 
          {{ state.name }}
          {%- endif -%}
          {%- endfor -%}
    service: script.turn_on

Petro Thanks a lot for your reply.
I don’t have the trigger because for testing purposes i use the manual trigger.
Thanks a lot!! i have lost the data_template… :frowning:
Works very well now!! :smiley: