Script with if ..... help

Good evening I have a script that prepares a message and sends the message to a script that makes alexa pronounce it but the script that builds the message does not work.

Message construction:

calendario_lavoro:
  alias: "Prossimo lavoro"
  sequence:
    - service: script.speak_all
      data_template:
        {% if is_state("calendar.lavoro", "off")-%}
          message: Oggi non hai nessun impegno programmato
        {%- else -%}
          message: Ciao Alberto, hai un impegno di lavoro il {{ as_timestamp(states.calendar.lavoro.attributes.start_time) | timestamp_custom('%-d %B') }} dalle {{ as_timestamp(states.calendar.lavoro.attributes.start_time) | timestamp_custom('%I:%M %p') }} alle {{ as_timestamp(states.calendar.lavoro.attributes.end_time) | timestamp_custom('%I:%M %p') }} al {{states.calendar.lavoro.attributes.message }}
        {%- endif %}

this script reads with alexa.
This definitely works because other messages read them

speak_all: 
  alias: "TTS message all"
  sequence:
    - service: media_player.volume_set
      entity_id: media_player.cucina
      data:
        volume_level: '.3'
    - service: notify.alexa_media
      data:
        data:
          type: tts
        target:
        - media_player.cucina
        - media_player.ufficio
        - media_player.letto
      data_template:
        message: "{{ message }}"
        data:
          type: announce
#        method: media_player.all

How can I solve its syntax?
Thanks, Alberto

Both scripts are not correct.

The calendario_lavoro script is close to correct but you’re making a common mistake with templates. You can only template in a single field. The field cannot be included with the return either.

calendario_lavoro:
  alias: "Prossimo lavoro"
  sequence:
    - service: script.speak_all
      data_template:
        message: >
          {% if is_state("calendar.lavoro", "off")-%}
            Oggi non hai nessun impegno programmato
          {%- else -%}
            Ciao Alberto, hai un impegno di lavoro il {{ as_timestamp(states.calendar.lavoro.attributes.start_time) | timestamp_custom('%-d %B') }} dalle {{ as_timestamp(states.calendar.lavoro.attributes.start_time) | timestamp_custom('%I:%M %p') }} alle {{ as_timestamp(states.calendar.lavoro.attributes.end_time) | timestamp_custom('%I:%M %p') }} al {{states.calendar.lavoro.attributes.message }}
          {%- endif %}

Your second script is overwriting the data section in the second service. Specifically you’re merging two dictionaries and ‘type’: tts is getting overwritten with ‘type’: announce. You need to fix second service.

    - service: notify.alexa_media
      data_template:
        message: "{{ message }}"
        data:
          type: announce
        target:
        - media_player.cucina
        - media_player.ufficio
        - media_player.letto

or

    - service: notify.alexa_media
      data_template:
        message: "{{ message }}"
        data:
          type: tts
        target:
        - media_player.cucina
        - media_player.ufficio
        - media_player.letto

Fantastic !!
Many many thanks

1 Like