Expand on this notification filer automation

using this:

  - alias: 'Forward notifications to filed_notifications'
    id: 'Forward notifications to filed_notifications'
  #  initial_state: 'off'
    trigger:
      platform: event
      event_type: call_service
      event_data:
        domain: notify
        service: notify
    condition: []
    action:
      service: notify.filed_notifications
      data_template:
        message: >
          {% set message = trigger.event.data.service_data.message %}
             {{ as_timestamp(now()) | timestamp_custom('%d %b: %X') }}: {{ message }}

I am sending all notify.notify messages to a filed_notifications.txt.

Id like to expand this to all services in the notify domain, of course excluding the notify.filed_notifications itself.

Could I do that leaving out the service: notify from the trigger?

secondly, Id like to add the service to the notification in the file.

right now the notifications look like:

11 Mar: 22:22:04: Netwerk Dorm switched off
I’d like to expand that too, with the relevant service, but am not sure what trigger data to use. would that be trigger.event.data.service_data.service ?

@pnbruckner since you helped me create this automation some time ago, allow me to ask for your assistance once more?

it wasn’t trigger.event.data.service_data.service but should be trigger.event.data.service
would this be correct:

  - alias: 'Forward notifications to filed_notifications'
    id: 'Forward notifications to filed_notifications'
  #  initial_state: 'off'
    trigger:
      platform: event
      event_type: call_service
      event_data:
        domain: notify
#        service: notify
    condition:
      condition: template
      value_template: >
        {{trigger.event.data.service != filed_notifications}}
    action:
      service: notify.filed_notifications
      data_template:
        message: >
          {% set message = trigger.event.data.service_data.message %}
          {% set service = trigger.event.data.service_data.service %}
             {{ as_timestamp(now()) | timestamp_custom('%d %b: %X') }} - {{service}}: {{ message }} 

of course I have tried this:
1- it triggers on all notifications services, which is what I hoped for.
2- BUT: {{trigger.event.data.service != filed_notifications}} doesn’t prevent the automation from triggering on the filed_notifications, and renders an infinite loop of nested notifications…

3- I do get this as filed notification:

11 Mar: 23:27:36 - notify: 23:27:36: Sun is below_horizon and Frontend is set to 'darkblue'

which means the service notify is displayed correctly, so trigger.event.data.service is the correct way to get that.

4- Why then won’t it filter out the filed_notifications in the condition?

This should work for the condition

    condition:
      condition: template
      value_template: >
        {{ trigger.event.data.service != 'filed_notifications' }}

of course! that would be it. thanks!

had already started to try it in another way, by specifying the individual services explicitly:

trigger:
  platform: event
  event_type: call_service
  event_data:
    domain: notify
    service:
      - notify
      - ios_telefoon
      - ios_iphone
      - name1
      - name2

would that be ok too, for services notify.notify, notify.ios_telefoon, notify.ios_iphone etc etc?

@petro is right, of course. :slight_smile:

And, no, listing the services that way won’t work.

Basically, the event_data parameter of an event trigger is a ā€œschemaā€. Anything listed under it must match what is in the event data if the listed keys are actually in the event data. Also note that event_data effectively is the same as trigger.event.data.

Maybe an example will help. Let’s say trigger.event looks like this (I’ll use a call_service event, since that’s what we’re talking about here):

trigger.event.event_type = 'call_service'
trigger.event.data.domain = 'notify'
trigger.event.data.service = 'ios_iphone'
trigger.event.data.service_data.message = 'Yikes!'

These event triggers will ā€œmatchā€:

event_type: call_service
event_type: call_service
event_data:
  domain: notify
event_type: call_service
event_data:
  service: ios_phone
event_type: call_service
event_data:
  domain: notify
  service: ios_phone

But so will:

event_type: call_service
event_data:
  what_the_heck: "You said it!"

This is because this only means, if trigger.event.data actually has a what_the_heck field, then it must be ā€œYou said it!ā€. But if trigger.event.data doesn’t have a what_the_heck field, then it’s a don’t care.

Now, to your question:

trigger:
  platform: event
  event_type: call_service
  event_data:
    domain: notify
    service:
      - notify
      - ios_telefoon
      - ios_iphone
      - name1
      - name2

This says that trigger.event.data.service must be a list that contains those five values. But, of course, that will never be, so it would not match any call_service event.

1 Like

thanks Phil,

I’ll note this in the textbook, a very concise explanation. Its running perfectly now, using 2 exceptions in my conditions.

Note to my self that when wanting a positive (including) specified trigger on a service, I’d need something like:

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

for now I’m using the excluding condition, and will see if that doesn’t overload the processor :wink:

Because of this, I’ve finally found out why I had several double notifications Ive been trying to explain for a long time. Turns out I had setup a notification group for my family’s devices and had setup a ā€˜group’ for my own device alone too… I now see that sends the notification twice. to the ā€˜group’ and the individual device. Got that cleared.

Been an eductive one this one :+1:

HI Phil,

just because it is slightly related, using multiple services in a list:

can I send the same notification to more than one notify platform like this:

action:
  service: 
    - notify.ios_telefoon
    - notify.ios_iphone
  data_template:
    title: 'Batterij Telefoon bijna leeg'
    message: >
      {{states('sensor.battery_telefoon')}} %!

I tried it but got cryptical error messages which could be related to a temporary meltdown too… so thought I’d better ask , just in case…

No, I don’t think so.