How to test the existence of a notification service?

I am writing a simple script to write a formatted message to a log file using a notify service specified by the caller. Before attempting to write the message I want to check that the service exists but cannot figure out how.

In the code below the is defined test always returns true. The script works with a valid service (so the service name is correctly built), and it correctly does nothing if the name is blank, but if the non-blank service does not exist, the script halts with an error message saying the service does not exist. I want to just ignore it and not attempt to write a message.

Playing with the template editor suggests it is simply testing that the variable holding the name of the service is defined. How can I test the existence of the service whose name is in the variable?

heating_xyz_logfile_entry:
  alias: Heating XYZ Log File Entry
  mode: parallel
  icon: mdi:script
  max: 100

  fields:

    notification_service:
      name: Notification Service
      description: Name of the (alrady configured) notification service to be used
      example: main_bedroom_heating_log 
      selector:
        text:
      required: false
      default: ""

    logfile_title:
      name: Logfile Title
      description: Title for the logfile (appears in the header)
      example: Heating X2
      selector:
        text:
      required: false
      default: ""

    message_preamble:
      name: Message Preamble
      description: Text identifyng the source of the message
      example: Main Bedroom Heating Control 
      selector:
        text:
      required: false
      default: ""

    message_body:
      name: Message Body
      description: The substance of the message to be logged
      example: This is a test message 
      selector:
        text:
      required: false
      default: ""
  
  variables:
    local_notification_service_name: >-
      {{ notification_service | string | trim }}
    local_notification_service: >-
      {{ "notify." + local_notification_service_name }}

  sequence:
    - if:
      - condition: template
        value_template: >
          {{ local_notification_service_name | length > 0 }}
      - condition: template
        value_template: >
          {{ local_notification_service is defined }}  ### ALWAYS RETURNS TRUE
      then:
      - service: >
          {{ local_notification_service }}
        data:
          message: >
            {{ "[" + message_preamble | string | trim + "] " + message_body | string | trim }} 
          title: >
            {{ logfile_title | string | trim }}


{{ local_notification_service is defined }}

checks whether the variable local_notification_service is defined. if it is passed in with anything then it is indeed defined.

i think you’re trying to find out if the contents of local_notification_service points to a valid service or not. i don’ know of a way to do that. not sure that there is a way. but someone more knowledgable than i maybe able to confirm or correct.

in what scenarios might it be garbage? seems like there would be a different better solution. trying to filter out garbage at this point feels kinda like a hack.

Correct.

The notification service name is passed to a blueprint, but because blueprints do not have selectors for notification services, it comes as plain text and is therefore prone to error. Checking on it is defensive programming. At the moment an erroneous name causes the automation to stop – despite my use of continue_on_error: true – and you have to go to the system log to find the error.