How to add unique id to persistent notification and binary sensor

building on the above I’ve made this template to use in a dismiss all script:

  dismiss_all:
    alias: 'Dismiss all Notifications'
    icon: mdi:message-bulleted-off
    sequence:
      service: persistent_notification.dismiss
      data_template:
        notification_id: >
          {%- set ns = namespace(notifications = '') %}
          {%- for item in states.persistent_notification %}
            {%- set id =  item.object_id %} #previously used entity_id....
            {%- set d = ', ' if ns.notifications | length > 0 else '' %}
            {%- set ns.notifications = ns.notifications ~ d ~ id %}
          {%- endfor %}
          {%- if ns.notifications|length|int == 0 %} None
          {% else %}
          {{- ns.notifications }}
          {%- endif %}

which shows the notifications as I would have thought they needed to be in a comma separated list, to use that in the notification-id field for the service:

however, clicking the script, (or using the developers-tools/service), nothing happens, not even a log entry to say what’s wrong :wink:

@petro would you know how to handle/solve this?

update
found this python_script by @pnbruckner which works perfectly, thanks! (isn’t object_id available in python for the notification_id ? )
Would still like to understand why the above jinja template doesn’t work in the script setting.