I recently got a new phone and had to update all of the hardcoded notify.mobile_app_*
service calls, and thought it would be nice to future proof my next phone upgrade with the advent of response_variable
in 2023.7. I figure someone else may find the script useful for this purpose, or as a ‘how to’ demonstration for utilizing the new response_variable
I’m sure not everyone’s system is setup the same way as mine, so I will mention that on my installation, the device_tracker
attribute of person.xyz
refers to two entities from the device_tracker
domain:
- one from my router’s integration (wifi connection, with an
ip
attribute), and - one from the mobile app installed on my phone (no
ip
attribute)
Additionally, only the device_tracker
from my mobile app accepts notify
service calls, I wanted to filter out the device_tracker
with an ip
attribute.
Here is my script:
alias: get_mobile_app
sequence:
- variables:
mobile_app: >-
{'mobile_app':"{{state_attr(person,'device_trackers')|expand|rejectattr('attributes.ip','defined')|map(attribute='object_id')|join(',')}}"}
- stop: Return mobile_app ID
response_variable: mobile_app
mode: parallel
and here is an example automation:
alias: test-mobile
trigger: []
condition: []
action:
- service: script.get_mobile_app
data:
person: person.xyz
response_variable: xyz
- service: notify.mobile_app_{{xyz.mobile_app}}
data:
message: "Hello World"
mode: single
If anyone who works on the HASS backend code is reading this, I’d say it would be nice to be able to define a variable directly as a call to a responsive script.
I’m sure someone with a little bit of time could clean it up to be more universally adaptable, or give it the ability to notify all entities in the person
domain, but I wanted to offer this to future searchers as a thanks to the community for providing answers to my past google searches.