Currently persons are a great way of dealing with the fact that a person’s location device (usually their phone) can change. When one of us gets a new phone, I just go to my People settings and change the Track Device for that particular person. In my automations and such, I always refer to the person entity which provides the location from the current tracking device.
That’s great, but the phone also has an attached notify.mobile_app entity which is not connected to the person in any way. So, when one of us changes phones, I have to go into any notification automations and change the notify.mobile_app entity accordingly.
What I’d like is to have that notify.mobile_app entity either change according to the “Track Device” (probably not the best way to do it, even though they have similar naming at the end (device_tracker.sm_f936u and notify.mobile_app_sm_f936u for example)) or have another entry in the person entity for “notification device”.
Alternately, is there some way to create an “alias” or the like so that I’d only have to change the name of my notify.mobile_app name once?
Make a notification group and just update the notification service in the group when the phone changes
- platform: group
name: "Russel"
services:
- service: notify.mobile_app_russel
Your other option is to not let the mobile app name your device. Put a name in it and it’ll use that name.
Companion app → Your profile → Details, Device Name field. The resulting notification service will be notify.mobile_app_<device_name>
I’ve actually tried that “device name” thing and it has worked but sometimes the app changed it back without my noticing it. (That was annoying.)
Anyway, thanks for the notification group solution. That should work nicely.
I assume that this also would work in the following way:
- platform: group
name: "Russel"
services:
- service: notify.mobile_app_russel
- platform: group
name: "whole_familty"
services:
- service: notify.mobile_app_russel
- service: notify.mobile_app_janette
- service: notify.mobile_app_kate
And, just to be sure, when I make the notification, how is that coded?
- alias: presence-notify of Russ' movement
trigger:
platform: state
entity_id: person.russell_smith
condition:
condition: template
value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
action:
# do I call the notification service like this for the group?
service: notify.Russel
data:
message: >
{% if is_state("person.russell_smith", "not_home") -%}
Russ is away
{%- else -%}
Russ is at {{ states("person.russell_smith") }}
{%- endif %}
Check in your services, it’ll let you know
Also, I’m having a little issue with converting the “platform: group” code to my set up. I have a specific group file in my configuration.yaml as follows:
group: !include groups.yaml
Within that file are entries like:
family:
name: family
entities:
- person.russell_smith
- person.janette
- person.katie
So how do I add that
- platform: group
name: "Russel"
services:
- service: notify.mobile_app_russell
? I’ve made several attempts. None seem to be allowed.
put it in configuration.yaml
notify:
- platform: group
name: "Russel"
services:
- service: notify.mobile_app_russell
got it. (both the configuration and the correct service call). Thanks
I figured out how to put the notifiers in their own file as well (not that hard).
In configuration.yaml:
notify: !include notify.yaml
In notify.yaml:
- platform: group
name: "Russel"
services:
- service: notify.mobile_app_russell
I have a little script that I use that does the hard lifting for you - looks up the person, figures out the tracking device, looks up the friendly name, then crunches that into something that can be prepended with notify.mobile_app…
I have (the usual) group for people in HA that have the app:
resident_people:
name: Resident People
entities:
- person.current_partner
- person.me_myself
- person.son_1
- person.son_2
Then I use this script:
alias: Send App Notification
sequence:
- variables:
call_list: >
{% set valid_people = state_attr('group.resident_people', 'entity_id') |
list %}
{% set excluded_entities = [] if exclude is not defined else exclude |
map('regex_replace', '^person\.', '') | map('regex_replace', '^',
'person.') | list %}
{% set potential_entities = valid_people if names is not defined else
names | map('regex_replace', '^person\.', '') | map('regex_replace',
'^', 'person.') | list %}
{% set named_entities = valid_people | select('in', potential_entities)
| list %}
{% set targets = named_entities | reject('in', excluded_entities) | list
if names is defined else valid_people | reject('in', excluded_entities)
| list %}
{% set notifiers = namespace(svc = []) %}
{% for target in targets %}
{% if state_attr(target, 'gps_accuracy') %}
{% set tracker = state_attr(target, 'source') %}
{% set mobile = state_attr(tracker, 'friendly_name') |
regex_replace('[\'\s]+', '_') | regex_replace('[\’]+', '') |
regex_replace('_+$', '') | lower %}
{% set notifiers.svc = notifiers.svc + ['notify.mobile_app_' + mobile] %}
{% endif %}
{% endfor %}
{{ notifiers.svc }}
- repeat:
count: "{{ call_list | count }}"
sequence:
- variables:
svc: "{{ call_list[repeat.index -1] }}"
- service: "{{ svc }}"
data: |
{ "message": "{{ message if message is defined else '' }}",
"title": "{{ title if title is defined else '' }}",
"data": {
"subtitle": "{{ subtitle if subtitle is defined else '' }}",
{% if critical is defined and critical %}
"importance": "high",
"push": { "sound": { "name": "default", "critical": 1, "volume": 1 } },
{% endif %}
"group": "{{ group if group is defined else 'info' }}",
"channel": "{{ group if group is defined else 'info' }}"
}
}
- delay:
hours: 0
minutes: 0
seconds: 1
milliseconds: 0
mode: queued
icon: mdi:bullhorn
max: 10
Call it with payload:
message: the message to send (should be something!)
names: an array of names (either with or without person. in front of them), if not specified everyone in the group
except: an array of names to NOT send to (again with or without person. in front)
title: optional title
subtitle: optional subtitle
critical: true or false (default false)
group: optional message group name
Some examples:
service: script.send_app_notification
data:
message: This is a test
names:
- me_myself
- person.son_1
title: A test
subtitle: Debugging
group: debug
service: script.send_app_notification
data:
message: This is a test
except:
- son_1
title: A test
group: debug
service: script.send_app_notification
data:
message: This is a test
critical: true
except:
- son_1
title: A test
Wow. A lot of effort went into getting that to work. That was kind of the point of my initial post.
What I ended up doing was writing a notify_[specific person] script for each person I want to inform and passing it the title and message. Then, when the app decides it really doesn’t want to allow me to change the name of the device, I can just change the name in one spot.
Seriously, we only go through this about every two years, but it was a bit annoying.
@Rozak thank you for this script!
but I have a small anomaly, a person has two devices. the notification is sent to the last registered device only. this is beyond my knowledge despite dozens of various attempts.
any idea how to fix this? thanks in advance !
Yes - I get the problem - it causes chaos if they have more than one ‘tracked’ device - especially if they don’t carry them around together… so I just use one device per person (their phone) - the algorithm will get complex I suspect. You could try and sort the list that is generated in the middle of the script using last_updated attribute?
yup - my folks randomly change their phone names too - thats why I did the script… you can always just manually manage the notifiers - but the script solution was just basically doing the join between information that is already inside HA in different places.
Thanks for the hack solutions gents. Let’s hope this gets implemented
I was inspired by this and just thought I should make a suggestion that might simplify the ‘call_list’ variable somewhat. Please note that I am relatively new so I might have missed something but I have had good results with the following sensor template (I chose this format to be able to use the notify services for different purposes, but the logic should be the same).
Please let me know if this helps or if I have misunderstood something. Not sure yet if other services can be present in the device_trackers attribute on a person (since I am only using mobile app myself). In that case more logic might be required, otherwise I think this should do the trick-
- unique_id: primary_devices
state: none
attributes:
friendly_name: "Notification services for devices owned by primary users"
devices: >
{% set primary_users = ['person1', 'person2'] %}
{%- set device_list = namespace(devices=[]) %}
{%- set devices = states.person | selectattr('attributes.id', 'in', primary_users) | map(attribute='attributes.device_trackers') %}
{%- for device in devices %}
{%- set device_list.devices = device_list.devices + device %}
{%- endfor %}
{{ device_list.devices | replace('device_tracker.','notify.mobile_app_') }}
it works perfectly but I didn’t get what integration is it or where should I put this config
That works - same logic without the exclusions and filtering - the only reason I added a whole pile of filtering to my cut was so I could be super flexible and call it in different ways.
I wanted to be able to selectively send app notifications to either everyone, an explicit list or everyone except an explicit list from other scripts and automations and just for ease of use I didn’t want to care about prepending ‘person.’ to everything - so the filters will handle ‘person.first_last’ or ‘first_last’ as input (that’s what the regex’s do).
Stick mikaelhertzman code in a template sensor (Templates - Home Assistant), or use mine as described just in a script (Scripts - Home Assistant) - they aren’t integrations - these are just config’s.