Dynamic notification groups - what's the best way?

Is there any way to make a dynamic notification group based on a multi input_select or something similar? Something where I can easily add entities.

Like if there was a hash table function for jinja where you could also have a notify group with a template_services section.

Right now I’m just using a bunch of notify groups and a service_template in my automation:

      - service_template: >
          {% if is_state('input_boolean.wakeup_notify_person1','on') and is_state('input_boolean.wakeup_notify_person2','on') %}
            notify.everyone
          {% elif is_state('input_boolean.wakeup_notify_person1','on') %}
            notify.person1
          {% elif is_state('input_boolean.wakeup_notify_person2','on') %}
            notify.person2
          {% else %}
            notify.nobody
          {% endif %}
1 Like

I researched doing something similar a while back but never found any way to add multiple entries with a single entity. What you have now looks pretty efficient. Are you just trying to save space in the UI?

Well this will get really complicated when adding a 3rd person or device.

I can’t see a way to even loop through several different options in a script doing basic if statements for each person.

Perhaps a master notification script that calls a different script for each potential person to be notified.

That’s what I was thinking (but as automations): Using the same service_template you have now but with automation.notify_everyone, automation.notify_person1, etc.

not really sure what you want, and what the full automation looks like, but depending on your triggering entities, you might be able to use the {{trigger.to_state}} in the service_template?

service_template:
  notify.wakeup_notify_{{trigger.to_state.name}}

need to find the correct filter to only use the final part of the name of course but you get the idea?

something else you might be able to use: I have input_booleans to activate my media_players and to allow these active players be used in the media_player services via the sensor.media_players_active.

      media_players_active:
        friendly_name_template: >
          {% set count = states.input_boolean
            |selectattr('entity_id','in',state_attr('group.activate_media_players','entity_id'))
            |selectattr('state','eq','on')|list|length %} 
          {% set player = 'player' if count in [0,1] else 'players' %}
          {% set number = 'No' if count == 0 else count %} 
          {{number}} Media {{player}} active
        entity_id:
          - input_boolean.googlehome_1
          - input_boolean.googlehome_2
          - input_boolean.googlehome_3
          - input_boolean.googlehome_4
          - input_boolean.googlehome_5
          - input_boolean.googlehome_6
        value_template: >
          {% set rooms = states.input_boolean
            |selectattr('entity_id','in',state_attr('group.activate_media_players','entity_id'))
            |selectattr('state','eq','on') |map(attribute='object_id')|list %}
          {% set ns = namespace(speakers = '') %}
          {% for i in range(rooms | length) %}
            {% if states('input_boolean.' ~ rooms[i]) == 'on' %}
            {% set d = ', ' if ns.speakers | length > 0 else '' %}
            {% set ns.speakers = ns.speakers ~ d ~ 'media_player.' ~ rooms[i] %}
            {% endif %}
          {% endfor %}
          {% if ns.speakers|length|int == 0 %} None
          {% else %}
          {{ ns.speakers }}
          {% endif %}

Did anyone find out a way to do this? I only want to notify people that are currently at X location

2 Likes

To revive a very old topic, I created a script which is called from various automations, setting the icon, group etc via variables (if set), and it sends it out to everyone who is home.

notify:
  alias: Notification
  icon: mdi:cellphone-information
  sequence:
    - repeat:
        for_each: >
          {{ states.person | selectattr('state','eq','home') | map(attribute='object_id') | list }}
        sequence:
          - service: notify.mobile_app_{{ repeat.item }}
            data:
              message: "{{ message }}"
              data:
                notification_icon: "{%- if notification_icon is defined -%}{{ notification_icon }}{%- else -%}mdi:home-assistant{%- endif -%}"
                group: "{%- if notification_icon is defined -%}{{ notification_icon }}{%- endif -%}"
                tag: "{%- if tag is defined -%}{{ tag }}{%- endif -%}"
                ttl: 0
                priority: high

e.g. calling it

      - service: script.notify
        data:
          notification_icon: mdi:washing-machine
          message: Washing Machine has finished
          tag: washing-machine
1 Like