Notify/alert only people present in the house, howto?

Using some other messages as inspiration - I’ve created a similar script to only notify people based on their location. It relies on having a custom attribute for each person to link people to their notifier.

For example:

configuration.yaml

homeassistant:
  customize: !include customize.yaml

customize.yaml

person.t:
  notifier: notify.mobile_app_t
person.a:
  notifier: notify.mobile_app_a

script_to_notify_based_on_location.yaml

alias: Notify people based on location
mode: parallel
fields:
  TITLE:
    description: Notification title
    example: "Example Notification"
    required: true
  MESSAGE:
    description: Message of notification
    example: "Something is happening"
    required: true
  LOCATION:
    description: State of people to send notification to
    example: "home"
    default: "home"
    required: false
  DEBUG:
    description: Only send messages to T
    example: true
    default: false
    required: false
sequence:
  # Determine all the people at scripted location to send notification to
  # Have debug handling to only send to me when debug = true
  - variables:
      NOTIFIERS: >
        {% if DEBUG %}
          {{ states.person | selectattr("entity_id", "eq", "person.t") | selectattr('state', 'eq', LOCATION) | map(attribute='attributes.notifier') | list }}
        {% else %}
          {{ states.person | selectattr('state', 'eq', LOCATION) | map(attribute='attributes.notifier') | list }}
        {% endif %}
  # Iterate through all the people and send the relevant notification
  - repeat:
      count: "{{ NOTIFIERS | count }}"
      sequence:
        - variables:
            NOTIFIER: "{{ NOTIFIERS[repeat.index - 1] }}"
        - service: "{{ NOTIFIER }}"
          data:
            title: "{{ TITLE }}"
            message: "{{ MESSAGE }}"
3 Likes