Skip to content

๐Ÿ“ฒ Centralized Notifications โ€‹

A unified notification script for Home Assistant (mode: parallel) that routes messages to mobile push, persistent UI notifications, and Telegram, adapting Markdown formatting per channel.

โš™๏ธ Parameters โ€‹

FieldSelectorRequiredDefaultDescription
canali_notificaselectYespush, home_assistantDelivery channels (push, home_assistant, telegram).
tutti_i_dispositivibooleanNotrueWhen true, sends push via notify.notify; when false, uses selected devices.
destinatari_specificientityNoOne or more notify entities to target when tutti_i_dispositivi is false.
titletextYesMain title shown at the top of the notification.
messagetextYesNotification body text (supports Markdown).
notification_idtextNoOptional persistent notification ID to overwrite earlier alerts of the same type.

๐Ÿ” How It Works โ€‹

  1. Mobile Push (push): strips Markdown syntax (**, *, _, #) before sending either globally via notify.notify or per device via notify.mobile_app_<device>.
  2. Home Assistant UI (home_assistant): creates a persistent_notification keeping native Markdown intact, using notification_id only when provided.
  3. Telegram (telegram): sends via telegram_bot.send_message with parse_mode: markdownv2 and automatic regex escaping of special characters.

๐Ÿงช Example Call โ€‹

yaml
action: script.gestione_notifiche_centralizzata
data:
  canali_notifica:
    - push
    - home_assistant
  tutti_i_dispositivi: true
  title: "โš ๏ธ Dishwasher Alert"
  message: "The **salt** is empty!"
  notification_id: dishwasher_status

๐Ÿ“œ Full Script YAML โ€‹

Import in Home Assistant

Go to Settings -> Automations & Scenes -> Scripts -> Add Script -> Edit in YAML and paste the configuration below. Use the language selector to switch between English and Italian.

Centralized NotificationsYAML
alias: ๐Ÿ“ฒ Centralized Notifications Manager
description: >-
  ### ๐Ÿ“ฒ Unified Notifications Script

  Centralized script for sending notifications.

  Handles global broadcasts or targeted delivery to dynamically filtered
  devices, stripping Markdown formatting for mobile push notifications.
mode: parallel
fields:
  canali_notifica:
    name: ๐Ÿ”Œ Notification Channels
    description: >-
      ๐Ÿ”Œ Select the channels to send the notification through (multiple
      allowed).
    required: true
    default:
      - push
      - home_assistant
    selector:
      select:
        multiple: true
        options:
          - label: ๐Ÿ“ฑ Push Notification (Mobile App)
            value: push
          - label: ๐Ÿ–ฅ๏ธ Persistent Notification (HA UI)
            value: home_assistant
          - label: โœˆ๏ธ Telegram
            value: telegram
  tutti_i_dispositivi:
    name: ๐Ÿ‘ฅ Send to All Devices
    description: >-
      ๐Ÿ‘ฅ When enabled, push notifications are sent to all registered devices.
      When disabled, uses the list below.
    required: false
    default: true
    selector:
      boolean: {}
  destinatari_specifici:
    name: ๐ŸŽฏ Specific Devices
    description: >-
      ๐Ÿ“ฑ Select one or more smartphones/services to notify (only applies when
      'Send to All Devices' is disabled).
    required: false
    selector:
      entity:
        domain: notify
        multiple: true
  title:
    name: โœจ Notification Title
    description: โœจ Main title shown at the top of the notification.
    example: โš ๏ธ Dishwasher Alert
    required: true
    selector:
      text: {}
  message:
    name: ๐Ÿ“ Notification Message
    description: โœจ Body text of the notification (may include Markdown).
    example: The **salt** is empty!
    required: true
    selector:
      text: {}
  notification_id:
    name: ๐Ÿ†” Persistent Notification ID
    description: >-
      ๐Ÿ†” Unique ID for the persistent notification (optional). Useful for
      overwriting previous notifications of the same type.
    example: dishwasher_status
    required: false
    selector:
      text: {}
sequence:
  - alias: ๐Ÿ“ฑ Check and Send Push Notification
    note: >-
      Checks whether the 'push' channel is enabled before resolving delivery
      targets.
    if:
      - condition: template
        value_template: '{{ ''push'' in canali_notifica }}'
        alias: '๐Ÿ” Check channel: Push'
        note: Checks the channels array for the 'push' value.
    then:
      - alias: ๐Ÿ”€ Select Push Target
        note: >-
          Sends to all devices or iterates over the specified targets.
        choose:
          - alias: '๐Ÿ‘ฅ Case 1: Global Broadcast'
            note: >-
              Sends a broadcast via notify.notify when the boolean is set to
              true.
            conditions:
              - condition: template
                value_template: '{{ tutti_i_dispositivi }}'
                alias: 'Check: Send to all devices = True'
            sequence:
              - action: notify.notify
                data:
                  title: >-
                    {{ title | regex_replace(find='\*\*|\*|_|###\s|##\s|#\s',
                    replace='', ignorecase=True) }}
                  message: >-
                    {{ message | regex_replace(find='\*\*|\*|_|###\s|##\s|#\s',
                    replace='', ignorecase=True) }}
                alias: ๐Ÿš€ Send Group Push
                note: >-
                  Immediately notifies all devices connected to the Home
                  Assistant hub.
          - alias: '๐ŸŽฏ Case 2: Targeted Delivery'
            note: >-
              Loops through the specific notify services selected in the array.
            conditions:
              - condition: template
                value_template: >-
                  {{ not tutti_i_dispositivi and destinatari_specifici is
                  defined and destinatari_specifici | length > 0 }}
                alias: 'Check: Specific devices selected'
            sequence:
              - repeat:
                  for_each: '{{ destinatari_specifici }}'
                  sequence:
                    - action: notify.mobile_app_{{ repeat.item.split('.')[1] }}
                      data:
                        title: >-
                          {{ title |
                          regex_replace(find='\*\*|\*|_|###\s|##\s|#\s',
                          replace='', ignorecase=True) }}
                        message: >-
                          {{ message |
                          regex_replace(find='\*\*|\*|_|###\s|##\s|#\s',
                          replace='', ignorecase=True) }}
                      alias: ๐Ÿš€ Send Push to Single Target
                      note: >-
                        Dynamically builds the service name by prepending
                        mobile_app_ as required by the official Home Assistant
                        mobile app integration.
  - alias: ๐Ÿ–ฅ๏ธ Check and Create Persistent Notification
    note: >-
      Checks whether the 'home_assistant' channel is enabled. The Home Assistant
      UI supports Markdown natively.
    if:
      - condition: template
        value_template: '{{ ''home_assistant'' in canali_notifica }}'
        alias: '๐Ÿ” Check channel: Home Assistant'
        note: Checks the channels array for the 'home_assistant' value.
    then:
      - alias: ๐Ÿ”€ Select Persistent Notification Payload
        note: >-
          Handles the presence or absence of the optional notification_id to
          avoid dictionary key errors.
        choose:
          - alias: '๐Ÿ†” Case A: With Specified ID'
            note: Runs when a notification ID has been provided.
            conditions:
              - condition: template
                value_template: >-
                  {{ notification_id is defined and notification_id != '' and
                  notification_id != none }}
            sequence:
              - action: persistent_notification.create
                data:
                  title: '{{ title }}'
                  message: '{{ message }}'
                  notification_id: '{{ notification_id }}'
                alias: ๐Ÿš€ Create Notification with Static ID
          - alias: '๐ŸŽฒ Case B: Without Specified ID'
            note: >-
              Runs when the ID field is empty, omitting the key from the data
              payload.
            conditions:
              - condition: template
                value_template: >-
                  {{ notification_id is not defined or notification_id == '' or
                  notification_id == none }}
            sequence:
              - action: persistent_notification.create
                data:
                  title: '{{ title }}'
                  message: '{{ message }}'
                alias: ๐Ÿš€ Create Notification with Random ID
  - alias: โœˆ๏ธ Check and Send Telegram Message
    note: >-
      Checks whether the 'telegram' channel is enabled and applies Telegram
      MarkdownV2 escaping.
    if:
      - condition: template
        value_template: '{{ ''telegram'' in canali_notifica }}'
        alias: '๐Ÿ” Check channel: Telegram'
        note: Checks the channels array for the 'telegram' value.
    then:
      - alias: ๐Ÿš€ Send Telegram with Parser
        note: >-
          Sends the message to Telegram after escaping special characters for
          MarkdownV2.
        action: telegram_bot.send_message
        data:
          title: >-
            {{ title |
            regex_replace(find='([_\\*\\`\\[\\]\\(\\)\\~\\>\\#\\+\\-\\=\\|\\{\\}\\.\\!])',
            replace='\\\\\\1', ignorecase=True) }}
          message: >-
            {{ message |
            regex_replace(find='([_\\*\\`\\[\\]\\(\\)\\~\\>\\#\\+\\-\\=\\|\\{\\}\\.\\!])',
            replace='\\\\\\1', ignorecase=True) }}
          parse_mode: markdownv2