Using a notification script to to send email, text_msgs & Alexa messages

First I created an automation that will trigger a script to test sending out notices.

alias: Test Notifications
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.spare_sensor_1
    from: "off"
    to: "on"
    id: Sensor Opened
condition: []
action:
  - service: script.notification_script
    data:
      notify_target: office_clock_echo_dot
      notify_title: This is the title
      notify_message: Hello World!
      alexa_enabled: true
mode: single

And then I created the notice_script

alias: notification_script
sequence:
  - service: notify.alexa_media
    data:
      message: "{{ notify_message }}"
      target: media_player.{{ notify_target }}
      data:
        type: announce
        method: speak
    enabled: true 
mode: queued
icon: mdi:message-outline
max: 10

Note there is only one service here, there will be others for generating emails and text messages to various household members.
The problem is that I want to be able to enable/disable the various services from the automation. The obvious solution would be to set the alexa_enabled variable to false if the alexa service was not wanted, and then change the ‘enabled’ line in the service to

enabled: "{{ alexa_enabled }}"

But when I do that I get an error message

Message malformed: invalid boolean value {{ alexa_enabled }} for dictionary value 
@ data['sequence'][0]['enabled']

So apparently it is not accepting the templating of the enabled: value.

My second thought was to use the alexa_enabled variable in a condition for the service. Something like this.

alias: notification_script
- conditions:
  - condition: template
    value_template: "{{ alexa_enabled }}"
  sequence:
    - service: notify.alexa_media
      data:
        message: "{{ notify_message }}"
        target: media_player.{{ notify_target }}
        data:
          type: announce
          method: speak
      enabled: true
mode: queued
icon: mdi:message-outline
max: 10

But in all the attempts to do this I either couldn’t get the indenting correct, or it would throw an error when attempting to save. And I’m not even sure that conditions are functional in a script anyway.
So, I’m looking for one of three things.

  1. How to set the enabled state from a variable in the automation
  2. How to implement a condition on the notify service in the script.
  3. Some other method to enable and disable individual services in the script, from the automation.

Thanks for an help.

The enable configuration variable doesn’t accept templates, you will need to use conditions. If your plan is to have multiple notification services in the same script you should use either If/Then or Choose actions for each service:

alias: notification_script
sequence:
  - alias: Alexa Announcement
    choose:
      - conditions:
          - condition: template
            value_template: "{{ alexa_enabled }}"
    sequence:
      - service: notify.alexa_media
        data:
          message: "{{ notify_message }}"
          target: media_player.{{ notify_target }}
          data:
            type: announce
            method: speak
  - alias:  Email Notification
    choose:
      - conditions:
          - condition: template
            value_template: "{{ email_enabled }}"
    sequence:
      - service: notify.email_example
...
mode: queued
icon: mdi:message-outline
max: 10

FWIW, you can use conditions in scripts, but without If/then or Choose they will block all actions below them. Conditions are used just like any other action in the sequence as follows:

alias: notification_script
sequence:
    - condition: template
      value_template: "{{ alexa_enabled }}"
    - service: notify.alexa_media
      data:
        message: "{{ notify_message }}"
        target: media_player.{{ notify_target }}
        data:
          type: announce
          method: speak
...
mode: queued
icon: mdi:message-outline
max: 10

I have an input_boolean that is use for various notification methods. for example, i have an input_boolean.audible_notifications that i use to turn on and off TTS notifications over my Sonos speakers. I use it in all my automations that have TTS either as a condition on the automation or as a condition on a choose action. then, i have a dashboard with toggles for the different notificaiton methods that i can turn on or off with a click of a button.