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