Question about mobile notifications

(1) It seems there are two ways to call the mobile app notification. Is there a difference?

action:
  - service: notify.mobile_app_<your_device_id_here>
    data:
    message: "Notification text"
  action:
  - device_id: 40e14b2f45f098cb1524fecd45624bb1
    domain: mobile_app
    type: notify
    message: "Notification text"

(2) Both methods seem to “hardcode” the mobile device. Is there a recommended way to make this more device independent?

You can’t globally change a device id in one place like you can an entity id. Well not without delving into the hidden .storage folder. Which is not meant to be user editable.

So if you replace your phone it would be much easier if you use the first version. You can change your new phone name to be the same as the old one. Then all your automated notifications just work.

If you use device ids everywhere then the new phone will have a different one. That is not as easy to change.

Though what you have shown in your first example is incorrect. It should be:

action:
  - service: notify.mobile_app_<your_device_id_here>
    data:
      message: "Notification text"

Yes, indentation does matter. Would it be possible to set up a group of mobile devices, add one or more devices to the group and send a notification to all devices to the group? In that case, you would not have to touch the automation.

Take a look at notification groups

Cool. It would create a new notification group and I could add the mobile in the group and use the group in the automation.

Could the notification group be used to call scripts with custom fields?

I don’t understand your question. How should a group be used to call a script? Maybe you rather explain what you want to do

Currently have three actions which do an email, mobile app, and persistent notification (still under construction).

  action:
  - service: script.email_snapshot
    data:
      snapshot_target: *e-mail address*
      snapshot_title: '{{ title }}'
      snapshot_message: '{{ message }}'
      snapshot_file: '{{ filename }}'
  - service: script.persistent_notification_snapshot
    data:
      snapshot_title: '{{ title }}'
      snapshot_message: '{{ message }}'
      snapshot_file: '{{ filename }}'
  - service: script.ios_notification_snapshot
    data:
      snapshot_title: '{{ title }}'
      snapshot_message: '{{ message }}'
      snapshot_file: '{{ filename }}'

The objective is to send a snapshot image to each of the notifications. To create some consistency, remove some complexity and make the code reusable, I created a script for each of them.

Just curios whether using a notification group, I could simplify it.

The original question also came from the above, e.g. remove the mobile device from the script.

Hard to tell without knowing what the scripts do. You also need to keep in mind that the different notify services don’t all support the same features. E.g. a mobile phone notification can be actionable, whereas an E-Mail doesn’t support this.

Another question related to mobile notifications. I have the following actions in a script:

  variables:
    target_device: 40d14a2e45e098ba1524edbc45624aa1
  sequence:
  - device_id: 40e14b2f45f098cb1524fecd45624bb1
    domain: mobile_app
    type: notify
    message: "Notification text"
    title: "Notification title"
  - device_id: '40e14b2f45f098cb1524fecd45624bb1'
    domain: mobile_app
    type: notify
    message: "Notification text 2"
    title: "Notification title 2"
  - device_id: '{{ target_device }}'
    domain: mobile_app
    type: notify
    message: "Notification text 3"
    title: "Notification title 3"

The third action gives an error

  - device_id: '{{ target_device }}'

Error: Unable to resolve webhook ID from the device ID

Any idea how to pass a variable or field to the device_id?

Don’t use device automations, use entity automations instead. If you ever replace a device, you need to replace the device id everywhere in your automations, with entity ids, you just change the entity_id of the new device to be the same as the one you had before and you’re done.

I think device automations don’t even support templates as you did.

I just found a comment from @tom_l here Allow scene.create to use device_id instead of entity_id - Feature Requests - Home Assistant Community (home-assistant.io) which seems to indicate that I cannot use a template with a device_id.

In the GUI, it supports the following action types: Condition, Delay, Fire Event, Activate Scene, Call Service, Wait, Wait for Trigger, Repeat, Choose, and Device. It does not seem to include Entity.

The other question though would be though, would I be able to send a notification to mobile phone entity as the only entities I see are device_trackers and sensors?

The blueprint provided by @vorion does use the following code for mobile notification (shortened to relevant code) Send camera snapshot notification on motion - Blueprints Exchange - Home Assistant Community (home-assistant.io)

blueprint:
  name: Send a camera snapshot when motion is detected
  domain: automation
  input:
    notify_device:
      name: Device to notify
      description: Device needs to run the official Home Assistant app to receive notifications
      selector:
        device:
          integration: mobile_app
action:
  - device_id: !input notify_device
    domain: mobile_app
    type: notify

The code in automations.yaml looks like:

  use_blueprint:
    path: vorion/send-camera-snapshot-notification-on-motion.yaml
    input:
      notify_device: 40e14b2f45f098cb1524fecd45624bb1

The code is able to refer to the device_id, however, I try to pass the device_id to a script.

  - device_id: !input notify_device

Sorry for the confusion, you don’t have an entity_id for notification, instead you have a specific service for each mobile, e.g. notify.your_phone or notify.mail. However, it’s still better than a device_id, because you can change the notify service name when you get a new phone to the same one as you had before.
You can see the different notify services under Developer Tools → Services

You’d use call service here.

E.g.

service: "notify.{{ mobile_id }}"
data:
  title: "Notification title"
  message: "Notification message"

I try to create script which defines a field for a mobile phone.

ios_notification_snapshot:
  alias: iOS Notification Snapshot
  description: This script sends a persistent notification a snapshot
  icon: mdi:email-outline
  mode: single
  fields:
    snapshot_target:
      name: Mobile
      description: Mobile phone device
      selector:
        device:
  sequence:
  - device_id: snapshot_target
    domain: mobile_app
    type: notify
    message: ""
    title: ""

When adding this script as an action to an automation using the GUI, it will allow me to select the mobile device. The resulting code in automations.yaml is

  action:
  - service: script.ios_notification_snapshot
    data:
      snapshot_target: 40e14b2f45f098cb1524fecd45624bb1

The device_id is inserted by the GUI when I choose the mobile. I don’t seem to be able to call the notification in the script. It give me an Error: Unable to resolve webhook ID from the device ID unless I hardcode the device id in the script.

Could I get the device_name from the device_id and create a service name to call the notification service?

Yes, I can. This is the code for the script I came up with:

  fields:
    snapshot_target:
      name: Mobile
      description: Mobile phone device
      selector:
        device:
  sequence:
  - service: 'notify.mobile_app_{{ device_attr(snapshot_target, "name") | replace("’","") | replace(" ","_") | lower}}'

Interesting solution. Using what you have already, you could also then narrow the available devices to just ones that use the mobile_app integration (ignoring, for instance, devices discovered by a router integration), like so:

    selector:
      target:
        device:
          integration: "mobile_app"

Actually, that allows for picking entities or areas as well. Maybe this is better:

    selector:
      device:
        manufacturer: Apple
        #integration: "mobile_app"

What’s strange is that I tried it using just integration: “mobile_app” first (w/o a manufacturer), and it didn’t seem to filter anything. I even added an integration I do not use, zha, which should have resulted in no entities in the script selector, and it still seemed to list all entities associated with devices. Bug with the selector code perhaps? It works using the target selector, but that then allows for the areas/entities.

Thanks for the suggestions. I get the same result; I also tried “Mobile App”. When looking at the device attributes, nothing is returned for “integration” while “manufacturer” and “model” do return the manufacturer’s and model’s name:

{{ device_attr("40e14b2f45f098cb1524fecd45624bb1", "integration") }}
{{ device_attr("40e14b2f45f098cb1524fecd45624bb1", "manufacturer") }}
{{ device_attr("40e14b2f45f098cb1524fecd45624bb1", "model") }}

Using the manufacturer attribute in the selector is definitely an improvement. Not being able to use the integration is unfortunate, because I get two devices for my iPhone, one through the mobile app integration and the other through the iCloud integration.

I know it’s been quite a long time, but I recently had a reason to revisit this topic, and noticed that the Home Assistant added a slugify filter to the template engine not long after this topic, so you could now simplify the template to:

notify.mobile_app_{{ device_attr(snapshot_target, "name") | slugify }}