Routing a notification, but 'data' from Alert-integration is not a dictionary?

Hi,

I’m struggling with the following:
I have an automation that effectively ‘routes’ a notification to the correct recipients (more specifically, it catches notifications sent to a dummy service, decides who you should receive it and then then sends out a copy to each recipient). This has worked fine for notifications originating from Alarmo.
However, for notifications originating from the Alert-integration, I’m receiving the error “Error rendering data template: Result is not a Dictionary” for the step:

action: notify.{{ repeat.item }}
data: "{{ trigger.event.data.service_data }}"

I can’t make sense of this error though, because the trigger-variable (as shown in the automation trace) for both is essentially the same. (Using diffchecker on both:
Example with a notification originating from Alarmo:

trigger:
  id: '0'
  idx: '0'
  alias: Melding naar 'automatisering'
  platform: event
  event:
    event_type: call_service
    data:
      domain: notify
      service: automatisering
      service_data:
        title: Schakel het alarm uit
        message: alarmactivatie binnen 30 seconden
        data:
          doel: verantwoordelijke personen nu thuis
          priority: high
          ttl: 0
          channel: beveiliging
          tag: alarmo
          actions:
            - action: ALARMO_DISARM
              title: Uitschakelen
    origin: LOCAL
    time_fired: '2024-08-14T19:36:58.958928+00:00'
    context:
      id: 01J5969ETE7AJHTGXEM8CPQQY8
      parent_id: null
      user_id: null
  description: event 'call_service'

Example with a notification originating from Alert:

trigger:
  id: '0'
  idx: '0'
  alias: Melding naar 'automatisering'
  platform: event
  event:
    event_type: call_service
    data:
      domain: notify
      service: automatisering
      service_data:
        message: is nog open
        title: achterdeur
        data:
          doel: verantwoordelijke personen nu thuis
          priority: high
          ttl: 0
          channel: beveiliging
          tag: achterdeur
    origin: LOCAL
    time_fired: '2024-08-15T14:12:30.138788+00:00'
    context:
      id: 01J5B6419TH89SV0W3MRCEJB71
      parent_id: null
      user_id: null
  description: event 'call_service'

Diffchecker of both:


I have other Alarmo-notifications (that also work without issue) without “actions: …” so that (by itself) isn’t it.

So, if “{{ trigger.event.data.service_data }}” in the second case is not a dictionary, what is it and/or how could I make it one?
Normally, I’d use the template developer tool to figure out similar type issues; but here since it’s in the trigger variable of an automation/the data of an event, I can’t immediately see how I could do that.

Alerts aren’t automations. They have no triggers thus no trigger variables.

Replace your alert with an automation.

Well yes, obviously. Not sure how I could have gotten the above output if I was trying to use an alert as such as an automation though?

It probably got lost in the noise, but my original goal is to achieve what a “dynamic notify group” (which doesn’t exist, afaik) would do. To do so, I have both Alarmo and Alert send their notifications to a dummy notify service, configured as (so without any actual services associated):

notify:
  - platform: group
    name: automatisering
    services:

Then the automation mentioned in the original post has as trigger a notification being sent to this dummy notify:

trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: notify
      service: automatisering

Up to there, everything works as expected (or at least, I experience no issue with it).

The issue comes when I try to use {{ trigger.event.data.service_data }}, as this gives the error “Error rendering data template: Result is not a Dictionary” when the source of the trigger is a notification originating from Alert (but the same does not happen with one originating from Alarmo), while I see to functional difference in the trigger object of both (see above).

This is the entire automation, if that helps:

alias: meldingen routen
description: ""
trigger:
  - alias: Melding naar 'automatisering'
    platform: event
    event_type: call_service
    event_data:
      domain: notify
      service: automatisering
condition: []
action:
  - alias: Bepaal wie melding moet ontvangen en verstuur
    repeat:
      for_each: >-
        {% from 'personen.jinja' import wie_zijn %} {{
        wie_zijn(trigger.event.data.service_data.data.doel) | replace([], '') or ['An', 'Arvid'] }}
      sequence:
        - action: notify.{{ repeat.item }}
          data: "{{ trigger.event.data.service_data }}"
mode: queued
max: 10

I’ve actually made some progress: turns out that–for the notification originating from Alert–the nested ‘data’ (as appears under trigger.event.data.service_data.data) is not a regular dict but an OrderedDict.

I split:

data: "{{ trigger.event.data.service_data }}"

to

data:
  title: '{{ trigger.event.data.service_data.title }}'
  message: '{{ trigger.event.data.service_data.message }}'
  data: '{{ trigger.event.data.service_data.data }}'

and then the trace revealed:

Uitgevoerd op: 17 augustus 2024 om 13:01:13
Fout: expected dict for dictionary value @ data['data']
Resultaat:
params:
  domain: notify
  service: an
  service_data:
    title: achterdeur
    message: is nog open
    data: >-
      OrderedDict({'doel': 'verantwoordelijke personen nu thuis', 'priority':
      'high', 'ttl': 0, 'channel': 'beveiliging', 'tag': 'achterdeur'})
  target: {}
running_script: false

It seems the OrderedDict can be turned into a regular dict by using in the above split-version:

data: '{{ dict(trigger.event.data.service_data.data) }}'

Now just looking to do that from the original

data: "{{ trigger.event.data.service_data }}"

, so I don’t have to e.g. check if {{ trigger.event.data.service_data.title }} even exist.
({{ dict(trigger.event.data.service_data) } leaves the nested OrderedDict, so that isn’t the solution.)