Advanced Mobile Device Notifier Blueprint

I made this blueprint because i was tired of adding/changing groups when new phones are added to home assistant for notifications. Its my first attempt at a blueprint so would appreciate feedback.

Advanced Mobile Device Notifier Blueprint

Overview

This blueprint allows you to send customized notifications to mobile devices based on their location state. You can filter notifications by location (Home, Away, All Locations, or Custom Zone). The blueprint is compatible with both iOS and Android devices and supports both critical and non-critical notifications.


Features

  • Location-based Notification Filtering: Send notifications based on the current location state of the devices (Home, Away, All Locations, or Custom Zone).
  • Critical and Non-Critical Alerts: Option to send critical alerts (iOS) or high-priority notifications (Android) that override silent modes.
  • Customizable Sounds: Choose from default or custom notification sounds.
  • Mobile Device Compatibility: Works with devices integrated via the mobile_app integration in Home Assistant.

Requirements

  • Home Assistant setup with the mobile_app integration for mobile device tracking.
  • Mobile devices registered under the device_tracker entity.
  • A working zone configuration if using the Custom Zone filter.

Inputs

  • Location Filter: Select the filter for location state (Home, Away, All Locations, or Custom Zone).
  • Custom Zone Name: If Custom Zone is selected, specify the exact name of the zone.
  • Notification Title: The title of the notification.
  • Notification Message: The content of the notification.
  • Critical Alert: If enabled, the notification will be sent as a critical alert (iOS) or high-priority (Android).
  • Notification Sound: Choose a sound for the notification (default sound or specify a custom sound name).

How It Works

  1. Location Filtering:
    • Based on the Location Filter selected, the blueprint identifies which mobile devices meet the criteria.
  2. Notification Sending:
    • Notifications are sent to devices matching the filter conditions. If Critical Alert is enabled, notifications will override silent modes (iOS) or be marked high-priority (Android).
  3. Logging:
    • The blueprint logs information about which devices received the notification and the parameters of the notification.

Example Use Cases

  • Alert when family arrives home: Use the Home filter to notify when someone arrives.
  • Notify when a device leaves a specific area: Use the Away filter to send notifications when someone leaves a location.
  • Send an urgent alert to all family members: Enable Critical Alert to override silent mode for critical situations.

Logs

  • Logbook: Records the notifications sent, including details like the notification title and message.
  • System Log: Logs the devices that received the notification and details such as sound and priority.

Troubleshooting

If notifications are not working as expected, check the following:

  • Device Integration: Ensure devices are correctly integrated and tracked via the device_tracker entity.
  • Filter Criteria: Confirm the correct Location Filter is selected and that devices meet the criteria.
  • System Logs: Check logs for issues in notification delivery or blueprint execution.

Example YAML

Below is an example YAML configuration for the blueprint:

blueprint:
  name: Advanced Mobile Device Notifier
  author: debonisd
  description: >
    Send customized notifications to mobile devices based on their location state.
    Supports filtering by home, away, all locations, or a specific zone.
    Compatible with both iOS and Android devices, with options for critical and non-critical alerts.
  domain: script
  input:
    filter_state:
      name: Location Filter
      description: >
        Choose which devices to notify based on their current location state.
      selector:
        select:
          options:
            - label: Home
              value: home
            - label: Away
              value: away
            - label: All Locations
              value: all
            - label: Custom Zone
              value: custom_zone
      default: all

    custom_zone_name:
      name: Custom Zone Name
      description: >
        If 'Custom Zone' is selected above, enter the exact name of the zone here (case-sensitive).
        Leave empty if not using a custom zone.
      default: ""
      selector:
        text:

    notification_title:
      name: Notification Title
      description: The title of the notification.
      default: ""
      selector:
        text:

    notification_message:
      name: Notification Message
      description: The main content of the notification.
      default: ""
      selector:
        text:

    critical_alert:
      name: Critical Alert
      description: >
        If enabled, the notification will be sent as a critical alert (iOS) or high priority (Android).
        Use this for important notifications that should override silent modes.
      default: false
      selector:
        boolean:

    notification_sound:
      name: Notification Sound
      description: >
        Choose a sound for the notification. Use 'default' for the default sound,
        or specify a valid sound name for the device.
      default: "default"
      selector:
        text:

Final Touch

Note: If you encounter any issues or have suggestions, feel free to open an issue or submit a pull request in this repository.


YAML

blueprint:
  name: Advanced Mobile Device Notifier
  author: dadeboni
  source: https://github.com/dadeboni/HomeAssistant-Config/blob/master/dynamic_notification.yaml
  description: >
    Send customized notifications to mobile devices based on their location state.
    Supports filtering by home, away, all locations, or a specific zone.
    Compatible with both iOS and Android devices, with options for critical and non-critical alerts.
  domain: script
  input:
    filter_state:
      name: Location Filter
      description: >
        Choose which devices to notify based on their current location state.
      selector:
        select:
          options:
            - label: Home
              value: home
            - label: Away
              value: away
            - label: All Locations
              value: all
            - label: Custom Zone
              value: custom_zone
      default: all

    custom_zone_name:
      name: Custom Zone Name
      description: >
        If 'Custom Zone' is selected above, enter the exact name of the zone here (case-sensitive).
        Leave empty if not using a custom zone.
      default: ""
      selector:
        text:

    notification_title:
      name: Notification Title
      description: The title of the notification.
      default: ""
      selector:
        text:

    notification_message:
      name: Notification Message
      description: The main content of the notification.
      default: ""
      selector:
        text:

    critical_alert:
      name: Critical Alert
      description: >
        If enabled, the notification will be sent as a critical alert (iOS) or high priority (Android).
        Use this for important notifications that should override silent modes.
      default: false
      selector:
        boolean:

    notification_sound:
      name: Notification Sound
      description: >
        Choose a sound for the notification. Use 'default' for the default sound,
        or specify a valid sound name for the device.
      default: "default"
      selector:
        text:

mode: parallel
max_exceeded: silent

variables:
  filter_state: !input filter_state
  custom_zone_name: !input custom_zone_name
  notification_title: !input notification_title
  notification_message: !input notification_message
  critical_alert: !input critical_alert
  notification_sound: !input notification_sound
  home_states: ['home', 'Home']
  all_zones: "{{ states.zone | map(attribute='name') | list }}"
  filtered_states: >
    {% if filter_state == 'custom_zone' %}
      [custom_zone_name]
    {% elif filter_state == 'home' %}
      home_states
    {% elif filter_state == 'away' %}
      ['not_home']
    {% else %}
      home_states + ['not_home'] + all_zones
    {% endif %}
  home_devices: >
    {{ states.device_tracker 
       | selectattr('state', 'in', filtered_states)
       | map(attribute='entity_id')
       | list }}
  mobile_app_devices: >
    {{ integration_entities('mobile_app') 
       | select('match', 'device_tracker') 
       | list }}
  notify_services: >
    {{ home_devices 
       | select('in', mobile_app_devices)
       | map('regex_replace', '^device_tracker\\.', 'notify.mobile_app_')
       | list }}

sequence:
  - repeat:
      for_each: "{{ notify_services }}"
      sequence:
        - service: "{{ repeat.item }}"
          data:
            title: "{{ notification_title }}"
            message: "{{ notification_message }}"
            data:
              push:
                sound: "{{ notification_sound }}"
                critical: "{{ 1 if critical_alert else 0 }}"
                volume: "{{ 1.0 if critical_alert else None }}"
                interruption-level: "{{ 'critical' if critical_alert else 'active' }}"
              apns_headers:
                apns-push-type: >-
                  {{ 'critical' if critical_alert and 'ios' in repeat.item else 'alert' }}
              category: >-
                {{ 'TIME_SENSITIVE' if critical_alert and 'ios' in repeat.item else '' }}

  - service: logbook.log
    data:
      name: "Notify Mobile Devices: Priority"
      message: >-
        Sent notification with Title: "{{ notification_title if notification_title is defined else 'Default Title' }}"
        Message: "{{ notification_message if notification_message is defined else 'No message provided' }}"
        To: {{ notify_services | join(', ') if notify_services else 'No devices found' }}

  - service: system_log.write
    data:
      level: debug
      message: >-
        Sent notifications to the following devices:
        {{ notify_services | join(', ') }}

  - service: system_log.write
    data:
      level: info
      message: >-
        Notification Parameters:
        Title: "{{ notification_title }}"
        Message: "{{ notification_message }}"
        Critical: {{ critical_alert }}
        Sound: "{{ notification_sound }}"

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

1 Like

I donā€™t understand the purpose of thisā€¦ :man_shrugging:
It seems to send a message to all ā€œmobile_app_ā€¦ā€ devices that have the same suffix of an already present DeviceTracker (this supposition could be wrong in many cases :sweat_smile:)

So all registered mobile apps will receive the same message: children, grannies or guests seems the same for you :man_facepalming:

Traditionally if you want to send groups notifications you have to make a notify group. Then if you get a new phone or change your phone name you have to update the notification group in your configuration.yaml. This will dynamically pick them up.

Also sometimes you want to send notifications if something important is going on in the house so I can just send the alerts to the people home.

All that being said I was tinkering around with my setup and trying to find areas to make more dynamic and it was fun lol

So why do not create a ā€œdynamic groupā€ like this already existent Blueprint? (maybe few edits are needed to include device_tracker(s) and take-off ā€œmobile_appā€¦ā€ entities that are present in a specified Zone)
Home Assistant Blueprint: Create a group with either dynamic or with static members. Ā· GitHub

Using a Script will force users to abandon normal ā€œnotify.xxxxā€ actions and obey to use a complete different and not-standard way to send (push only) notifications.

To my knowledge the dynamic group doesnā€™t work for notify groups only for standard groups. I could certainly make modifications to this to include other entities not just mobile phone.

1 Like

Hi Domenic. Nice that you made your first blueprint. Thanks for sharing! I havenā€™t implemented it yet but my use case is this:
My household has three iPhones and an iPad currently in home assistant utilizing HA app tracking and icloud3. I would like an easy way to manage notifications for each saved location to each mobile device. As of now I am creating automations to do this and as you can imagine it is very tedious since we have 25 saved locations. Will your blueprint help me easily configure and manage these alerts? Or do the alerts in your blueprint automatically go to all mobile devices on the account?

Thanks!

The blueprint will automatically pick up all your devices as well as iterate over all your stored zonesā€¦ in the location filter home will send to all devices that are at home. Away will send to all devices not_home and custom will send a notification to the devices in a named zone.

Let me know if that helps, if not then Iā€™m sure I can make a change for you

I appreciate the reply. For my use, I would to be able to customize who gets notifications for each zone. The biggest factor is that my wife and I always want to get updates when the other person enters or leaves any zone, but my daughter will not want all of those. She may only want to know when we enter or leave home zone, and maybe when I enter and leave the office zone. I dont expect you to make changes per my request but I do sincerely appreciate the follow-up! Let me know if what Iā€™m asking for is do-able.
Iā€™ve only been using HA for a month and a half but I do have a fair bit of experience with some other programming languages so Iā€™m pretty tech savvy. My family has used Life 360 for a couple years and although itā€™s great, I feel like theyā€™ve priced themselves out of my interest. Iā€™m trying to replicate what they offer(ed) with HA, and Iā€™m on track with that, but the tedious nature of setting up each notifier automation made me think there has to be a better way. Someday I may be able to create my own dashboard with a checkbox by each zone and one by each mobile device to allow me to freely determine which location notifies whom, but Iā€™m not there at this point. A google search brought me to this thread! Cheers!

I think this is doable ! Based on your request, Iā€™ve made some adjustments to the blueprint to allow for zone-specific recipient mapping. Hereā€™s the updated blueprint:

blueprint:
  name: Advanced Mobile Device Notifier with Zone-Recipient Mapping
  author: dadeboni
  description: >
    Send customized notifications to mobile devices based on their location state or specific zones.
    Supports filtering by home, away, all locations, or custom zones, with recipient mappings for specific zones.
  domain: script
  source_url: https://github.com/dadeboni/HomeAssistant-Config/blob/923c8e858cdc8b7a8fa58b0588849a9683a83171/dynamic_notification.yaml
  input:
    filter_state:
      name: Location Filter
      description: >
        Choose which devices to notify based on their current location state.
        If you want specific notifications based on zones, leave this as 'all'.
      selector:
        select:
          options:
            - label: Home
              value: home
            - label: Away
              value: away
            - label: All Locations
              value: all
            - label: Custom Zone
              value: custom_zone
      default: all

    custom_zone_name:
      name: Custom Zone Name
      description: >
        If 'Custom Zone' is selected above, enter the exact name of the zone here (case-sensitive).
        Leave empty if not using a custom zone.
      default: ""
      selector:
        text:

    zone_recipients:
      name: Zone Recipients Mapping
      description: >
        Map zones to specific recipients. Each zone can notify different devices.
        Leave zones blank if no one should receive notifications for them.
      default: []
      selector:
        object:
          - zone: string
            recipients:
              selector:
                entity:
                  domain: notify
                  multiple: true

    notification_title:
      name: Notification Title
      description: The title of the notification.
      default: ""
      selector:
        text:

    notification_message:
      name: Notification Message
      description: The main content of the notification.
      default: ""
      selector:
        text:

    critical_alert:
      name: Critical Alert
      description: >
        If enabled, the notification will be sent as a critical alert (iOS) or high priority (Android).
        Use this for important notifications that should override silent modes.
      default: false
      selector:
        boolean:

    notification_sound:
      name: Notification Sound
      description: >
        Choose a sound for the notification. Use 'default' for the default sound,
        or specify a valid sound name for the device.
      default: "default"
      selector:
        text:

mode: parallel
max_exceeded: silent

# Define variables for filtered states, home devices, and notification services
variables:
  filter_state: !input filter_state
  custom_zone_name: !input custom_zone_name
  zone_recipients: !input zone_recipients
  notification_title: !input notification_title
  notification_message: !input notification_message
  critical_alert: !input critical_alert
  notification_sound: !input notification_sound
  home_states: ['home', 'Home']
  all_zones: "{{ states.zone | map(attribute='name') | list }}"

  # Filtered states based on the userā€™s selection
  filtered_states: >
    {% if filter_state == 'custom_zone' %}
      [custom_zone_name]
    {% elif filter_state == 'home' %}
      home_states
    {% elif filter_state == 'away' %}
      ['not_home']
    {% else %}
      home_states + ['not_home'] + all_zones
    {% endif %}

  # Match zone-recipients mapping to the current zone
  notify_services: >
    {% set matched_zone = zone_recipients | selectattr('zone', 'eq', custom_zone_name) | first %}
    {% if matched_zone and matched_zone.recipients %}
      {{ matched_zone.recipients }}
    {% else %}
      []  # No recipients for this zone
    {% endif %}

sequence:
  - repeat:
      for_each: "{{ notify_services }}"
      sequence:
        - service: "{{ repeat.item }}"
          data:
            title: "{{ notification_title }}"
            message: "{{ notification_message }}"
            data:
              push:
                sound: "{{ notification_sound }}"
                critical: "{{ 1 if critical_alert else 0 }}"
                volume: "{{ 1.0 if critical_alert else None }}"
                interruption-level: "{{ 'critical' if critical_alert else 'active' }}"
              apns_headers:
                apns-push-type: >-
                  {{ 'critical' if critical_alert and 'ios' in repeat.item else 'alert' }}
              category: >-
                {{ 'TIME_SENSITIVE' if critical_alert and 'ios' in repeat.item else '' }}

  - service: logbook.log
    data:
      name: "Notify Mobile Devices: Priority"
      message: >-
        Sent notification with Title: "{{ notification_title if notification_title is defined else 'Default Title' }}"
        Message: "{{ notification_message if notification_message is defined else 'No message provided' }}"
        To: {{ notify_services | join(', ') if notify_services else 'No devices found' }}

  - service: system_log.write
    data:
      level: debug
      message: >-
        Sent notifications to the following devices:
        {{ notify_services | join(', ') }} ```

Outstanding! I will give it a try in the morning and report back. Thank you!

I am getting this error in the ā€œnameā€ of the blueprint on the blueprint page.

dadeboni/dynamic_notification.yaml could not be loaded

Invalid blueprint: extra keys not allowed @ data[ā€˜blueprintā€™][ā€˜inputā€™][ā€˜zone_recipientsā€™][ā€˜selectorā€™][ā€˜additional_propertiesā€™]. Got None extra keys not allowed @ data[ā€˜blueprintā€™][ā€˜inputā€™][ā€˜zone_recipientsā€™][ā€˜selectorā€™][ā€˜propertiesā€™]. Got None

Thanks Iā€™ll take a look. I edited the code from my phone so Iā€™m sure I screwed something simple up lol.

after re-thinking your use case i think these changes would be more what you are looking for

blueprint:
  name: Zone-Based Notifier
  author: dadeboni
  description: >
    Notify specific devices when a person enters or leaves a zone. 
    Allows mapping of zones to devices for granular control over notifications.
  domain: automation
  input:
    device_tracker_entities:
      name: Device Tracker Entities
      description: >
        List of device trackers to monitor. Example:
        `device_tracker.person_1, device_tracker.person_2`.
      selector:
        entity:
          domain: device_tracker
          multiple: true

    zone_device_map:
      name: Zone-Device Map
      description: >
        Map zones to devices that should receive notifications. 
        Example: {"zone.home": ["notify.mobile_app_me"], "zone.work": ["notify.mobile_app_wife"]}.
      selector:
        object: {}

    notification_title:
      name: Notification Title
      description: The title of the notification (dynamic placeholders supported).
      default: "Zone Notification"
      selector:
        text:

    notification_message:
      name: Notification Message
      description: >
        The body of the notification. Use `{person}` and `{zone}` as placeholders.
        Example: `{person} has entered {zone}`.
      default: "{person} has entered {zone}"
      selector:
        text:

    enter_leave:
      name: Enter or Leave
      description: >
        Specify if the automation should trigger on entering, leaving, or both.
      default: both
      selector:
        select:
          options:
            - both
            - entering
            - leaving

trigger:
  - platform: state
    entity_id: !input device_tracker_entities
    for:
      hours: 0
      minutes: 0
      seconds: 0

condition: []

action:
  - variables:
      current_zone: "{{ trigger.to_state.state }}"
      person: "{{ trigger.to_state.attributes.friendly_name }}"
      notify_services: "{{ zone_device_map.get(current_zone, []) }}"

  - choose:
      - conditions:
          - condition: template
            value_template: >
              {% set transition = (trigger.from_state.state, trigger.to_state.state) %}
              {% if enter_leave == 'both' %}
                true
              {% elif enter_leave == 'entering' and transition[1] in zone_device_map %}
                true
              {% elif enter_leave == 'leaving' and transition[0] in zone_device_map %}
                true
              {% else %}
                false
              {% endif %}
        sequence:
          - service: "{{ notify_services | join(', ') }}"
            data:
              title: "{{ notification_title }}"
              message: "{{ notification_message | replace('{person}', person) | replace('{zone}', current_zone) }}"
  - service: logbook.log
    data:
      name: "Zone Notification"
      message: >
        Sent notification to: {{ notify_services | join(', ') }}
        Title: "{{ notification_title }}"
        Message: "{{ notification_message }}"
        Zone: "{{ current_zone }}"

Zone-Based Notifier Automation Blueprint
Description

This automation blueprint allows you to send notifications to specific devices when a person enters or leaves a defined zone. You can map zones to a list of devices to receive notifications, offering granular control over who gets notified based on the location changes of the people in your home.
Features

Device Tracker Entities: Monitor multiple device_tracker entities (e.g., family members) for zone-based events.
Zone-Device Mapping: Customize which devices receive notifications for each zone (e.g., notify mobile devices when someone enters or leaves the home zone).
Notification Customization: Customize the title and message of the notifications. Use placeholders like {person} and {zone} to dynamically insert the personā€™s name and the zone name.
Enter/Leave Event Control: Choose whether the automation should trigger for entering, leaving, or both events.
Granular Notification Control: Tailor notifications to specific devices depending on which zone is entered or left.

Inputs
device_tracker_entities

Type: entity
Description: List of device_tracker entities to monitor for zone-based events.
Example: device_tracker.person_1, device_tracker.person_2
Multiple: Yes (can select multiple entities)

zone_device_map

Type: object
Description: A mapping of zones to devices that should receive notifications when someone enters or leaves a zone. The key is the zone name, and the value is a list of devices to be notified.
Example:

zone_device_map:
  zone.home:
    - notify.mobile_app_me
    - notify.mobile_app_wife
  zone.work:
    - notify.mobile_app_me

In this example, whenever someone enters or leaves the zone.home, both notify.mobile_app_me and notify.mobile_app_wife will receive notifications. Similarly, when entering or leaving the zone.work, only notify.mobile_app_me will be notified.

notification_title

Type: text
Description: The title of the notification. You can use dynamic placeholders like {person} and {zone}.
Default: "Zone Notification"
Example: "Entering {zone}: {person}"

notification_message

Type: text
Description: The body of the notification. Use placeholders like {person} and {zone}.
Default: "{person} has entered {zone}"
Example: "{person} has entered {zone}"

enter_leave

Type: select
Description: Determines if the automation should trigger on entering, leaving, or both events.
Options:
    both: Triggers on both entering and leaving a zone.
    entering: Triggers only when entering a zone.
    leaving: Triggers only when leaving a zone.
Default: both

Triggers

The automation is triggered whenever a monitored device_tracker entity changes its state. This could be when a person enters or leaves a specific zone.
Action Logic

Zone Detection: The zone entered or left by the device_tracker is detected.
Device Notification: The automation retrieves the list of devices associated with that zone from the zone_device_map.
Notification Delivery: If the conditions are met (based on the enter_leave setting), the specified devices will receive a notification with a title and message. The message is dynamically populated with the person's name and the zone name.
Logbook Entry: A logbook entry is created to keep track of the notification that was sent, including the devices notified, the message, and the zone.

Example
Example 1: Basic Configuration

Device Trackers: device_tracker.person_1, device_tracker.person_2
Zone-Device Map:

zone_device_map:
  zone.home:
    - notify.mobile_app_me
    - notify.mobile_app_wife
  zone.work:
    - notify.mobile_app_me

Notification Title: "Entering {zone}: {person}"
Notification Message: "{person} has entered {zone}"
Enter/Leave Event: both

In this example, whenever device_tracker.person_1 or device_tracker.person_2 enters or leaves the zone.home, both notify.mobile_app_me and notify.mobile_app_wife will receive a notification with the title ā€œEntering home: John Doeā€ and the message ā€œJohn Doe has entered homeā€.

Similarly, if the zone.work is entered or left, only notify.mobile_app_me will receive the notification.
Notes

Make sure that the devices you're trying to notify are correctly set up in Home Assistant and available for notification services.
This blueprint does not dynamically pull zone names from your Home Assistant setup. You need to manually define the zones you want to monitor in the zone_device_map.
Ensure that your device_tracker entities are correctly configured and able to track state changes (i.e., entering or leaving zones).

Troubleshooting

If youā€™re experiencing issues, check the following:

Ensure all entities in device_tracker_entities are valid and properly configured.
Double-check the zones defined in zone_device_map and ensure the correct notification services are mapped.
Review Home Assistant's logbook and automation logs for any errors related to entity states or actions.