Create a group of devices to notify people that are at home

I am starting to work more wit actionable notifications these days and I’m having difficulties with te following scenario:

  • my wife and I both have a mobile phone with companion app installed that can receive notifications
  • sending notifications to a group is working well for messages that I want everybody to receive.
  • I have some things where I only want to send a message to the app when that person is at home.

I can do this in the automation manually every time, entering an if statement for myself and another one for my wife, but I would like to just call a group where alle devices that are at home are listed.

I’ve tried doing this by adding this in configuration.yaml:

notify:  
  - platform: group
    name: "mobile_devices_at_home"
    services: >-
      {% if is_state('person.simon','home') %}
        - service: mobile_app_simons_iphone_13mini
      {% endif %}
      {% if is_state('person.maren','home') %}
        - service: mobile_app_iphone_2
      {% endif %}

When checking yaml before restarting I get this message: Invalid config for ‘group’ from integration ‘notify’ at configuration.yaml, line 29: expected a dictionary ‘services->0’, got ‘{’

After a restart I get this error: The following integrations and platforms could not be set up: [group.notify] ([Show logs]

I would appreciate any pointers to try and solve this issue!

You can’t build YAML configuration using templates like that.

Approach it from the other direction… set up a notifier group for each Person entity in the household, making sure the names match. For example, if your Person entity’s friendly_name is “Bob”, the notifier group should be:

notify:
  - name: Bob
    platform: group
    services:
      - service: mobile_app_bob_phone
  - name: Alice
    platform: group
    services:
      - service: mobile_app_alice_phone
      - service: alexa_media_alice_bedroom_dot

Once you have your notifier groups set up you can use a template to dynamically define the list for the for_each of the Repeat action.

repeat:
  for_each: >
    {{ states.person | selectattr('state', 'eq' ,'home')
    | map(attribute = 'name' ) | list | slugify }}
  sequence:
    - service: notify.{{ repeat.item }}
      data:
        message: There's someone at the door

There is work being done to have a single notification service and convert the existing notification services into entities to make automations like this easier to set up. Currently it has only been implemented for simpler notifiers like File.

1 Like

Thanks for your reply! This seems like a reasonable alternative, yet it would be very sleek and require less programming in each automation if there would be a way to dynamically allocate members to the group of ‘at home’ devices. Would anybody have an idea how I could realise this idea?

You don’t need to do it in every automation. Set it up as a script, then in your automations call the script as a service, passing any necessary data as variables.

Creating a dynamic group of devices can be done using an automation that employees the group.set service. However, that isn’t really a solution because, currently, there is no direct link between device_tracker entities and their notification service. You would still need to create and maintain some method to link the device to the notifier service. This is one of the reasons why the notification integrations will be undergoing the conversion I mentioned in my post above.

Other Threads on the subject:
https://community.home-assistant.io/t/notify-person/159876/22

https://community.home-assistant.io/t/notify-alert-only-people-present-in-the-house-howto/277736

https://community.home-assistant.io/t/template-notifier-for-alert/244149/5

2 Likes

Thanks a lot! I will give your solution a try!