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:
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.
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.
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.
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?
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
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
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
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?
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:
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:
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: