Sending notifications to devices/persons within a specific zone

I am working on a script to use in different automations that allows me to send notifications to multiple devices. To make my script work in different situations, I want to be able to filter the devices that are notified. I am starting with a filter that defines a zone, which a device has to be in to be notified.

variables:
  persons_in_zone: "{{ states('zone.home', 'persons') }}"
sequence:
  - repeat:
      count: '{{ persons_in_zone | count }}'
      sequence:
        - variables:
            device_name: "{{ state_attr(persons_in_zone[repeat.index-1], 'source') | replace('device_tracker.', '') }}"
        - service: 'notify.mobile_app_' + device_name
            data:
            message: 'test'

In my code the zone is hardcoded to ‘home’ for now.
The problem comes with the service call.

'notify.mobile_app_' + device_name

This does not match the required format for a service call. Anyone got an idea on how combine a service call with a variable?

- variables:
    device_name: "{{ 'notify.mobile_app_' + state_attr(persons_in_zone[repeat.index-1], 'source') | replace('device_tracker.', '') }}"
- service: "{{ device_name }}"

This also doesn’t work, because it’s still not what parser expects.

I appreciate any suggestions and ideas. Thanks in advance!

Why don’t you use the state of the device tracker as a condition?

When a device tracker is within a zone it’s state is the name of the zone. Then you can use if/then in an automation to achieve what you need!

variables:
  persons_in_zone: "{{ state_attr('zone.home', 'persons') }}"
sequence:
  - repeat:
      for_each: '{{ persons_in_zone }}'
      sequence:
        - variables:
            device_name: >
              {{ expand(repeat.item)
              | map(attribute='attributes.source')
              | list | join | replace('device_tracker.', '') }}
        - service: notify.mobile_app_{{ device_name}}
          data:
            message: 'test'
1 Like

Ok I found my mistake(s).
So to use a variable in a service call it has to look like this:

service: notify.mobile_app_{{device_name}}

And secondly, I used states where I should have used state_attr when setting the variables.
So it looks like this now and works:

variables:
  persons_in_zone: "{{ state_attr('zone.home', 'persons') }}"
sequence:
  - repeat:
      count: "{{ persons_in_zone | count }}"
      sequence:
        - variables:
            device_name: >-
              {{ state_attr(persons_in_zone[repeat.index-1], 'source') |
              replace('device_tracker.', '') }}
        - service: notify.mobile_app_{{device_name}}
          data:
            message: test
2 Likes

Yes that is the simple solution and that totally works! However, I like to keep the script a little more dynamic and not use an if-clause for every device_tracker and every automation.

1 Like

Hi @Bofrostmann11

This seems to be the solution to the exact challenge I’m trying to solve.
However, I’m not that experienced with using scripts in automations.

Could share the .yaml config of your automation, so I could see how you’ve implemented it in the automation actions?

Thank you :slight_smile: