Need help with a Generic script to notify multiple devices in one shot (Alexa, Google Home, Mobile, Perm notification)

Hi All, I am in the process of creating a single reusable notification script that will notify Alexas, Google Assistant, and mobile entities all in one shot.

Though this script is triggering I don’t get notifications on Alexa or Google, I am getting mobile notifications on HA App.

Can someone review and tell me what am i doing wrong? I wanted to pass the list of Alexa and Google Homes as parameters for this.

alias: Script Notify Displays
description: Send notification to key devices
fields:
  name:
    description: Subject of the message
    required: true
    selector:
      text: null
    example: You are awesome
  message:
    description: Message content of the notification.
    required: true
    selector:
      text: null
    example: Notifications are awesome
  notify_alexa:
    description: Notify Alexa Devices
    default: true
    selector:
      boolean: null
    example: 'False'
  notify_google_home:
    description: Notify Google Home Devices
    default: true
    selector:
      boolean: null
    example: 'False'
  entities:
    description: List of Google Homes to notify
    selector:
      target:
        entity:
          domain: media_player
    example: media_player.kitchen_display
  alexa_entities:
    description: List of Alexa Players to notify
    selector:
      target:
        entity:
          domain: media_player
    example: media_player.kitchen_display
  emergency:
    description: Notify even if the device is in DND Mode.
    default: false
    selector:
      boolean: null
    example: 'False'
  logbook:
    description: Add entry to Log Book
    default: true
    selector:
      boolean: null
    example: 'True'
  title:
    description: Title for permenant notifications
    required: true
    selector:
      text: null
    example: Permanent notification Title
  mobile:
    description: Send Mobile Notification
    default: true
    selector:
      boolean: null
    example: 'True'
  permanent:
    description: Create a permanent notification?
    default: false
    selector:
      boolean: null
    example: 'True'
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ notify_alexa }}'
        sequence:
          - service: notify.alexa_media
            data:
              title: '{{ title }}'
              message: '{{ name }} {{ message }}'
              target: '{{ alexa_entities.entity_id }}'
              data:
                type: announce
                method: all
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ permanent }}'
        sequence:
          - service: persistent_notification.create
            data:
              title: '{{ title }}'
              message: '** {{ name }} ** {{ message }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ mobile }}'
        sequence:
          - service: notify.xxx_phone_group
            data:
              title: '{{ title }}'
              message: '{{ name }} {{ message }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ logbook }}'
        sequence:
          - service: logbook.log
            data:
              name: '{{ name }}'
              message: '{{ message }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ notify_google_home }}'
        sequence:
          - service: media_player.play_media
            target:
              entity_id: |
                {{ entities.entity_id }}
            data:
              media_content_id: >-
                media-source://tts/google_translate?message="{{ name | replace("
                ", "+") }} + {{ message | replace(" ", "+") }}"
              media_content_type: provider
            metadata:
              title: 'Back Door is open '
              thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
              media_class: app
              children_media_class: null
              navigateIds:
                - {}
                - media_content_type: app
                  media_content_id: media-source://tts
                - media_content_type: provider
                  media_content_id: >-
                    media-source://tts/google_translate?message="{{ name |
                    replace(" ", "+") }}+{{ message | replace(" ", "+") }}"
mode: single
icon: mdi:speaker-wireless

Can someone please help me with this?

From your field examples: you are passing the echo device entity_id string as alexa_entities, then your target template is trying to extract a non-existent entity_id attribute out of that string…

Remove the .entity_id:

sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ notify_alexa }}'
        sequence:
          - service: notify.alexa_media
            data:
              title: '{{ title }}'
              message: '{{ name }} {{ message }}'
              target: '{{ alexa_entities }}'
              data:
                type: announce
                method: all

I have no experience with google home… but it looks like you’re trying to do the same thing i.e. extract a non-existent attribute from the entities string.

Thanks @Didgeridrew , let me give it a try

While I tried changing it I am getting this error now

Logger: homeassistant.helpers.script.websocket_api_script
Source: helpers/script.py:1718
First occurred: 1:57:38 PM (3 occurrences)
Last logged: 1:58:27 PM

websocket_api script: Error executing script. Error for call_service at pos 1: Template rendered invalid entity IDs: {β€˜0’: β€˜m’, β€˜1’: β€˜e’, β€˜2’: β€˜d’, β€˜3’: β€˜i’, β€˜4’: β€˜a’, β€˜5’: β€˜', β€˜6’: β€˜p’, β€˜7’: β€˜l’, β€˜8’: β€˜a’, β€˜9’: β€˜y’, β€˜10’: β€˜e’, β€˜11’: β€˜r’, β€˜12’: β€˜.’, β€˜13’: β€˜k’, β€˜14’: β€˜i’, β€˜15’: β€˜t’, β€˜16’: β€˜c’, β€˜17’: β€˜h’, β€˜18’: β€˜e’, β€˜19’: β€˜n’, β€˜20’: '’, β€˜21’: β€˜d’, β€˜22’: β€˜i’, β€˜23’: β€˜s’, β€˜24’: β€˜p’, β€˜25’: β€˜l’, β€˜26’: β€˜a’, β€˜27’: β€˜y’, β€˜entity_id’: [β€˜media_player.master_bathroom_speaker’, β€˜media_player.master_bedroom_display’]}

Here is the updated code

alias: Script Notify Displays
description: Send notification to key devices
fields:
  name:
    description: Subject of the message
    required: true
    selector:
      text: null
    example: You are awesome
  message:
    description: Message content of the notification.
    required: true
    selector:
      text: null
    example: Notifications are awesome
  notify_alexa:
    description: Notify Alexa Devices
    default: true
    selector:
      boolean: null
    example: 'False'
  notify_google_home:
    description: Notify Google Home Devices
    default: true
    selector:
      boolean: null
    example: 'False'
  entities:
    description: List of Google Homes to notify
    selector:
      target:
        entity:
          domain: media_player
    example: media_player.kitchen_display
  alexa_entities:
    description: List of Alexa Players to notify
    selector:
      target:
        entity:
          domain: media_player
    example: media_player.kitchen_display
  emergency:
    description: Notify even if the device is in DND Mode.
    default: false
    selector:
      boolean: null
    example: 'False'
  logbook:
    description: Add entry to Log Book
    default: true
    selector:
      boolean: null
    example: 'True'
  title:
    description: Title for permenant notifications
    required: true
    selector:
      text: null
    example: Permanent notification Title
  mobile:
    description: Send Mobile Notification
    default: true
    selector:
      boolean: null
    example: 'True'
  permanent:
    description: Create a permanent notification?
    default: false
    selector:
      boolean: null
    example: 'True'
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ notify_alexa }}'
        sequence:
          - service: notify.alexa_media
            data:
              title: '{{ title }}'
              message: '{{ name }} {{ message }}'
              target: '{{ alexa_entities }}'
              data:
                type: announce
                method: all
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ permanent }}'
        sequence:
          - service: persistent_notification.create
            data:
              title: '{{ title }}'
              message: '** {{ name }} ** {{ message }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ mobile }}'
        sequence:
          - service: notify.usha_ajith
            data:
              title: '{{ title }}'
              message: '{{ name }} {{ message }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ logbook }}'
        sequence:
          - service: logbook.log
            data:
              name: '{{ name }}'
              message: '{{ message }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ notify_google_home }}'
        sequence:
          - service: media_player.play_media
            target:
              entity_id: |
                {{ entities }}
            data:
              media_content_id: >-
                media-source://tts/google_translate?message="{{ name | replace("
                ", "+") }} + {{ message | replace(" ", "+") }}"
              media_content_type: provider
            metadata:
              title: 'Back Door is open '
              thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
              media_class: app
              children_media_class: null
              navigateIds:
                - {}
                - media_content_type: app
                  media_content_id: media-source://tts
                - media_content_type: provider
                  media_content_id: >-
                    media-source://tts/google_translate?message="{{ name |
                    replace(" ", "+") }}+{{ message | replace(" ", "+") }}"
mode: single
icon: mdi:speaker-wireless

1 Like

Sorry about that. That method worked when I manually entered an entity_id, but I neglected to test it using the selector, which output a dictionary. The following is working for me using the selector:

        sequence:
          - service: notify.alexa_media
            data:
              title: '{{ title }}'
              target: >
                {{ alexa_entities.get("entity_id") }}
              message: '{{ name }} {{ message }}'
              data:
                type: announce
                method: all

I don’t know how much you use the yaml editor in the Services tool, but you may want to edit the example field for alexa_entities so that it reflects the correct data type and fills in correctly if you use the β€œFill Example Data” button:

alexa_entities:
    description: List of Alexa Players to notify
    selector:
      target:
        entity:
          domain: media_player
    example: {"entity_id": ["media_player.living_room_dot","media_player.kitchen_display"]}

Thanks a lot @Didgeridrew, Alexa one worked, let me play around with google one now

did you get this working, looks like what i want to do as well

Yes here is the full code

alias: Script Notify Displays
description: Send notification to key devices
fields:
  name:
    description: Subject of the message
    required: true
    selector:
      text: null
    example: You are awesome
  message:
    description: Message content of the notification.
    required: true
    selector:
      text: null
    example: Notifications are awesome
  notify_alexa:
    description: Notify Alexa Devices
    default: true
    selector:
      boolean: null
    example: 'False'
  notify_google_home:
    description: Notify Google Home Devices
    default: true
    selector:
      boolean: null
    example: 'False'
  entities:
    description: List of Google Homes to notify
    selector:
      target:
        entity:
          domain: media_player
    example: media_player.kitchen_display
  alexa_entities:
    description: List of Alexa Players to notify
    selector:
      target:
        entity:
          domain: media_player
    example: media_player.kitchen_display
  emergency:
    description: Notify even if the device is in DND Mode.
    default: false
    selector:
      boolean: null
    example: 'False'
  logbook:
    description: Add entry to Log Book
    default: true
    selector:
      boolean: null
    example: 'True'
  title:
    description: Title for permenant notifications
    required: true
    selector:
      text: null
    example: Permanent notification Title
  mobile:
    description: Send Mobile Notification
    default: true
    selector:
      boolean: null
    example: 'True'
  permanent:
    description: Create a permanent notification?
    default: false
    selector:
      boolean: null
    example: 'True'
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ notify_alexa }}'
        sequence:
          - service: notify.alexa_media
            data:
              title: '{{ title }}'
              message: '{{ name }} {{ message }}'
              target: '{{ alexa_entities.get("entity_id") }}'
              data:
                type: announce
                method: all
      - conditions:
          - condition: template
            value_template: '{{ permanent }}'
        sequence:
          - service: persistent_notification.create
            data:
              title: '{{ title }}'
              message: '** {{ name }} ** {{ message }}'
      - conditions:
          - condition: template
            value_template: '{{ permanent }}'
        sequence:
          - service: persistent_notification.create
            data:
              title: '{{ title }}'
              message: '** {{ name }} ** {{ message }}'
      - conditions:
          - condition: template
            value_template: '{{ mobile }}'
        sequence:
          - service: notify.ajith
            data:
              title: '{{ title }}'
              message: '{{ name }} {{ message }}'
      - conditions:
          - condition: template
            value_template: '{{ logbook }}'
        sequence:
          - service: logbook.log
            data:
              name: '{{ name }}'
              message: '{{ message }}'
      - conditions:
          - condition: template
            value_template: '{{ notify_google_home }}'
        sequence:
          - service: media_player.play_media
            target:
              entity_id: |
                '{{ entities.get("entity_id") }}'
            data:
              media_content_id: >-
                media-source://tts/google_translate?message="{{ name | replace("
                ", "+") }} + {{ message | replace(" ", "+") }}"
              media_content_type: provider
            metadata:
              title: 'Back Door is open '
              thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
              media_class: app
              children_media_class: null
              navigateIds:
                - {}
                - media_content_type: app
                  media_content_id: media-source://tts
                - media_content_type: provider
                  media_content_id: >-
                    media-source://tts/google_translate?message="{{ name |
                    replace(" ", "+") }}+{{ message | replace(" ", "+") }}"
mode: single
icon: mdi:speaker-wireless

2 Likes

just saw this reply, thanks.

im still pretty green with this stuff, to use this code do i create a script and paste it in the yaml, then can i call the script in a automation as a notify service or what?

thanks in advance

Paste this in yaml and change mobile notification to point to your phone

If anybody can convert this to a blueprint please do so, it will be a really good value add for the community

Thank you for your work, wanted to create something similar with multiple notifications and supporting few more integrations. So I modified your script for my needs, you work helped me a lot.
I publish my modified version here, someone might find it useful.
Message Types can be localized to any language (in array and dropdown the strings must be identical)…

    notify_all:
    alias: Script for Notifying multiple devices
    description: Send notifications to multiple devices
    variables:
      message_types: {"Information":"info", "Warning":"warning", "Alert":"error"}
    fields:
      message_type:
        name: "Message type"
        required: true
        example: warning
        selector:
          select:
            mode: dropdown
            options:
              - ""
              - "Information"
              - "Warning"
              - "Alert"
      title:
        name: "Title"
        description: Title for notification
        required: true
        selector:
          text: null
        example: Notification Title
      message:
        name: "Message"
        description: Message content of the notification.
        required: true
        selector:
          text: null
        example: Notifications are awesome
      tts_google_entities:
        name: "Google TTS Entities"
        description: List of Media Players to notify via Google_Translate TTS
        selector:
          target:
            entity:
              domain: media_player
        example: media_player.kitchen_display
      tts_cache:
        name: "Cache TTS audio file"
        description: Cache the generated audio file for offline use
        default: false
        selector:
          boolean: null
        example: 'True'
      alexa_entities:
        name: "Alexa Entities"
        description: List of Alexa Media Players to notify
        selector:
          target:
            entity:
              domain: media_player
        example: media_player.kitchen_display
      kodi:
        name: "Kodi"
        description: Send notification to KODI TVbox (if online)
        default: true
        selector:
          boolean: null
        example: 'True'
      enigma:
        name: "Enigma"
        description: Send notification to Enigma receiver (if online)
        default: true
        selector:
          boolean: null
        example: 'True'
      pushbullet:
        name: "Pushbullet"
        description: "Send notification via Pushbullet service"
        default: true
        selector:
          boolean: null
        example: 'True'
      client:
        name: "Client mobile App"
        description: Send notification to HA Client mobile App
        default: false
        selector:
          boolean: null
        example: 'True'
      sms:
        name: "SMS"
        description: Send SMS notification to cellphone
        default: false
        selector:
          boolean: null
        example: 'True'
      logbook:
        description: Add entry to HA Logbook
        name: "Logbook"
        default: true
        selector:
          boolean: null
        example: 'True'
      persistent:
        name: "Persistent"
        description: Create HA Persistent notification
        default: false
        selector:
          boolean: null
        example: 'True'
    sequence:
    - choose:
          - conditions:
              - condition: template
                value_template: '{{ kodi }}'
              - condition: template
                value_template: '{{ states.media_player.kodi.state is defined and states.media_player.kodi.state != "off" }}'
            sequence:
              - service: notify.kodi
                data:
                  title: '{{ title if title != "" else "HA Message"}}'
                  message: '{{ message }}'
                  data:
                    displaytime: 20000
                    icon: '{{ message_types[message_type] if meessage_type != "" else "info"}}'
    - choose:
          - conditions:
              - condition: template
                value_template: '{{ enigma }}'
              - condition: template
                value_template: '{{ states.media_player.vuplus.state is defined and states.media_player.vuplus.state != "off" }}'
            sequence:
             -  service: notify.enigma
                data:
                  message: '{{ message_type + " - " if message_type != "" }}{{ title + ": " if title != "" }}{{ message }}'
                  data:
                    displaytime: "20"
                    #messagetype: '{{ "\x22" + ((message_types.keys()|list).index(message_type) + 1) | string + "\x22" }}'
                    #messagetype: '"{{ 1 | string }}"'
                    messagetype: "1" # only static value works, not sure why templates(above) don't
    - choose:
          - conditions:
              - condition: template
                value_template: '{{ sms }}'
              - condition: template
                value_template: '{{ states.sensor.sms_myphone1.state is defined }}'
            sequence:
              - service: notify.sms
                data:
                  message: '{{ message_type + " - " if message_type != "" }}{{ title + ": " if title != "" }}{{ message }}'
                  target: "{{ states('sensor.sms_myphone1') }}"
    - choose:
          - conditions:
              - condition: template
                value_template: '{{ persistent }}'
            sequence:
              - service: persistent_notification.create
                data:
                  title: '{{ message_type + " - " if message_type != "" }}{{ title if title != "" else "HA Message"}}'
                  message: '{{ message }}'
    - choose:
          - conditions:
              - condition: template
                value_template: '{{ logbook }}'
            sequence:
              - service: logbook.log
                data:
                  name: '{{ message_type + " - " if message_type != "" }}{{ title + ": " if title != "" else "HA Message"}}'
                  message: '{{ message }}'
    - choose:
          - conditions:
              - condition: template
                value_template: '{{ pushbullet }}'
            sequence:
              - service: notify.notify
                data:
                  title: '{{ message_type + " - " if message_type != "" }}{{ title if title != "" else "HA Message"}}'
                  message: '{{ message }}'
    - choose:
          - conditions:
              - condition: template
                value_template: '{{ client }}'
            sequence:
              - service: notify.mobile_app_g8441
                data:
                  message: '{{ message_type + " - " if message_type != "" }}{{ title + ": " if title != "" }}{{ message }}'
    - choose:
          - conditions:
              - condition: template
                value_template: '{{ tts_google_entities is defined }}'
              - condition: template
                value_template: '{{ tts_google_entities.get("entity_id") | count > 0 }}'
            sequence:
              - service: tts.google_say
                data:
                  message: '{{ message_type + "!" if message_type != "" }}{{ title + "..." if title != "" }}{{ message }}'
                  entity_id: '{{ tts_google_entities.get("entity_id") }}'
                  language: sk
                  cache: '{{ tts_cache if tts_cache is defined else "false" }}'
    - choose:
          - conditions:
              - condition: template
                value_template: '{{ alexa_entities is defined }}'
              - condition: template
                value_template: '{{ alexa_entities.get("entity_id") | count > 0 }}'
            sequence:
              - service: notify.alexa_media
                data:
                  title: '{{ message_type + "!"  if message_type != "" }}{{ title + "..." if title != "" }}'
                  message: '{{ message }}'
                  target: '{{ alexa_entities.get("entity_id") }}'
                  data:
                    type: announce
                    method: all
    mode: single
    icon: mdi:speaker-wireless
3 Likes