Dynamic notification groups

I have a few iOS devices I want to send notifications to. I know that I can create a notification group like this

  - name: ios_devices
    platform: group
      - service: mobile_app_iphone
      - service: mobile_app_ipad

but I want to have a group “iOS_devices_at_home” (and one “not_at_home”) so I can send notifications also to only devices that are currently home (e.g. plants out of water etc.). These groups would need to be dynamically updated

My idea was to create an automation that is triggered by changes from home to away and vice versa and then adds any devices currently at home to the “…at_home” group.

I tried using the service group.set with the following code

service: group.set
  object_id: ios_devices_at_home
    - |-
      {% set devices_at_home = states.device_tracker
         | selectattr('state', 'equalto', 'home')
         | map(attribute='entity_id') | list %}
      {{ devices_at_home }}
  all: false

It does not work and I discovered that the group.set service apparently only works for standard groups not for notification groups.

Does anyone have an idea how I could update the notification group or also other first principles ideas how to achieve this in general?

I have this syntax for a battery group, seems just a bit different from yours:

      service: group.set
        object_id: battery_sensors_auto
        icon: mdi:battery-sync
        entities: >
         {{- label_entities('batterij')}}

Having said that I am not sure dont think you can do that with notification groups.

(if anything, I believe they come to the UI group functionality, albeit probably without being dynamic…)

seems you could do what you want without a notification group, by templating the devices in the data service option

1 Like

You can make a regular group with all the devices (notify entities). Then make a script that takes a message as a parameter. Expand the group and iterate over it while checking a presence sensor or device tracker for each.

I can try and type out an example tomorrow if you’d like to go this route.

I like the direction of this, but how would you do the presence check?

Edit: I think I see it: update the group dynamically and use that with an expand in a template.

if device_tracker.mobile_phone_1 == home, template object_id and use service notify.mobile_phone_1

and do so for the trackers involved?

but, given the fact there wont be an endless amount of mobile_app devices involved, why not just use an if/then in an automation, and have the message pasted in all of the then blocks

      - if:
          service: notify.mobile_app_1
          data: &notification
            title: My message title
            message: >
              {{whatever template you like }}

      - if:
          service: notify.mobile_app_2
          data: *notification

      - if:
          service: notify.mobile_app_3
          data: *notification

seems quite doable

1 Like