Split Webhook output to multiple notifications

Heya,

I’m scratching my head on how to split my automation’s Webhook trigger to multiple notifications instead of just one big one using a service template.

Why?

Because the notification becomes truncated and you cannot see the entirety of the output! I’m attempting to use a for-loop that determines if we need to iterate over the webhook response. If the response contains more than 1 item, we’ll try to send an individual notification for each index.

My current automation is:

alias: Sonarr Webhooks
trigger:
  - platform: webhook
    id: sonarrSnatched
  - platform: webhook
    id: sonarrHealth
  - platform: webhook
    id: sonarrChange
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sonarrSnatched
        sequence:
          - service: |
              {% for episode in trigger.json.episodes %}
                {% if loop.length > 1 %}
                  notify.notify
                    title: Sonarr
                    message: >
                      Sonarr:
                        Event: {{ trigger.json.eventType }}
                        Title: {{ trigger.json.series.title }}
                        Episodes: 0{{ episode.seasonNumber }}x0{{ episode.episodeNumber }}
                        EpisodeTitle: {{ episode.title }}
                        Release: {{ trigger.json.release.releaseGroup }}
                        Quality: {{ trigger.json.release.quality }}
                  {% endif %}
                {% if loop.length == 0 %}
                  {% endif %}
                {% if loop.length == 1 %}
                  notify.notify
                    title: Sonarr
                    message: |
                      Sonarr:
                        Event: {{ trigger.json.eventType }}
                        Title: {{ trigger.json.series.title }}
                        Episode: 0{{ trigger.json.episodes[0].seasonNumber }}x0{{ trigger.json.episodes[0].episodeNumber}}
                        EpisodeTitle: {{ trigger.json.episodes[0].title }}
                        Release: {{ trigger.json.release.releaseGroup }}
                        Quality: {{ trigger.json.release.quality }}
                {% endif %}
              {% endfor%}
      - conditions:
          - condition: trigger
            id: sonarrChange
        sequence:
          - service: notify.notify
            data:
              title: Sonarr
              message: |-
                Sonarr:
                  Event: {{trigger.json.eventType.title()}}
                  Show: {{ trigger.json.series.title }}
                  Episode: 0{{ trigger.json.episodes[0].seasonNumber }}x0{{ trigger.json.episodes[0].episodeNumber }}
                  Change: {{ trigger.json.episodes[0].title }}
      - conditions:
          - condition: trigger
            id: sonarrHealth
        sequence:
          - service: notify.notify
            data:
              title: Sonarr
              message: "Sonarr:\n\tError: {{trigger.json.eventType.title()}}\n\tMessage: {{ trigger.json }}"
mode: restart

This is where I’m stuck, I get this in the logs when testing:

Error while executing automation automation.sonarr_webhooks: Template rendered invalid service

Ideas?

Thanks!

No idea if this will work, but something like this is closer to valid that yours:

repeat:
  for_each: "{{ trigger.json.episodes }}"
  sequence:
    - service: notify.notify
      data:
        title: "Sonarr"
        message: >
          Sonarr:
              Event: {{ trigger.json.eventType }}
              Title: {{ trigger.json.series.title }}
              Episodes: 0{{ repeat.item.seasonNumber }}x0{{ repeat.item.episodeNumber }}
              EpisodeTitle: {{ repeat.item.title }}
              Release: {{ trigger.json.release.releaseGroup }}
              Quality: {{ trigger.json.release.quality }}

Your template is attempting to produce “dynamic YAML” — it doesn’t work like that.

Oooo nice idea, let me give it a shot and let you know how it goes!