My Flexible Mobile Notification Script

I’m sure there are many similar scripts, but this is what worked for me! The main point was just avoiding needing to go through every automation that sent a mobile notification and updating it when someone gets a new phone.

This script makes it super convenient to send notifications, without needing to remember the exact actions to call, or a lot of copy-pasted logic to check if people are home before notifying them. It does require that every Person you want to notify has their mobile device set as belonging to them, but that should already be the case!

Just select the Person entities you want to notify, type in your message, and you’re good to go!

alias: Notify People (Mobile)
description: >-
  Send a mobile notification to all mobile devices belonging to the specified
  people. Using this means not needing to update whatever automation calls it at
  all when someone gets a new phone!
mode: parallel
max: 10
fields:
  persons:
    name: People
    description: People whose mobile devices will be notified
    required: true
    selector:
      entity:
        domain: person
        multiple: true
  message:
    name: Message
    required: true
    selector:
      text: null
  title:
    name: Title
    required: false
    selector:
      text: null
  tag:
    name: Tag
    description: Used to replace or dismiss a prior notification
    required: false
    selector:
      text: null
  actions:
    name: Actions
    description: List of action buttons objects
    required: false
    selector:
      object: null
  extra_data:
    name: Extra Data
    description: >-
      Additional fields merged into the notification data payload (e.g. channel,
      ttl, priority). Explicit fields like tag and actions take precedence if
      there's a conflict.
    required: false
    selector:
      object: null
  only_if_home:
    name: Only If Home
    description: Only notify people who are currently home
    required: false
    default: false
    selector:
      boolean: null
sequence:
  - variables:
      notify_targets: >
        {% set ns = namespace(targets=[]) %} {% set mobile_trackers =
        integration_entities('mobile_app')
           | select('match', 'device_tracker\\.') | list %}
        {% for person in persons %}
          {% if not (only_if_home | default(false)) or states(person) == 'home' %}
            {% for tracker in state_attr(person, 'device_trackers') | default([]) %}
              {% if tracker in mobile_trackers %}
                {% set ns.targets = ns.targets + ['notify.mobile_app_' + tracker.split('.')[1]] %}
              {% endif %}
            {% endfor %}
          {% endif %}
        {% endfor %} {{ ns.targets | unique | list }}
  - repeat:
      for_each: "{{ notify_targets }}"
      sequence:
        - action: "{{ repeat.item }}"
          data: >
            {% set d = {'message': message} %} {% if title is defined and title
            %}
              {% set d = dict(d, title=title) %}
            {% endif %} {% set inner = (extra_data if (extra_data is defined and
            extra_data) else {}) %} {% if tag is defined and tag %}
              {% set inner = dict(inner, tag=tag) %}
            {% endif %} {% if actions is defined and actions %}
              {% set inner = dict(inner, actions=actions) %}
            {% endif %} {% if inner %}
              {% set d = dict(d, data=inner) %}
            {% endif %} {{ d }}

I also have a script just for clearing notifications, bit more convenient than using the full script:

alias: Clear People Notifications (Mobile)
description: >-
  Clear a tagged mobile notification from all mobile devices belonging to the
  specified people.
mode: parallel
max: 10
fields:
  persons:
    name: People
    description: People whose mobile devices will have the notification cleared
    required: true
    selector:
      entity:
        domain: person
        multiple: true
  tag:
    name: Tag
    description: Tag of the notification to clear
    required: true
    selector:
      text: null
sequence:
  - variables:
      notify_targets: >
        {% set ns = namespace(targets=[]) %} {% set mobile_trackers =
        integration_entities('mobile_app')
           | select('match', 'device_tracker\\.') | list %}
        {% for person in persons %}
          {% for tracker in state_attr(person, 'device_trackers') | default([]) %}
            {% if tracker in mobile_trackers %}
              {% set ns.targets = ns.targets + ['notify.mobile_app_' + tracker.split('.')[1]] %}
            {% endif %}
          {% endfor %}
        {% endfor %} {{ ns.targets | unique | list }}
  - repeat:
      for_each: "{{ notify_targets }}"
      sequence:
        - action: "{{ repeat.item }}"
          data:
            message: clear_notification
            data:
              tag: "{{ tag }}"